Home>Article>Java> What are the ways to remove duplicates from List collections in Java?
What are the ways to remove duplicates from List collections in Java?
王林forward
2019-11-27 15:11:134643browse
Duplication removal method:
1. Through the characteristics of the set collection, the uniqueness of the set elements
public static List heavyListMethod01(List list){ Set set=new HashSet(list); //Set set1=new TreeSet(list); List alist=new ArrayList(); for (Object o:set) { alist.add(o); } return alist; }
Use the characteristics of the set collection, the uniqueness of the elements AddAll(Collection c)
public static List heavyListMethod02(List list){ List newList=new ArrayList(); newList.addAll(new HashSet(list)); return newList; }
2. Use the list method contains method to remove duplicates
public static List heavyListMethod03(List list){ List alist=new ArrayList(); for (Object o:list) { if(!(alist.contains(o))){ alist.add(o); } } return alist; }
3. By traversing, and then using the remove method to remove duplicate elements
public static List heavyListMethod04(List
Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete