Method One: Loop element deletion
Delete duplicate elements in ArrayList
public static void RemoveDuplicate1 (List list) {
for (int i = 0; i < list.size ()-1; i + +) {
for (int j = list.size ()-1; j > i; J--) {
if (List.get (j). Equals (List.get (i))) {
List.remove (j);
}
}
}
SYSTEM.OUT.PRINTLN (list);
}
Java removes list duplicate values
Two ways, one without order, and one to maintain the order of elements in the original list.
Hastset according to Hashcode, the data is not duplicated.
public static void Removeduplicate (ArrayList arllist)
{
HashSet h = new HashSet (arllist);
Arllist.clear ();
Arllist.addall (h);
}
Use the Add method of HashSet to determine if the same data has been added, and if the same data already exists, do not add
public static void Removeduplicatewithorder (ArrayList arllist)
{
Set set = new HashSet ();
List NewList = new ArrayList ();
for (Iterator iter = Arllist.iterator (); Iter.hasnext ();)
{
Object element = Iter.next ();
if (Set.add (Element)) Newlist.add (element);
}
Arllist.clear ();
Arllist.addall (NewList);
}
List Delete repeating elements