BAEL-5348: Invert a Map in Java + tests
This commit is contained in:
parent
ad893be8ac
commit
3dd2802e2d
|
@ -0,0 +1,59 @@
|
|||
package com.baeldung.map.invert;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class InvertHashMapExample {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Map<String, Integer> map = new HashMap<>();
|
||||
map.put("first", 1);
|
||||
map.put("second", 2);
|
||||
System.out.println(map);
|
||||
|
||||
invertMapUsingForLoop(map);
|
||||
invertMapUsingStreams(map);
|
||||
invertMapUsingMapper(map);
|
||||
|
||||
map.put("two", 2);
|
||||
invertMapUsingGroupingBy(map);
|
||||
}
|
||||
|
||||
public static <V, K> Map<V, K> invertMapUsingForLoop(Map<K, V> map) {
|
||||
Map<V, K> inversedMap = new HashMap<V, K>();
|
||||
for (Entry<K, V> entry : map.entrySet()) {
|
||||
inversedMap.put(entry.getValue(), entry.getKey());
|
||||
}
|
||||
System.out.println(inversedMap);
|
||||
return inversedMap;
|
||||
}
|
||||
|
||||
public static <V, K> Map<V, K> invertMapUsingStreams(Map<K, V> map) {
|
||||
Map<V, K> inversedMap = map.entrySet()
|
||||
.stream()
|
||||
.collect(Collectors.toMap(Entry::getValue, Entry::getKey));
|
||||
System.out.println(inversedMap);
|
||||
return inversedMap;
|
||||
}
|
||||
|
||||
public static <K, V> Map<V, K> invertMapUsingMapper(Map<K, V> sourceMap) {
|
||||
Map<V, K> inversedMap = sourceMap.entrySet()
|
||||
.stream()
|
||||
.collect(Collectors.toMap(Entry::getValue, Entry::getKey, (oldValue, newValue) -> oldValue));
|
||||
System.out.println(inversedMap);
|
||||
return inversedMap;
|
||||
}
|
||||
|
||||
public static <V, K> Map<V, List<K>> invertMapUsingGroupingBy(Map<K, V> map) {
|
||||
Map<V, List<K>> inversedMap = map.entrySet()
|
||||
.stream()
|
||||
.collect(Collectors.groupingBy(Map.Entry::getValue, Collectors.mapping(Map.Entry::getKey, Collectors.toList())));
|
||||
System.out.println(inversedMap);
|
||||
return inversedMap;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
package com.baeldung.map.invert;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
|
||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||
public class InvertHashMapExampleTest {
|
||||
|
||||
Map<String, Integer> sourceMap;
|
||||
|
||||
@BeforeAll
|
||||
void setup() {
|
||||
sourceMap = new HashMap<>();
|
||||
sourceMap.put("Sunday", 0);
|
||||
sourceMap.put("Monday", 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void test1_invertMapUsingForLoop() {
|
||||
Map<Integer, String> inversedMap = InvertHashMapExample.invertMapUsingForLoop(sourceMap);
|
||||
assertNotNull(inversedMap);
|
||||
assertEquals(inversedMap.size(), 2);
|
||||
assertEquals("Monday", inversedMap.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void test2_invertMapUsingStreams() {
|
||||
Map<Integer, String> inversedMap = InvertHashMapExample.invertMapUsingStreams(sourceMap);
|
||||
assertNotNull(inversedMap);
|
||||
assertEquals(inversedMap.size(), 2);
|
||||
assertEquals("Monday", inversedMap.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void test3_invertMapUsingMapper() {
|
||||
Map<Integer, String> inversedMap = InvertHashMapExample.invertMapUsingMapper(sourceMap);
|
||||
assertNotNull(inversedMap);
|
||||
assertEquals(inversedMap.size(), 2);
|
||||
assertEquals("Monday", inversedMap.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void test4_invertMapUsingGroupingBy() {
|
||||
sourceMap.put("monday", 1);
|
||||
Map<Integer, List<String>> inversedMap = InvertHashMapExample.invertMapUsingGroupingBy(sourceMap);
|
||||
assertNotNull(inversedMap);
|
||||
assertEquals(inversedMap.size(), 2);
|
||||
assertTrue(inversedMap.get(1) instanceof List);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue