import java.util.*;
public class HashMapConversion {
public static void main(String[] args){
HashMap<Integer, String> inputMap = new HashMap<>();
inputMap.put(1, "a");
inputMap.put(2, "b");
inputMap.put(3, "a");
inputMap.put(4, "k");
inputMap.put(5, "fg");
inputMap.put(7, "aa");
inputMap.put(9, "b");
inputMap.put(10, "vc");
HashMap<String, List<Integer>> outputMap= new HashMap<>();
for (Map.Entry<Integer, String> entry:inputMap.entrySet()) {
Integer key= entry.getKey();
String value= entry.getValue();
if (outputMap.containsKey(value)) {
outputMap.get(value).add(key);
}
else {
List<Integer> list = new ArrayList<>();
list.add(key);
outputMap.put(value, list);
}
}
HashMap<String, Integer[]> finalMap= new HashMap<>();
for (Map.Entry<String, List<Integer> > entry:outputMap.entrySet()) {
String key = entry.getKey();
List<Integer> values= entry.getValue();
finalMap.put(key,
values.toArray(new Integer[0]));
}
for (Map.Entry<String, Integer[]> entry:finalMap.entrySet()) {
System.out.print("<" + entry.getKey() + ",[");
Integer[] values = entry.getValue();
for (int i = 0; i < values.length; i++) {
System.out.print(values[i]);
if (i < values.length - 1) {
System.out.print(",");
}
}
System.out.println("]>");
}
}
}