在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

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

2017-2-9 13:05| 发布者: zhangjf| 查看: 540| 评论: 0

摘要: ArrayList是Java中最常用的集合类型之一。它允许灵活添加多个null元素,重复的元素,并保持元素的插入顺序。在编码时我们经常会遇 到那种必须从已建成的ArrayList中删除重复元素的要求。这篇文章将给出两种从ArrayLi ...

ArrayList是Java中最常用的集合类型之一。它允许灵活添加多个null元素,重复的元素,并保持元素的插入顺序。在编码时我们经常会遇 到那种必须从已建成的ArrayList中删除重复元素的要求。这篇文章将给出两种从ArrayList中删除重复元素的方法。

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

方法1:使用HashSet删除ArrayList中重复的元素

在该方法中,我们使用HashSet来删除重复的元素。如你所知,HashSet不允许有重复的元素。我们使用HashSet的这个属性来删除已建 成的ArrayList中的重复元素。但是,这种方法有一个缺点。那就是,它会删除ArrayList中元素的插入顺序。这意味着,删除重复的元素后,元 素的插入顺序就不对了。先来看下面这个例子。

  1. import java.util.ArrayList;
  2. import java.util.HashSet;
  3. public class MainClass
  4. {
  5. public static void main(String[] args)
  6. {
  7. //Constructing An ArrayList
  8. ArrayList<String> listWithDuplicateElements = new ArrayList<String>();
  9. listWithDuplicateElements.add("JAVA");
  10. listWithDuplicateElements.add("J2EE");
  11. listWithDuplicateElements.add("JSP");
  12. listWithDuplicateElements.add("SERVLETS");
  13. listWithDuplicateElements.add("JAVA");
  14. listWithDuplicateElements.add("STRUTS");
  15. listWithDuplicateElements.add("JSP");
  16. //Printing listWithDuplicateElements
  17. System.out.print("ArrayList With Duplicate Elements :");
  18. System.out.println(listWithDuplicateElements);
  19. //Constructing HashSet using listWithDuplicateElements
  20. HashSet<String> set = new HashSet<String>(listWithDuplicateElements);
  21. //Constructing listWithoutDuplicateElements using set
  22. ArrayList<String> listWithoutDuplicateElements = new ArrayList<String>(set);
  23. //Printing listWithoutDuplicateElements
  24. System.out.print("ArrayList After Removing Duplicate Elements :");
  25. System.out.println(listWithoutDuplicateElements);
  26. }
  27. }
复制代码

输出:

  1. ArrayList With Duplicate Elements :[JAVA, J2EE, JSP, SERVLETS, JAVA, STRUTS, JSP]
  2. ArrayList After Removing Duplicate Elements :[JAVA, SERVLETS, JSP, J2EE, STRUTS]
复制代码

注意输出结果。你会发现,在删除重复元素之后,元素重新洗牌。不再按照插入顺序排列。如果你想在删除重复的元素之后依然保持元素的插入顺序,那么不 建议使用此方法。还有另一种方法,可以保证在删除重复的元素之后也不改变元素的插入顺序。那就是使用LinkedHashSet。

方法2:使用LinkedHashSet删除ArrayList中重复的元素

在该方法中,我们使用LinkedHashSet删除ArrayList中重复的元素。正如你知道的,LinkedHashSet不允许重复元素, 同时保持元素的插入顺序。LinkedHashSet的这两个属性可以确保在删除ArrayList中的重复元素之后,依然保持元素的插入顺序。参见下面 的例子。

  1. import java.util.ArrayList;
  2. import java.util.LinkedHashSet;
  3. public class MainClass
  4. {
  5. public static void main(String[] args)
  6. {
  7. //Constructing An ArrayList
  8. ArrayList<String> listWithDuplicateElements = new ArrayList<String>();
  9. listWithDuplicateElements.add("JAVA");
  10. listWithDuplicateElements.add("J2EE");
  11. listWithDuplicateElements.add("JSP");
  12. listWithDuplicateElements.add("SERVLETS");
  13. listWithDuplicateElements.add("JAVA");
  14. listWithDuplicateElements.add("STRUTS");
  15. listWithDuplicateElements.add("JSP");
  16. //Printing listWithDuplicateElements
  17. System.out.print("ArrayList With Duplicate Elements :");
  18. System.out.println(listWithDuplicateElements);
  19. //Constructing LinkedHashSet using listWithDuplicateElements
  20. LinkedHashSet<String> set = new LinkedHashSet<String>(listWithDuplicateElements);
  21. //Constructing listWithoutDuplicateElements using set
  22. ArrayList<String> listWithoutDuplicateElements = new ArrayList<String>(set);
  23. //Printing listWithoutDuplicateElements
  24. System.out.print("ArrayList After Removing Duplicate Elements :");
  25. System.out.println(listWithoutDuplicateElements);
  26. }
  27. }
复制代码

输出:

  1. ArrayList With Duplicate Elements :[JAVA, J2EE, JSP, SERVLETS, JAVA, STRUTS, JSP]
  2. ArrayList After Removing Duplicate Elements :[JAVA, J2EE, JSP, SERVLETS, STRUTS]
复制代码

注意输出。你可以发现在删除ArrayList中的重复元素后,依然保持了元素的插入顺序。

译文链接: http://www.codeceo.com/article/java-arraylist-remove-duplicate-ele.html
英文原文: How To Remove Duplicate Elements From ArrayList In Java?
翻译作者: 码农网 – 小峰

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-10 00:17

Copyright 2015-2025 djqfx

返回顶部