BAEL-2178 (#5194)
* BAEL-2178 * revert previous commit, adding classes to collections project
This commit is contained in:
parent
57b6480e73
commit
fe96e112a5
|
@ -0,0 +1,44 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.java.map;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* @author swpraman
|
||||
*
|
||||
*/
|
||||
public class MapUtil {
|
||||
|
||||
public static <K, V> Stream<K> keys(Map<K, V> map, V value) {
|
||||
return map.entrySet()
|
||||
.stream()
|
||||
.filter(entry -> value.equals(entry.getValue()))
|
||||
.map(Map.Entry::getKey);
|
||||
}
|
||||
|
||||
public static <K, V> Set<K> getKeys(Map<K, V> map, V value) {
|
||||
Set<K> keys = new HashSet<>();
|
||||
for (Entry<K, V> entry : map.entrySet()) {
|
||||
if (entry.getValue().equals(value)) {
|
||||
keys.add(entry.getKey());
|
||||
}
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
||||
public static <K, V> K getKey(Map<K, V> map, V value) {
|
||||
for (Entry<K, V> entry : map.entrySet()) {
|
||||
if (entry.getValue().equals(value)) {
|
||||
return entry.getKey();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,104 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.java.map;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.commons.collections4.BidiMap;
|
||||
import org.apache.commons.collections4.bidimap.DualHashBidiMap;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.common.collect.HashBiMap;
|
||||
|
||||
/**
|
||||
* @author swpraman
|
||||
*
|
||||
*/
|
||||
public class MapUtilUnitTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void whenUsingImperativeWayForSingleKey_shouldReturnSingleKey() {
|
||||
Map<String, String> capitalCountryMap = new HashMap<>();
|
||||
capitalCountryMap.put("Tokyo", "Japan");
|
||||
capitalCountryMap.put("New Delhi", "India");
|
||||
assertEquals("New Delhi", MapUtil.getKey(capitalCountryMap, "India"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingImperativeWayForAllKeys_shouldReturnAllKeys() {
|
||||
Map<String, String> capitalCountryMap = new HashMap<>();
|
||||
capitalCountryMap.put("Tokyo", "Japan");
|
||||
capitalCountryMap.put("Berlin", "Germany");
|
||||
capitalCountryMap.put("Cape Town", "South Africa");
|
||||
capitalCountryMap.put("Pretoria", "South Africa");
|
||||
capitalCountryMap.put("Bloemfontein", "South Africa");
|
||||
|
||||
assertEquals(new HashSet<String>(Arrays.asList(
|
||||
new String[] {"Cape Town", "Pretoria", "Bloemfontein"})),
|
||||
MapUtil.getKeys(capitalCountryMap, "South Africa"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingFunctionalWayForSingleKey_shouldReturnSingleKey() {
|
||||
Map<String, String> capitalCountryMap = new HashMap<>();
|
||||
capitalCountryMap.put("Tokyo", "Japan");
|
||||
capitalCountryMap.put("Berlin", "Germany");
|
||||
assertEquals("Berlin", MapUtil.keys(capitalCountryMap, "Germany").findFirst().get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingFunctionalWayForAllKeys_shouldReturnAllKeys() {
|
||||
Map<String, String> capitalCountryMap = new HashMap<>();
|
||||
capitalCountryMap.put("Tokyo", "Japan");
|
||||
capitalCountryMap.put("Berlin", "Germany");
|
||||
capitalCountryMap.put("Cape Town", "South Africa");
|
||||
capitalCountryMap.put("Pretoria", "South Africa");
|
||||
capitalCountryMap.put("Bloemfontein", "South Africa");
|
||||
assertEquals(new HashSet<String>(Arrays.asList(
|
||||
new String[] {"Cape Town", "Pretoria", "Bloemfontein"})),
|
||||
MapUtil.keys(capitalCountryMap, "South Africa").collect(Collectors.toSet()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingBidiMap_shouldReturnKey() {
|
||||
BidiMap<String, String> capitalCountryMap = new DualHashBidiMap<String, String>();
|
||||
capitalCountryMap.put("Berlin", "Germany");
|
||||
capitalCountryMap.put("Cape Town", "South Africa");
|
||||
assertEquals("Berlin", capitalCountryMap.getKey("Germany"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingBidiMapAddDuplicateValue_shouldRemoveOldEntry() {
|
||||
BidiMap<String, String> capitalCountryMap = new DualHashBidiMap<String, String>();
|
||||
capitalCountryMap.put("Berlin", "Germany");
|
||||
capitalCountryMap.put("Cape Town", "South Africa");
|
||||
capitalCountryMap.put("Pretoria", "South Africa");
|
||||
assertEquals("Pretoria", capitalCountryMap.getKey("South Africa"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingBiMap_shouldReturnKey() {
|
||||
HashBiMap<String, String> capitalCountryMap = HashBiMap.create();
|
||||
capitalCountryMap.put("Berlin", "Germany");
|
||||
capitalCountryMap.put("Cape Town", "South Africa");
|
||||
assertEquals("Berlin", capitalCountryMap.inverse().get("Germany"));
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void whenUsingBiMapAddDuplicateValue_shouldThrowException() {
|
||||
HashBiMap<String, String> capitalCountryMap = HashBiMap.create();
|
||||
capitalCountryMap.put("Berlin", "Germany");
|
||||
capitalCountryMap.put("Cape Town", "South Africa");
|
||||
capitalCountryMap.put("Pretoria", "South Africa");
|
||||
assertEquals("Berlin", capitalCountryMap.inverse().get("Germany"));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue