I have a list of list. There is a list within a list.
[[-1, 0, 1], [-1, 2, -1], [0, 1, -1]]
, name of this list say result. result list contain duplicate elements as a list. [-1,0,1]
and [0,1,-1]
they are same. I want to make a list which does not contain duplicates. So list result become [[-1,0,1],[-1,2,-1]]
or [[-1,2,-1],[0,1,-1]]
.
I read that Hashmap can not store duplicate keys but allow duplicate values. So to remove duplicates I was trying Hashmap.
But after writing the code it run well there is no error.
HashMap<List<Integer>,Integer> final_sol=new HashMap<>();
for(int k1=0;k1<result.size();k1++){
final_sol.put(result.get(k1),k1);
}
System.out.println(final_sol);
Output:
{[-1, 2, -1]=1, [0, 1, -1]=2, [-1, 0, 1]=0}
After writing this code blocks I thought my duplicate keys cannot shown only unique keys displayed.
Then how could I make this list unique using Hash map?Fail to understand
When I was using tree map it doesn't compile and gave som eerror.