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:
parent
24864eb370
commit
6835bf200d
|
@ -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)));
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -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"));
|
||||
}
|
||||
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue