I have written the following code and i am getting all the duplicates even i have used set.
private void validatingpswwithpattern(String password) throws IOException
{
List<String[]> list=new ArrayList<String[]>();
list.add(new String[]{"raj","duvva","sathish"});
list.add(new String[]{"raj","duvva","sathish"});
list.add(new String[]{"raj","duvva","sathish"});
list.add(new String[]{"raj","duvva","sathish"});
list.add(new String[]{"raj","duvva","sathish"});
list.add(new String[]{"raj","duvva","sathish"});
Set<String[]> hs = new HashSet<String[]>();
hs.addAll(list);
list.clear();
list.addAll(hs);
System.out.println(list);
}
Since arrays don't override the default implementation of hashCode and equals from Object class, HashSet is useless for eliminating duplicates.
You could use a TreeSet and supply a Comparator<String[]> that would determine when two String arrays are equal.
TreeSet<String[]> set = new TreeSet<> (new Comparator<String[]>() {
public int compare(String[] o1, String[] o2)
{
// write here logic to determine whether o1<o2 (return -1) or
// o1>o2 (return 1) or
// o1 is equal to o2 (return 0)
}
});
set.addAll(list);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With