BAEL-5163: HashMap - keySet vs entrySet vs values methods (#11301)

* Hexagonal Architecture in Java

* Refactor Hexagonal Architecture in Java

* Refactor Hexagonal Architecture in Java

* BAEL-5163: HashMap - keySet vs entrySet vs values methods

* BAEL-5163: HashMap - keySet vs entrySet vs values methods

* Revert "Hexagonal Architecture in Java"

This reverts commit 1add21c1

* BAEL-5163: HashMap - keySet vs entrySet vs values methods

* BAEL-5163: HashMap - keySet vs entrySet vs values methods"
This commit is contained in:
Yashasvii 2021-10-25 18:15:25 +05:45 committed by GitHub
parent 24864eb370
commit 6835bf200d
3 changed files with 84 additions and 0 deletions

View File

@ -0,0 +1,30 @@
package com.baeldung.map.keysetValuesEntrySet;
import org.junit.Test;
import java.util.AbstractMap.SimpleEntry;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class EntrySetExampleUnitTest {
@Test
public void givenHashMap_whenEntrySetApplied_thenShouldReturnSetOfEntries() {
Map<String, Integer> map = new HashMap<>();
map.put("one", 1);
map.put("two", 2);
Set<Map.Entry<String, Integer>> actualValues = map.entrySet();
assertEquals(2, actualValues.size());
assertTrue(actualValues.contains(new SimpleEntry<>("one", 1)));
assertTrue(actualValues.contains(new SimpleEntry<>("two", 2)));
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.map.keysetValuesEntrySet;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class KeySetExampleUnitTest {
@Test
public void givenHashMap_whenKeySetApplied_thenShouldReturnSetOfKeys() {
Map<String, Integer> map = new HashMap<>();
map.put("one", 1);
map.put("two", 2);
Set<String> actualValues = map.keySet();
assertEquals(2, actualValues.size());
assertTrue(actualValues.contains("one"));
assertTrue(actualValues.contains("two"));
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.map.keysetValuesEntrySet;
import org.junit.Test;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class ValuesExampleUnitTest {
@Test
public void givenHashMap_whenValuesApplied_thenShouldReturnCollectionOfValues() {
Map<String, Integer> map = new HashMap<>();
map.put("one", 1);
map.put("two", 2);
Collection<Integer> actualValues = map.values();
assertEquals(2, actualValues.size());
assertTrue(actualValues.contains(1));
assertTrue(actualValues.contains(2));
}
}