Java之——刪除ArrayList中的反覆元素的2種方法

來源:互聯網
上載者:User

標籤:target   結果   sys   jsp   art   rgs   printing   net   排列   

轉載請註明出處:http://blog.csdn.net/l1028386804/article/details/47414935

ArrayList是Java中最經常使用的集合類型之中的一個。它同意靈活加入多個null元素,反覆的元素,並保持元素的插入順序。在編碼時我們經常會遇 到那種必須從已建成的ArrayList中刪除反覆元素的要求。這篇文章將給出兩種從ArrayList中刪除反覆元素的方法。

方法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 ArrayListArrayList<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 listWithDuplicateElementsSystem.out.print("ArrayList With Duplicate Elements :");System.out.println(listWithDuplicateElements);//Constructing HashSet using listWithDuplicateElementsHashSet<String> set = new HashSet<String>(listWithDuplicateElements);//Constructing listWithoutDuplicateElements using setArrayList<String> listWithoutDuplicateElements = new ArrayList<String>(set);//Printing listWithoutDuplicateElementsSystem.out.print("ArrayList After Removing Duplicate Elements :");System.out.println(listWithoutDuplicateElements);    }}

輸出:

ArrayList With Duplicate Elements :[JAVA, J2EE, JSP, SERVLETS, JAVA, STRUTS, JSP]
ArrayList After Removing Duplicate Elements :[JAVA, SERVLETS, JSP, J2EE, STRUTS]

注意輸出結果。

你會發現,在刪除反覆元素之後,元素又一次洗牌。不再依照插入順序排列。假設你想在刪除反覆的元素之後依舊保持元素的插入順序,那麼不 建議使用此方法。還有還有一種方法,能夠保證在刪除反覆的元素之後也不改變元素的插入順序。那就是使用LinkedHashSet。

方法2:使用LinkedHashSet刪除ArrayList中反覆的元素

在該方法中。我們使用LinkedHashSet刪除ArrayList中反覆的元素。正如你知道的,LinkedHashSet不同意反覆元素, 同一時候保持元素的插入順序。LinkedHashSet的這兩個屬效能夠確保在刪除ArrayList中的反覆元素之後。依舊保持元素的插入順序。

參見以下 的範例。

import java.util.ArrayList;import java.util.LinkedHashSet;public class MainClass{    public static void main(String[] args)    {        //Constructing An ArrayListArrayList<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 listWithDuplicateElementsSystem.out.print("ArrayList With Duplicate Elements :");System.out.println(listWithDuplicateElements);//Constructing LinkedHashSet using listWithDuplicateElementsLinkedHashSet<String> set = new LinkedHashSet<String>(listWithDuplicateElements);//Constructing listWithoutDuplicateElements using setArrayList<String> listWithoutDuplicateElements = new ArrayList<String>(set);//Printing listWithoutDuplicateElementsSystem.out.print("ArrayList After Removing Duplicate Elements :");System.out.println(listWithoutDuplicateElements);    }}

輸出:

ArrayList With Duplicate Elements :[JAVA, J2EE, JSP, SERVLETS, JAVA, STRUTS, JSP]
ArrayList After Removing Duplicate Elements :[JAVA, J2EE, JSP, SERVLETS, STRUTS]

注意輸出。你能夠發如今刪除ArrayList中的反覆元素後。依舊保持了元素的插入順序。



Java之——刪除ArrayList中的反覆元素的2種方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.