From 2bf03506390daaafd7486fc7fccd9fc492369e9b Mon Sep 17 00:00:00 2001 From: Seshu Thanneeru Date: Sat, 8 Jan 2022 20:45:16 +0530 Subject: [PATCH 1/5] IdentityHashMapDemonstrator class --- .../identity/IdentityHashMapDemonstrator.java | 150 ++++++++++++++++++ .../IdentityHashMapDemonstratorUnitTest.java | 21 +++ 2 files changed, 171 insertions(+) create mode 100644 java-collections-maps-3/src/main/java/com/baeldung/map/identity/IdentityHashMapDemonstrator.java create mode 100644 java-collections-maps-3/src/test/java/com/baeldung/map/identity/IdentityHashMapDemonstratorUnitTest.java 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 new file mode 100644 index 0000000000..9f5eeea93c --- /dev/null +++ b/java-collections-maps-3/src/main/java/com/baeldung/map/identity/IdentityHashMapDemonstrator.java @@ -0,0 +1,150 @@ +package com.baeldung.map.identity; + +import java.util.*; + +public class IdentityHashMapDemonstrator { + public static void main(String[] args) { + IdentityHashMap identityHashMap = createWithSimpleData(); + System.out.println("Map details: " + identityHashMap); + IdentityHashMap copiedMap = createFromAnotherMap(identityHashMap); + + updateWithNewValue(copiedMap); + iterateIdentityHashMap(copiedMap); + addNullKeyValue(); + demoHashMapVsIdentityMap(identityHashMap); + demoMutableKeys(); + + Map synchronizedMap = getSynchronizedMap(); + //Do multithreaded operations on synchronizedMap + } + + private static void addNullKeyValue() { + IdentityHashMap identityHashMap = new IdentityHashMap<>(); + identityHashMap.put(null, "Null Key Accepted"); + identityHashMap.put("Null Value Accepted", null); + System.out.println(identityHashMap); + } + + private static void iterateIdentityHashMap(IdentityHashMap identityHashMap) { + // Iterating using entrySet + System.out.println("Iterating values: "); + Set> entries = identityHashMap.entrySet(); + for (Map.Entry entry: entries) { + System.out.println(entry.getKey() + ": " + entry.getValue()); + } + + // Iterating using keySet + System.out.println("Iterating values using keySet: "); + for (String key: identityHashMap.keySet()) { + System.out.println(key + ": " + identityHashMap.get(key)); + } + + // Throws error if we modify while iterating + System.out.println("This iteration throws error: "); + try { + for (Map.Entry entry: entries) { + System.out.println(entry.getKey() + ": " + entry.getValue()); + identityHashMap.remove("title"); + } + } catch (ConcurrentModificationException ex) { + System.out.println("This exception will raise for sure, if we modify while iterating"); + } + } + + private static class Book { + String title; + int year; + + Book() { + // nothing to do + } + + Book(String title, int year) { + this.title = title; + this.year = year; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Book book = (Book) o; + return year == book.year && title.equals(book.title); + } + + @Override + public int hashCode() { + return Objects.hash(title, year); + } + + @Override + public String toString() { + return "Book{" + + "title='" + title + '\'' + + ", year=" + year + + '}'; + } + } + + 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)); + System.out.println("HashMap: " + hashMap); + 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()); + System.out.println("HashMap content: " + hashMap); + System.out.println("IdentityHashMap content: " + identityHashMap); + } + + private static Map getSynchronizedMap() { + Map synchronizedMap = Collections.synchronizedMap(new IdentityHashMap()); + return synchronizedMap; + } + + private static IdentityHashMap createFromAnotherMap(Map otherMap) { + IdentityHashMap identityHashMap = new IdentityHashMap<>(otherMap); + return identityHashMap; + } + + 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")); + } + + public static void addValue(IdentityHashMap identityHashMap, String key, String value) { + identityHashMap.put(key, value); + } + + public static void addAllValues(IdentityHashMap identityHashMap, Map otherMap) { + identityHashMap.putAll(otherMap); + } + + public static IdentityHashMap createWithSimpleData() { + IdentityHashMap identityHashMap = new IdentityHashMap<>(); + identityHashMap.put("title", "Harry Potter and the Goblet of Fire"); + identityHashMap.put("author", "J. K. Rowling"); + identityHashMap.put("language", "English"); + identityHashMap.put("genre", "Fantasy"); + return identityHashMap; + } +} diff --git a/java-collections-maps-3/src/test/java/com/baeldung/map/identity/IdentityHashMapDemonstratorUnitTest.java b/java-collections-maps-3/src/test/java/com/baeldung/map/identity/IdentityHashMapDemonstratorUnitTest.java new file mode 100644 index 0000000000..cc74ce4dd6 --- /dev/null +++ b/java-collections-maps-3/src/test/java/com/baeldung/map/identity/IdentityHashMapDemonstratorUnitTest.java @@ -0,0 +1,21 @@ +package com.baeldung.map.identity; + +import org.junit.jupiter.api.Test; + +import java.util.IdentityHashMap; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class IdentityHashMapDemonstratorUnitTest { + + @Test + public void givenIdentityHashMap_whenNewObjectWithSameKey_thenAddsAsNewValue() { + IdentityHashMap identityHashMap = IdentityHashMapDemonstrator.createWithSimpleData(); + String newGenreKey = new String("genre"); + identityHashMap.put(newGenreKey, "Drama"); + + assertEquals(5, identityHashMap.size()); + assertEquals("Fantasy", identityHashMap.get("genre")); + assertEquals("Drama", identityHashMap.get(newGenreKey)); + } +} From bc57937126429c2c86c85086116cc6c5f5cb25b3 Mon Sep 17 00:00:00 2001 From: Seshu Thanneeru Date: Mon, 17 Jan 2022 21:43:26 +0530 Subject: [PATCH 2/5] 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) { From cdc4005047fae5538099890aca27ac097adba4d7 Mon Sep 17 00:00:00 2001 From: Seshu Thanneeru Date: Wed, 19 Jan 2022 19:44:51 +0530 Subject: [PATCH 3/5] changed code to show copy-paste ready code comment --- .../map/identity/IdentityHashMapDemonstrator.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 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 155cdc06e7..dbd309eab3 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 @@ -11,7 +11,7 @@ public class IdentityHashMapDemonstrator { updateWithNewValue(copiedMap); iterateIdentityHashMap(copiedMap); addNullKeyValue(); - demoHashMapVsIdentityMap(identityHashMap); + demoHashMapVsIdentityMap(); demoMutableKeys(); Map synchronizedMap = getSynchronizedMap(); @@ -106,7 +106,13 @@ public class IdentityHashMapDemonstrator { System.out.println("IdentityHashMap: " + identityHashMap); } - private static void demoHashMapVsIdentityMap(IdentityHashMap identityHashMap) { + private static void demoHashMapVsIdentityMap() { + IdentityHashMap identityHashMap = new IdentityHashMap<>(); + identityHashMap.put("title", "Harry Potter and the Goblet of Fire"); + identityHashMap.put("author", "J. K. Rowling"); + identityHashMap.put("language", "English"); + identityHashMap.put("genre", "Fantasy"); + HashMap hashMap = new HashMap<>(identityHashMap); hashMap.put(new String("genre"), "Drama"); assert (4 == hashMap.size()); From a25d1e76363ad9f4a187716a2f9c8c075887012f Mon Sep 17 00:00:00 2001 From: Seshu Thanneeru Date: Wed, 19 Jan 2022 20:19:11 +0530 Subject: [PATCH 4/5] assert statement replaced with assertEquals function --- java-collections-maps-3/pom.xml | 7 +++++++ .../identity/IdentityHashMapDemonstrator.java | 17 +++++++++-------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/java-collections-maps-3/pom.xml b/java-collections-maps-3/pom.xml index 81466b17ba..a54061404d 100644 --- a/java-collections-maps-3/pom.xml +++ b/java-collections-maps-3/pom.xml @@ -16,6 +16,13 @@ + + + org.junit.jupiter + junit-jupiter-api + 5.8.1 + + org.springframework spring-core 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 dbd309eab3..faba9f60f8 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 @@ -1,6 +1,7 @@ package com.baeldung.map.identity; import java.util.*; +import static org.junit.jupiter.api.Assertions.assertEquals; public class IdentityHashMapDemonstrator { public static void main(String[] args) { @@ -22,8 +23,8 @@ public class IdentityHashMapDemonstrator { IdentityHashMap identityHashMap = new IdentityHashMap<>(); identityHashMap.put(null, "Null Key Accepted"); identityHashMap.put("Null Value Accepted", null); - assert ("Null Key Accepted" == identityHashMap.get(null)); - assert (null == identityHashMap.get("Null Value Accepted")); + assertEquals("Null Key Accepted", identityHashMap.get(null)); + assertEquals(null, identityHashMap.get("Null Value Accepted")); } private static void iterateIdentityHashMap(IdentityHashMap identityHashMap) { @@ -95,14 +96,14 @@ public class IdentityHashMapDemonstrator { hashMap.put(book1, "A great work of fiction"); hashMap.put(book2, "won the US National Book Award"); book2.year = 1952; - assert (null == hashMap.get(book2)); + assertEquals(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)); + assertEquals("won the US National Book Award", identityHashMap.get(book2)); System.out.println("IdentityHashMap: " + identityHashMap); } @@ -115,11 +116,11 @@ public class IdentityHashMapDemonstrator { HashMap hashMap = new HashMap<>(identityHashMap); hashMap.put(new String("genre"), "Drama"); - assert (4 == hashMap.size()); + assertEquals(4, hashMap.size()); System.out.println("HashMap content: " + hashMap); identityHashMap.put(new String("genre"), "Drama"); - assert (5 == identityHashMap.size()); + assertEquals(5, identityHashMap.size()); System.out.println("IdentityHashMap content: " + identityHashMap); } @@ -135,8 +136,8 @@ public class IdentityHashMapDemonstrator { private static void updateWithNewValue(IdentityHashMap identityHashMap) { String oldTitle = identityHashMap.put("title", "Harry Potter and the Deathly Hallows"); - assert ("Harry Potter and the Goblet of Fire" == oldTitle); - assert ("Harry Potter and the Deathly Hallows" == identityHashMap.get("title")); + assertEquals("Harry Potter and the Goblet of Fire", oldTitle); + assertEquals("Harry Potter and the Deathly Hallows", identityHashMap.get("title")); } public static void addValue(IdentityHashMap identityHashMap, String key, String value) { From 5794989dae995f191c5d466224c1dc2fcfcd9a27 Mon Sep 17 00:00:00 2001 From: Seshu Thanneeru Date: Thu, 20 Jan 2022 19:09:27 +0530 Subject: [PATCH 5/5] reformatted long line continuation --- .../baeldung/map/identity/IdentityHashMapDemonstrator.java | 5 +---- 1 file changed, 1 insertion(+), 4 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 faba9f60f8..3cbe09b93f 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 @@ -81,10 +81,7 @@ public class IdentityHashMapDemonstrator { @Override public String toString() { - return "Book{" + - "title='" + title + '\'' + - ", year=" + year + - '}'; + return "Book{title='" + title + "', year=" + year + "}"; } }