Created
June 8, 2016 20:20
-
-
Save oleksiiBobko/1b8820b490422dd015b1fb765e3f6301 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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