fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main(String[] args) {
  11.  
  12. List<String> firstList = new ArrayList<String>();
  13. firstList.add("");
  14.  
  15. List<String> secondList = new ArrayList<String>();
  16. secondList.add("OP1004");
  17.  
  18. List<String> thirdList = new ArrayList<String>();
  19. thirdList.add("OP1003");
  20.  
  21. List<String> fourthList = new ArrayList<String>();
  22. fourthList.add("OP1006");
  23. fourthList.add("OP1044");
  24. fourthList.add("OP1046");
  25.  
  26. List<String> fifthList = new ArrayList<String>();
  27. fifthList.add("OP1008");
  28. fifthList.add("OP1009");
  29. fifthList.add("OP1044");
  30. fifthList.add("OP1005");
  31. fifthList.add("OP1004");
  32.  
  33. Map<String, List<String>> map = new LinkedHashMap<String, List<String>>();
  34. map.put("OP1004", firstList);
  35. map.put("OP1006", secondList);
  36. map.put("OP1005", thirdList);
  37. map.put("OP1009", fourthList);
  38. map.put("OP1016", fifthList);
  39.  
  40. for (Map.Entry<String, List<String>> keyAndValue: map.entrySet()) {
  41. String key = keyAndValue.getKey();
  42. List<String> values = keyAndValue.getValue();
  43.  
  44. if (values.isEmpty() || (values.size() < 2))
  45. continue;
  46.  
  47. for (Map.Entry<String, List<String>> mapKeyAndValue: map.entrySet()) {
  48.  
  49. String key1 = mapKeyAndValue.getKey();
  50. if (key.equals(key1))
  51. continue;
  52.  
  53. List<String> values2 = mapKeyAndValue.getValue();
  54. if (values2.isEmpty() || (values2.size() < 2))
  55. continue;
  56.  
  57. values2.removeAll(values);
  58. }
  59. }
  60.  
  61. for (Map.Entry<String, List<String>> keyAndValue: map.entrySet()) {
  62. System.out.println("Key is " + keyAndValue.getKey() + " Value are " + keyAndValue.getValue());
  63. }
  64. }
  65. }
Success #stdin #stdout 0.05s 711168KB
stdin
Standard input is empty
stdout
Key is OP1004 Value are []
Key is OP1006 Value are [OP1004]
Key is OP1005 Value are [OP1003]
Key is OP1009 Value are [OP1006, OP1044, OP1046]
Key is OP1016 Value are [OP1008, OP1009, OP1005, OP1004]