2种Java删除ArrayList中的重复元素的方法
这篇文章将给出两种从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中的重复元素后,依然保持了元素的插入顺序。
以上就是本文的全部内容,希望对大家的学习有所帮助。
更多2种Java删除ArrayList中的重复元素的方法相关文章请关注PHP中文网!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

公司安全软件导致部分应用无法正常运行的排查与解决方法许多公司为了保障内部网络安全,会部署安全软件。...

将姓名转换为数字以实现排序的解决方案在许多应用场景中,用户可能需要在群组中进行排序,尤其是在一个用...

系统对接中的字段映射处理在进行系统对接时,常常会遇到一个棘手的问题:如何将A系统的接口字段有效地映�...

在使用MyBatis-Plus或其他ORM框架进行数据库操作时,经常需要根据实体类的属性名构造查询条件。如果每次都手动...

在使用IntelliJIDEAUltimate版本启动Spring...

Java对象与数组的转换:深入探讨强制类型转换的风险与正确方法很多Java初学者会遇到将一个对象转换成数组的�...

电商平台SKU和SPU表设计详解本文将探讨电商平台中SKU和SPU的数据库设计问题,特别是如何处理用户自定义销售属...

Redis缓存方案如何实现产品排行榜列表的需求?在开发过程中,我们常常需要处理排行榜的需求,例如展示一个�...
