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 index 4d2d6e8d53..11845e6aa0 100644 --- 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 @@ -7,19 +7,21 @@ 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!"); + @Override + public V put(K key, V value) { + V res = null; + if (this.maxSize == -1 || this.size() < this.maxSize) { + res = super.put(key, value); + } else if (this.maxSize != -1) { + throw new RuntimeException("Max size exceeded!"); } - return this.put(key, value); + return res; } } 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 index 5be9c61245..f351bcc3cc 100644 --- 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 @@ -1,8 +1,9 @@ package com.baeldung.map; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertThrows; -import java.util.LinkedHashMap; +import java.util.HashMap; import java.util.Map; import org.junit.jupiter.api.BeforeEach; @@ -11,33 +12,27 @@ import org.junit.jupiter.api.Test; class LimitMaxSizeHashMapByCustomHashMapUnitTest { private final int MAX_SIZE = 4; - private LinkedHashMap linkedHashMap; + private HashMapWithMaxSizeLimit hashMapWithMaxSizeLimit; @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"); + hashMapWithMaxSizeLimit = new HashMapWithMaxSizeLimit(MAX_SIZE); } @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()); + void givenCustomHashMapObject_whenAddingNewEntryAndLimitExceeded_thenThrowsException() { + Exception exception = assertThrows(RuntimeException.class, () -> { + hashMapWithMaxSizeLimit.put(1, "One"); + hashMapWithMaxSizeLimit.put(2, "Two"); + hashMapWithMaxSizeLimit.put(3, "Three"); + hashMapWithMaxSizeLimit.put(4, "Four"); + hashMapWithMaxSizeLimit.put(5, "Five"); + }); + + String messageThrownWhenSizeExceedsLimit = "Max size exceeded!"; + String actualMessage = exception.getMessage(); + + assertTrue(actualMessage.equals(messageThrownWhenSizeExceedsLimit)); } } 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 index 20fcd94011..0986f426d9 100644 --- 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 @@ -1,7 +1,9 @@ package com.baeldung.map; -import static org.junit.Assert.assertTrue; -import static org.junit.jupiter.api.Assertions.assertThrows; +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; @@ -9,27 +11,33 @@ import org.junit.jupiter.api.Test; class LimitMaxSizeHashMapByLinkedHashMapUnitTest { private final int MAX_SIZE = 4; - private HashMapWithMaxSizeLimit hashMapWithMaxSizeLimit; + private LinkedHashMap linkedHashMap; @BeforeEach void setUp() { - hashMapWithMaxSizeLimit = new HashMapWithMaxSizeLimit(MAX_SIZE); + 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 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)); + 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()); } }