From 6190f6801fd054e50514516f3ba588d1972c8848 Mon Sep 17 00:00:00 2001 From: Seshu Thanneeru Date: Mon, 17 Jan 2022 21:43:26 +0530 Subject: [PATCH] Changes as per review comments --- .../identity/IdentityHashMapDemonstrator.java | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/java-collections-maps-3/src/main/java/com/baeldung/map/identity/IdentityHashMapDemonstrator.java b/java-collections-maps-3/src/main/java/com/baeldung/map/identity/IdentityHashMapDemonstrator.java index 9f5eeea93c..155cdc06e7 100644 --- a/java-collections-maps-3/src/main/java/com/baeldung/map/identity/IdentityHashMapDemonstrator.java +++ b/java-collections-maps-3/src/main/java/com/baeldung/map/identity/IdentityHashMapDemonstrator.java @@ -22,7 +22,8 @@ public class IdentityHashMapDemonstrator { IdentityHashMap identityHashMap = new IdentityHashMap<>(); identityHashMap.put(null, "Null Key Accepted"); identityHashMap.put("Null Value Accepted", null); - System.out.println(identityHashMap); + assert ("Null Key Accepted" == identityHashMap.get(null)); + assert (null == identityHashMap.get("Null Value Accepted")); } private static void iterateIdentityHashMap(IdentityHashMap identityHashMap) { @@ -89,29 +90,30 @@ public class IdentityHashMapDemonstrator { private static void demoMutableKeys() { Book book1 = new Book("A Passage to India", 1924); Book book2 = new Book("Invisible Man", 1953); + HashMap hashMap = new HashMap<>(10); - IdentityHashMap identityHashMap = new IdentityHashMap<>(10); - hashMap.put(book1, "A great work of fiction"); - identityHashMap.put(book1, "A great work of fiction"); - hashMap.put(book2, "won the US National Book Award"); - identityHashMap.put(book2, "won the US National Book Award"); - book2.year = 1952; - System.out.println("Book2 from HashMap: " + hashMap.get(book2)); - System.out.println("Book2 from IdentityHashMap: " + identityHashMap.get(book2)); + assert (null == hashMap.get(book2)); System.out.println("HashMap: " + hashMap); + + IdentityHashMap identityHashMap = new IdentityHashMap<>(10); + identityHashMap.put(book1, "A great work of fiction"); + identityHashMap.put(book2, "won the US National Book Award"); + book2.year = 1951; + assert ("won the US National Book Award" == identityHashMap.get(book2)); System.out.println("IdentityHashMap: " + identityHashMap); } private static void demoHashMapVsIdentityMap(IdentityHashMap identityHashMap) { HashMap hashMap = new HashMap<>(identityHashMap); hashMap.put(new String("genre"), "Drama"); - identityHashMap.put(new String("genre"), "Drama"); - System.out.println("HashMap size: " + hashMap.size()); - System.out.println("IdentityHashMap size: " + identityHashMap.size()); + assert (4 == hashMap.size()); System.out.println("HashMap content: " + hashMap); + + identityHashMap.put(new String("genre"), "Drama"); + assert (5 == identityHashMap.size()); System.out.println("IdentityHashMap content: " + identityHashMap); } @@ -127,8 +129,8 @@ public class IdentityHashMapDemonstrator { private static void updateWithNewValue(IdentityHashMap identityHashMap) { String oldTitle = identityHashMap.put("title", "Harry Potter and the Deathly Hallows"); - System.out.println("Old Title: " + oldTitle); - System.out.println("Updated Title: " + identityHashMap.get("title")); + assert ("Harry Potter and the Goblet of Fire" == oldTitle); + assert ("Harry Potter and the Deathly Hallows" == identityHashMap.get("title")); } public static void addValue(IdentityHashMap identityHashMap, String key, String value) {