Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- // Using HashMap
- // with a key with no hashCode or equals over-riding
- public class Test {
- public static void main(String[] args) {
- HashMap<MyClass, String> map = new HashMap<>();
- MyClass a = new MyClass(1);
- MyClass b = new MyClass(2);
- MyClass c = new MyClass(3);
- map.put(a, "One");
- map.put(b, "Two");
- map.put(c, "Three");
- String str = map.get(a);
- System.out.println(str); // One
- map.put(b, "Change"); // update value of key b
- str = map.get(b);
- System.out.println(str); // Change
- }
- }
- class MyClass // key class
- {
- int x;
- MyClass(int x) {
- this.x = x;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment