Skip to content

Instantly share code, notes, and snippets.

@oleksiiBobko
Created June 8, 2016 20:20
Show Gist options
  • Save oleksiiBobko/1b8820b490422dd015b1fb765e3f6301 to your computer and use it in GitHub Desktop.
Save oleksiiBobko/1b8820b490422dd015b1fb765e3f6301 to your computer and use it in GitHub Desktop.
package x.y.z;
import java.util.HashMap;
import java.util.Map;
/**
* Hello world!
*
*/
public class App {
public static void main(String[] args) {
new App().f();
}
public void f() {
Map<A, String> map = new HashMap<A, String>();
A a = new A(3);
map.put(a, "value");
System.out.println(map.get(a));
a.setI(4);
System.out.println(map.get(a));
a.setI(3);
System.out.println(map.get(a));
}
private class A {
private int i;
public A(int i) {
this.i = i;
}
public void setI(int i) {
this.i = i;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + getOuterType().hashCode();
result = prime * result + i;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
A other = (A) obj;
if (!getOuterType().equals(other.getOuterType()))
return false;
if (i != other.i)
return false;
return true;
}
private App getOuterType() {
return App.this;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment