0

I need to delete all duplicates from my HashMap but those duplicates are the lists inside the HashMap.

For example:



      HashMap<Integer, ArrayList<Intteger>> myList = new HashMap<>();

      myList.put(0, list0);
      myList.put(1, list1);
      myList.put(2, list2);
      myList.put(3, list3);
      myList.put(4, list4);


Now when I display all elements of the HashMap I've got:

  0 [1,3]
   1 [0,3]
   2 [1,3]
   3 [1,4]
   4 [2,4]

How can I remove array's duplicate? As you can see in this case elements with key 0 and 2 have the same values so I want to remove the duplicate. `

azro
  • 53,056
  • 7
  • 34
  • 70
  • Why my question is closed? This case describe totally different situation than in the question you have attached here. I want to delete duplicates of ArrayList inside HashMap and unfortunately your question doesn't help me in this situation. – Karlz-Bandz Jan 23 '23 at 13:54
  • Actually the duplicate cited will solve your issue. Check out [this answer](http://stackoverflow.com/a/26135521/1552534). Starting with the solution and not the data either change the Collection type from `Object` to `List` or from `Object` to `? extends Object`. It will delete the duplicate values from your map. But remember that the keys are unordered so the remaining value may be a different key than what you want. – WJS Jan 23 '23 at 14:38
  • Correction. ...the remaining value may be *`associated with`* a different key than what you want. – WJS Jan 23 '23 at 15:18

0 Answers0