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?

王林
王林forward
2019-11-27 15:11:134837browse

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;
    }

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;
    }

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<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;
    }

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;
    }

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]
    }

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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete