fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.util.stream.*;
  5. import java.lang.*;
  6. import java.io.*;
  7.  
  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. List<String> list = Stream.of(
  14. "06|20|1",
  15. "11|20|2",
  16. "11|20|2",
  17. "07|207|6",
  18. "11|207|2",
  19. "07|207|6"
  20. ).collect(Collectors.toList());
  21. HashMap<String, HashMap<String, String>> hashMap = new HashMap<>();
  22. HashMap<String, String> newhas = new HashMap<>();
  23. for (String line : list) {
  24. String key, value, priority;
  25. key = line.split("\\|", -1)[1];
  26. value = line.split("\\|", -1)[0];
  27. priority = line.split("\\|", -1)[2];
  28.  
  29. if (hashMap.containsKey(key)) {
  30. HashMap<String, String> getPriority = hashMap.get(key);
  31. Map.Entry<String, String> entry = getPriority.entrySet().iterator().next();
  32. String oldKey = entry.getKey();
  33. String previousPrior = getPriority.get(oldKey);
  34. if (Integer.parseInt(priority) > Integer.parseInt(previousPrior)) {
  35. getPriority.remove(oldKey);
  36. getPriority.put(value,priority);
  37. hashMap.put(key, getPriority);
  38. }
  39. } else {
  40. newhas = new HashMap<>();
  41. newhas.put(value, priority);
  42. hashMap.put(key, newhas);
  43. }
  44.  
  45. }
  46. System.out.println(hashMap);
  47. }
  48. }
Success #stdin #stdout 0.09s 48252KB
stdin
Standard input is empty
stdout
{207={07=6}, 20={11=2}}