Java删除ArrayList中的重复元素的2种方法java
ArrayList是Java中最经常使用的集合类型之一。它容许灵活添加多个null元素,重复的元素,并保持元素的插入顺序。在编码时咱们常常会遇到那种必须从已建成的ArrayList中删除重复元素的要求。这篇文章将给出两种从ArrayList中删除重复元素的方法。ui
方法1:使用HashSet删除ArrayList中重复的元素】
在该方法中,咱们使用HashSet来删除重复的元素。如你所知,HashSet不容许有重复的元素。咱们使用HashSet的这个属性来删除已建成的ArrayList中的重复元素。可是,这种方法有一个缺点。那就是,它会删除ArrayList中元素的插入顺序。这意味着,删除重复的元素后,元素的插入顺序就不对了。先来看下面这个例子。编码
import java.util.ArrayList;
import java.util.HashSet;
public class MainClass
{
public static void main(String[] args)
{
//Constructing An ArrayList
ArrayList<String> listWithDuplicateElements = new ArrayList<String>();
listWithDuplicateElements.add("JAVA");
listWithDuplicateElements.add("J2EE");
listWithDuplicateElements.add("JSP");
listWithDuplicateElements.add("SERVLETS");
listWithDuplicateElements.add("JAVA");
listWithDuplicateElements.add("STRUTS");
listWithDuplicateElements.add("JSP");
//Printing listWithDuplicateElements
System.out.print("ArrayList With Duplicate Elements :");
System.out.println(listWithDuplicateElements);
//Constructing HashSet using listWithDuplicateElements
HashSet<String> set = new HashSet<String>(listWithDuplicateElements);
//Constructing listWithoutDuplicateElements using set
ArrayList<String> listWithoutDuplicateElements = new ArrayList<String>(set);
//Printing listWithoutDuplicateElements
System.out.print("ArrayList After Removing Duplicate Elements :");
System.out.println(listWithoutDuplicateElements);
}
}
输出;spa
ArrayList With Duplicate Elements :[JAVA, J2EE, JSP, SERVLETS, JAVA, STRUTS, JSP]
ArrayList After Removing Duplicate Elements :[JAVA, SERVLETS, JSP, J2EE, STRUTS]
注意输出结果。你会发现,在删除重复元素以后,元素从新洗牌。再也不按照插入顺序排列。若是你想在删除重复的元素以后依然保持元素的插入顺序,那么不建议使用此方法。还有另外一种方法,能够保证在删除重复的元素以后也不改变元素的插入顺序。那就是使用LinkedHashSet。code
方法2:使用LinkedHashSet删除ArrayList中重复的元素ip
在该方法中,咱们使用LinkedHashSet删除ArrayList中重复的元素。正如你知道的,LinkedHashSet不容许重复元素,同时保持元素的插入顺序。LinkedHashSet的这两个属性能够确保在删除ArrayList中的重复元素以后,依然保持元素的插入顺序。参见下面的例子。string
import java.util.ArrayList;
import java.util.LinkedHashSet;
public class MainClass
{
public static void main(String[] args)
{
//Constructing An ArrayList
ArrayList<String> listWithDuplicateElements = new ArrayList<String>();
listWithDuplicateElements.add("JAVA");
listWithDuplicateElements.add("J2EE");
listWithDuplicateElements.add("JSP");
listWithDuplicateElements.add("SERVLETS");
listWithDuplicateElements.add("JAVA");
listWithDuplicateElements.add("STRUTS");
listWithDuplicateElements.add("JSP");
//Printing listWithDuplicateElements
System.out.print("ArrayList With Duplicate Elements :");
System.out.println(listWithDuplicateElements);
//Constructing LinkedHashSet using listWithDuplicateElements
LinkedHashSet<String> set = new LinkedHashSet<String>(listWithDuplicateElements);
//Constructing listWithoutDuplicateElements using set
ArrayList<String> listWithoutDuplicateElements = new ArrayList<String>(set);
//Printing listWithoutDuplicateElements
System.out.print("ArrayList After Removing Duplicate Elements :");
System.out.println(listWithoutDuplicateElements);
}
}
输出:it
ArrayList With Duplicate Elements :[JAVA, J2EE, JSP, SERVLETS, JAVA, STRUTS, JSP]
ArrayList After Removing Duplicate Elements :[JAVA, J2EE, JSP, SERVLETS, STRUTS]
注意输出。你能够发如今删除ArrayList中的重复元素后,依然保持了元素的插入顺序。io