Skip to content

Instantly share code, notes, and snippets.

@daveRanjan
Created July 14, 2022 09:52
Show Gist options
  • Save daveRanjan/bea5fdca57479ba15aff206985084a44 to your computer and use it in GitHub Desktop.
Save daveRanjan/bea5fdca57479ba15aff206985084a44 to your computer and use it in GitHub Desktop.
Coding Question 1
import java.util.HashMap;
import java.util.Map;
public class HashMapTest {
public static class A {
String name;
public int hashCode() {
return name.hashCode();
//return 10;
}
public boolean equals(Object o) {
if (o instanceof A) {
return name.equals(((A) o).name);
}
return false;
}
}
public static void main(String[] args) {
Map<A, Integer> map = new HashMap<>();
A a1 = new A();
a1.name="one";
map.put(a1, 1);
a1.name="two";
A a2=new A();
a2.name="one";
map.put(a2, 2);
//What will be the output below?
System.out.println(map.get(a1));
System.out.println(map.get(a2));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment