From f802d3d33773db703d994c307d29fb152d9e80de Mon Sep 17 00:00:00 2001 From: Seshu Thanneeru Date: Sat, 8 Jan 2022 20:45:16 +0530 Subject: [PATCH] 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)); + } +}