栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

2种Java删除ArrayList中的重复元素的方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 listWithDuplicateElements = new ArrayList();
 
 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 set = new HashSet(listWithDuplicateElements);
 
 //Constructing listWithoutDuplicateElements using set
 
 ArrayList listWithoutDuplicateElements = new ArrayList(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 listWithDuplicateElements = new ArrayList();
 
 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 set = new linkedHashSet(listWithDuplicateElements);
 
 //Constructing listWithoutDuplicateElements using set
 
 ArrayList listWithoutDuplicateElements = new ArrayList(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中的重复元素后,依然保持了元素的插入顺序。

以上就是本文的全部内容,希望对大家的学习有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/151306.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号