diff --git a/core-java-modules/core-java-collections-maps-7/src/main/java/com/baeldung/map/HashMapWithMaxSizeLimit.java b/core-java-modules/core-java-collections-maps-7/src/main/java/com/baeldung/map/HashMapWithMaxSizeLimit.java new file mode 100644 index 0000000000..4d2d6e8d53 --- /dev/null +++ b/core-java-modules/core-java-collections-maps-7/src/main/java/com/baeldung/map/HashMapWithMaxSizeLimit.java @@ -0,0 +1,25 @@ +package com.baeldung.map; + +import java.util.HashMap; + +public class HashMapWithMaxSizeLimit extends HashMap { + + private static final long serialVersionUID = 1L; + + private int maxSize = -1; + + + public HashMapWithMaxSizeLimit(int maxSize) { + super(); + this.maxSize = maxSize; + } + + + public V putWithLimit(K key, V value) throws Exception { + if (this.maxSize != - 1 && this.size() >= this.maxSize && !this.containsKey(key)) { + throw new Exception("Max size exceeded!"); + } + return this.put(key, value); + } + +} diff --git a/core-java-modules/core-java-collections-maps-7/src/test/java/com/baeldung/map/LimitMaxSizeHashMapByCustomHashMapUnitTest.java b/core-java-modules/core-java-collections-maps-7/src/test/java/com/baeldung/map/LimitMaxSizeHashMapByCustomHashMapUnitTest.java new file mode 100644 index 0000000000..5be9c61245 --- /dev/null +++ b/core-java-modules/core-java-collections-maps-7/src/test/java/com/baeldung/map/LimitMaxSizeHashMapByCustomHashMapUnitTest.java @@ -0,0 +1,43 @@ +package com.baeldung.map; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import java.util.LinkedHashMap; +import java.util.Map; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class LimitMaxSizeHashMapByCustomHashMapUnitTest { + + private final int MAX_SIZE = 4; + private LinkedHashMap linkedHashMap; + + @BeforeEach + void setUp() { + linkedHashMap = new LinkedHashMap() { + private static final long serialVersionUID = 1L; + + protected boolean removeEldestEntry(Map.Entry eldest) { + return size() > MAX_SIZE; + } + }; + linkedHashMap.put(1, "One"); + linkedHashMap.put(2, "Two"); + linkedHashMap.put(3, "Three"); + linkedHashMap.put(4, "Four"); + } + + @Test + void givenLinkedHashMapObject_whenAddingNewEntry_thenEldestEntryIsRemoved() { + linkedHashMap.put(5, "Five"); + String[] expectedArrayAfterFive = { "Two", "Three", "Four", "Five" }; + assertArrayEquals(expectedArrayAfterFive, linkedHashMap.values() + .toArray()); + linkedHashMap.put(6, "Six"); + String[] expectedArrayAfterSix = { "Three", "Four", "Five", "Six" }; + assertArrayEquals(expectedArrayAfterSix, linkedHashMap.values() + .toArray()); + } + +} diff --git a/core-java-modules/core-java-collections-maps-7/src/test/java/com/baeldung/map/LimitMaxSizeHashMapByLinkedHashMapUnitTest.java b/core-java-modules/core-java-collections-maps-7/src/test/java/com/baeldung/map/LimitMaxSizeHashMapByLinkedHashMapUnitTest.java new file mode 100644 index 0000000000..20fcd94011 --- /dev/null +++ b/core-java-modules/core-java-collections-maps-7/src/test/java/com/baeldung/map/LimitMaxSizeHashMapByLinkedHashMapUnitTest.java @@ -0,0 +1,35 @@ +package com.baeldung.map; + +import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class LimitMaxSizeHashMapByLinkedHashMapUnitTest { + + private final int MAX_SIZE = 4; + private HashMapWithMaxSizeLimit hashMapWithMaxSizeLimit; + + @BeforeEach + void setUp() { + hashMapWithMaxSizeLimit = new HashMapWithMaxSizeLimit(MAX_SIZE); + } + + @Test + void givenCustomHashMapObject_whenAddingNewEntryAndLimitExceeded_thenThrowsException() { + Exception exception = assertThrows(Exception.class, () -> { + hashMapWithMaxSizeLimit.putWithLimit(1, "One"); + hashMapWithMaxSizeLimit.putWithLimit(2, "Two"); + hashMapWithMaxSizeLimit.putWithLimit(3, "Three"); + hashMapWithMaxSizeLimit.putWithLimit(4, "Four"); + hashMapWithMaxSizeLimit.putWithLimit(5, "Five"); + }); + + String messageThrownWhenSizeExceedsLimit = "Max size exceeded!"; + String actualMessage = exception.getMessage(); + + assertTrue(actualMessage.equals(messageThrownWhenSizeExceedsLimit)); + } + +}