|
| 1 | +package com.howtodoinjava.core.collections.map; |
| 2 | + |
| 3 | +import com.google.common.collect.BiMap; |
| 4 | +import com.google.common.collect.HashBiMap; |
| 5 | +import com.google.common.collect.ImmutableBiMap; |
| 6 | +import com.google.common.collect.ImmutableMultimap; |
| 7 | +import com.google.common.collect.Multimap; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.Map.Entry; |
| 13 | +import java.util.stream.Collectors; |
| 14 | +import org.apache.commons.collections4.BidiMap; |
| 15 | +import org.apache.commons.collections4.MapUtils; |
| 16 | +import org.apache.commons.collections4.bidimap.DualHashBidiMap; |
| 17 | + |
| 18 | +public class InvertedMap { |
| 19 | + |
| 20 | + public static void main(String[] args) { |
| 21 | + Map<String, String> map = Map.of("key1", "value1", "key2", "value2"); |
| 22 | + System.out.println(map); |
| 23 | + |
| 24 | + Map<String, Integer> originalMap = new HashMap<>(); |
| 25 | + originalMap.put("Key1", 1); |
| 26 | + originalMap.put("Key2", 2); |
| 27 | + |
| 28 | + Map<Integer, String> invertedMap = new HashMap<>(); |
| 29 | + for (Entry<String, Integer> entry : originalMap.entrySet()) { |
| 30 | + invertedMap.put(entry.getValue(), entry.getKey()); |
| 31 | + } |
| 32 | + System.out.println(invertedMap); //{1=Key1, 2=Key2} |
| 33 | + |
| 34 | + Map<Integer, String> inverseMap2 = originalMap.entrySet() |
| 35 | + .stream() |
| 36 | + .collect(Collectors.toMap(Entry::getValue, Entry::getKey)); |
| 37 | + |
| 38 | + Map<Integer, String> mapWithDuplicateValues = new HashMap<Integer, String>(); |
| 39 | + mapWithDuplicateValues.put(1, "Value1"); |
| 40 | + mapWithDuplicateValues.put(2, "Value2"); |
| 41 | + mapWithDuplicateValues.put(3, "Value2"); |
| 42 | + |
| 43 | + HashMap<String, List<Integer>> inverseMap = new HashMap<String, List<Integer>>(); |
| 44 | + |
| 45 | + for (Entry<Integer, String> entry : mapWithDuplicateValues.entrySet()) { |
| 46 | + if (inverseMap.containsKey(entry.getValue())) { |
| 47 | + inverseMap.get(entry.getValue()).add(entry.getKey()); |
| 48 | + } else { |
| 49 | + List<Integer> list = new ArrayList<Integer>(); |
| 50 | + list.add(entry.getKey()); |
| 51 | + inverseMap.put(entry.getValue(), list); |
| 52 | + } |
| 53 | + } |
| 54 | + System.out.println(inverseMap); |
| 55 | + |
| 56 | + /*inverseMap = originalMap.entrySet() |
| 57 | + .stream() |
| 58 | + .collect(Collectors.groupingBy(Entry::getValue, |
| 59 | + Collectors.mapping(Entry::getKey, Collectors.toList()))); |
| 60 | +
|
| 61 | + System.out.println(inverseMap);*/ |
| 62 | + |
| 63 | + BiMap<Integer, String> biMap = ImmutableBiMap.of(1, "Key1", 2, "Key2", 3, "Key3"); |
| 64 | + BiMap<String, Integer> inverseBiMap = biMap.inverse(); |
| 65 | + |
| 66 | + System.out.println(inverseBiMap); //{Key1=1, Key2=2, Key3=3} |
| 67 | + |
| 68 | + // |
| 69 | + biMap = HashBiMap.create(); |
| 70 | + BiMap<String, Integer> inversedMap = biMap.inverse(); |
| 71 | + System.out.println(inversedMap); //{Key1=1, Key2=2, Key3=3} |
| 72 | + |
| 73 | + Multimap<Integer, String> multimap = ImmutableMultimap.of(1, "Key1", 1, "Key2", 2, "Key3"); |
| 74 | + System.out.println(multimap); //{1=[Key1, Key2], 2=[Key3]} |
| 75 | + |
| 76 | + BidiMap bidiMap = new DualHashBidiMap(); |
| 77 | + bidiMap.put(1, "Value1"); |
| 78 | + bidiMap.put(2, "Value2"); |
| 79 | + |
| 80 | + System.out.println(bidiMap.inverseBidiMap()); //{Value1=1, Value2=2} |
| 81 | + |
| 82 | + Map<Integer, String> hashMap = new HashMap<Integer, String>(); |
| 83 | + hashMap.put(1,"Value1"); |
| 84 | + hashMap.put(2,"Value2"); |
| 85 | + hashMap.put(3,"Value2"); |
| 86 | + |
| 87 | + Map<String,Integer> inversedMap1 = MapUtils.invertMap(hashMap); |
| 88 | + System.out.println(inversedMap1); //{Value1=1, Value2=3} |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | +} |
0 commit comments