国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

2種Java刪除ArrayList中的重復元素的方法

2019-11-26 15:01:46
字體:
來源:轉載
供稿:網友

這篇文章將給出兩種從ArrayList中刪除重復元素的方法,分別是使用HashSet和LinkedHashSet。

ArrayList是Java中最常用的集合類型之一。它允許靈活添加多個null元素,重復的元素,并保持元素的插入順序。在編碼時我們經常會遇到那種必須從已建成的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 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); }}

輸出:

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 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); }}

 輸出:

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

注意輸出。你可以發現在刪除ArrayList中的重復元素后,依然保持了元素的插入順序。

以上就是本文的全部內容,希望對大家的學習有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 英吉沙县| 新建县| 岳西县| 三都| 辉南县| 两当县| 渭南市| 姚安县| 海阳市| 全南县| 平塘县| 陵川县| 商南县| 安溪县| 海城市| 大兴区| 乐清市| 罗江县| 贵阳市| 宣城市| 台州市| 永兴县| 台北市| 乌海市| 宁津县| 西林县| 城固县| 腾冲县| 金沙县| 陆良县| 德昌县| 许昌市| 溆浦县| 铁岭市| 博爱县| 延川县| 晴隆县| 三原县| 庆阳市| 土默特左旗| 华亭县|