2種Java刪除ArrayList中的重復(fù)元素的方法
這篇文章將給出兩種從ArrayList中刪除重復(fù)元素的方法,分別是使用HashSet和LinkedHashSet。
ArrayList是Java中最常用的集合類型之一。它允許靈活添加多個(gè)null元素,重復(fù)的元素,并保持元素的插入順序。在編碼時(shí)我們經(jīng)常會(huì)遇到那種必須從已建成的ArrayList中刪除重復(fù)元素的要求。
方法1:使用HashSet刪除ArrayList中重復(fù)的元素
在該方法中,我們使用HashSet來刪除重復(fù)的元素。如你所知,HashSet不允許有重復(fù)的元素。我們使用HashSet的這個(gè)屬性來刪除已建 成的ArrayList中的重復(fù)元素。但是,這種方法有一個(gè)缺點(diǎn)。那就是,它會(huì)刪除ArrayList中元素的插入順序。這意味著,刪除重復(fù)的元素后,元 素的插入順序就不對(duì)了。先來看下面這個(gè)例子。
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]
注意輸出結(jié)果。你會(huì)發(fā)現(xiàn),在刪除重復(fù)元素之后,元素重新洗牌。不再按照插入順序排列。如果你想在刪除重復(fù)的元素之后依然保持元素的插入順序,那么不 建議使用此方法。還有另一種方法,可以保證在刪除重復(fù)的元素之后也不改變?cè)氐牟迦腠樞?。那就是使用LinkedHashSet。
方法2:使用LinkedHashSet刪除ArrayList中重復(fù)的元素
在該方法中,我們使用LinkedHashSet刪除ArrayList中重復(fù)的元素。正如你知道的,LinkedHashSet不允許重復(fù)元素, 同時(shí)保持元素的插入順序。LinkedHashSet的這兩個(gè)屬性可以確保在刪除ArrayList中的重復(fù)元素之后,依然保持元素的插入順序。參見下面的例子。
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]
注意輸出。你可以發(fā)現(xiàn)在刪除ArrayList中的重復(fù)元素后,依然保持了元素的插入順序。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
Spring實(shí)現(xiàn)自定義注解處理器解析和處理注解
這篇文章主要介紹了Spring實(shí)現(xiàn)自定義注解處理器解析和處理注解,注解在現(xiàn)代Java編程中扮演了至關(guān)重要的角色,無論是簡(jiǎn)化代碼、增強(qiáng)可讀性,還是將元數(shù)據(jù)與業(yè)務(wù)邏輯分離,注解都讓我們的代碼更加優(yōu)雅和靈活,需要的朋友可以參考下2024-10-10關(guān)于Java中修飾符的總結(jié)(fina除外)
下面小編就為大家?guī)硪黄P(guān)于Java中修飾符的總結(jié)(fina除外)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09一文詳解SpringBoot中CommandLineRunner接口
Spring Boot的CommandLineRunner接口是一個(gè)函數(shù)式接口,用于在Spring Boot應(yīng)用程序啟動(dòng)后執(zhí)行一些初始化操作,它提供了一個(gè)run方法,該方法在應(yīng)用程序啟動(dòng)后被調(diào)用,本文給大家詳細(xì)介紹了SpringBoot中CommandLineRunner接口,需要的朋友可以參考下2023-10-10springboot實(shí)現(xiàn)讀取nacos配置文件
這篇文章主要介紹了springboot實(shí)現(xiàn)讀取nacos配置文件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09@JsonSerialize(using = LongToStringUtil.class)注解的使
這篇文章主要介紹了@JsonSerialize(using = LongToStringUtil.class)注解的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08SpringBoot通過@Scheduled實(shí)現(xiàn)定時(shí)任務(wù)及單線程運(yùn)行問題解決
Scheduled定時(shí)任務(wù)是Spring boot自身提供的功能,所以不需要引入Maven依賴包,下面這篇文章主要給大家介紹了關(guān)于SpringBoot通過@Scheduled實(shí)現(xiàn)定時(shí)任務(wù)以及問題解決的相關(guān)資料,需要的朋友可以參考下2023-02-02