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. `

3
  • 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. Commented Jan 23, 2023 at 13:54
  • Actually the duplicate cited will solve your issue. Check out this answer. Starting with the solution and not the data either change the Collection type from Object to List<Integer> 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. Commented Jan 23, 2023 at 14:38
  • Correction. ...the remaining value may be associated with a different key than what you want. Commented Jan 23, 2023 at 15:18

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.