Untitled
unknown
java
a year ago
1.6 kB
8
Indexable
// Online Java Compiler // Use this editor to write, compile and run your Java code online import java.util.HashMap; class A { public int a; public A(int value) { this.a = value; } public boolean equals(Object other) { A other_a = (A)other; return this.a == other_a.a; } public int hashCode() { return this.a; } public String toString() { return "id:" + String.valueOf(System.identityHashCode(this)) + " a:" + String.valueOf(this.a); } } class HelloWorld { public static void main(String[] args) { A a = new A(1); A b = new A(2); HashMap<A, Integer> d = new HashMap<A, Integer>(); d.put(a, 1); d.put(b, 2); System.out.println(a.toString()); System.out.println(b.toString()); System.out.println("eq " + a.equals(b)); System.out.println("hash " + (a.hashCode() == b.hashCode())); System.out.println(d.toString()); for(A key: d.keySet()) { if (key.a == 1) { key.a = 2; } } System.out.println(a.toString()); System.out.println(b.toString()); System.out.println("eq " + a.equals(b)); System.out.println("hash " + (a.hashCode() == b.hashCode())); System.out.println(d.toString()); HashMap<A, Integer> new_d = new HashMap<A, Integer>(); new_d.put(a, 1); new_d.put(b, 2); System.out.println(new_d.toString()); } }
Editor is loading...
Leave a Comment