fork(2) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import static java.util.stream.Collectors.*;
  7. import java.util.List;
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. public static void main (String[] args) throws java.lang.Exception
  12. {
  13. Map<String, List<Integer>> mapOfIntList = new HashMap<String, List<Integer>>();
  14.  
  15. mapOfIntList.put("UNIT", Arrays.asList(1, 2, 3, 8, 7, 0, 8, 6));
  16. mapOfIntList.put("TEN", Arrays.asList(24, 90, 63, 87));
  17. mapOfIntList.put("HUNDRED", Arrays.asList(645, 457, 306, 762));
  18. mapOfIntList.put("THOUSAND", Arrays.asList(1234, 3456, 5340, 9876));
  19.  
  20.  
  21. Map<Integer, String> collect = mapOfIntList.entrySet()
  22. .stream()
  23. .flatMap(e -> e.getValue().stream().map(s -> new AbstractMap.SimpleEntry<>(s, e.getKey())))
  24. .collect(toMap(AbstractMap.SimpleEntry::getKey,
  25. AbstractMap.SimpleEntry::getValue,
  26. (l, r) -> l));
  27.  
  28. System.out.println(collect);
  29. }
  30. }
Success #stdin #stdout 0.15s 2184192KB
stdin
Standard input is empty
stdout
{3456=THOUSAND, 0=UNIT, 1=UNIT, 2=UNIT, 3=UNIT, 645=HUNDRED, 6=UNIT, 7=UNIT, 8=UNIT, 457=HUNDRED, 306=HUNDRED, 1234=THOUSAND, 9876=THOUSAND, 87=TEN, 24=TEN, 90=TEN, 762=HUNDRED, 5340=THOUSAND, 63=TEN}