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