Home > Java > javaTutorial > What are the ways to remove duplicates from List collections in Java?

What are the ways to remove duplicates from List collections in Java?

王林
Release: 2019-11-27 15:11:13
forward
4793 people have browsed it

What are the ways to remove duplicates from List collections in Java?

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;
    }
Copy after login

Free video tutorial recommendation: java video

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;
    }
Copy after login

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;
    }
Copy after login

3. By traversing, and then using the remove method to remove duplicate elements

public static List heavyListMethod04(List<Object> list){
       for (int i=0;i<list.size()-1;i++){
           Object o =list.get(i);
           for (int j=i+1;j<list.size();j++){
               if(o.equals(list.get(j))){
                   list.remove(j);
                   j--;
               }
           }
       }
       return list;
    }
Copy after login

4. (jdk1.8) Call the stream method to convert the list For a stream, remove duplicate elements in the stream through distinct (internally based on the equals() method)

public static List heavyListMethod05(List<Object> list){
        list = list.stream().distinct().collect(Collectors.toList());
        return list;
    }
Copy after login

Test verification:

public static void main(String[] args) {
        List list=new ArrayList();
        list.add(1);
        list.add(2);
        list.add(5);
        list.add(2);
        list.add(3);
        list.add(1);
        list.add(4);
        List list1 =heavyListMethod01(list);
        System.out.println("1----"+list1);//[1, 2, 3, 4, 5]
        List list2 =heavyListMethod02(list);
        System.out.println("2----"+list2);//[1, 2, 3, 4, 5]
        List list3 =heavyListMethod03(list);
        System.out.println("3----"+list3);//[1, 2, 5, 3, 4]
        List list4 =heavyListMethod04(list);
        System.out.println("4----"+list4);//[1, 2, 5, 3, 4]
        List list5 =heavyListMethod05(list);
        System.out.println("5----"+list5);//[1, 2, 5, 3, 4]
    }
Copy after login

Recommended related articles and tutorials: zero basic introduction to java

The above is the detailed content of What are the ways to remove duplicates from List collections in Java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template