Five methods of Java list de duplication

Time:2020-6-27

preface

De duplication is essential for many occasions. The reason for writing this article is that in the previous screen, I did it in the databaseDistinctandOrder ByDe duplication is found to affect the efficiency, so it is first processed in the background; therefore, this article has been recorded for reference:

Num1: use java8 new feature stream for list de duplication

public static void main(String[] args) {
    List list = new ArrayList();
    list.add(10);
    list.add(11);
    list.add(14);
    list.add(10);
    list.add(19);
    list.add(12);
    System.out.println ("initialization set is:" + list);
    List newList = (List) list.stream().distinct().collect(Collectors.toList());
    System.out.println (the set of "java8 new feature stream after de duplication is:" + NEWLIST);
  }

The results are as follows:

在这里插入图片描述

Num2: double for cycle weight removal

public static void main(String[] args) {
    List list = new ArrayList();
    list.add(10);
    list.add(11);
    list.add(14);
    list.add(10);
    list.add(19);
    list.add(12);
    System.out.println ("initialization set is:" + list);
    for (int i = 0; i < list.size(); i++) {
      for (int j = 0; j < list.size(); j++) {
        if(i!=j&&list.get(i)==list.get(j)) {
          list.remove(list.get(j));
        }
      }
    }
    System.out.println ("the new set after de duplication is:" + list);
  }

The results are as follows:

在这里插入图片描述

Num3: set set set to judge the de duplication without disturbing the order

public static void main(String[] args) {
    List list = new ArrayList();
    list.add(10);
    list.add(11);
    list.add(14);
    list.add(10);
    list.add(19);
    list.add(12);
    System.out.println ("initialization set is:" + list);
    Set set1 = new HashSet();
    List newList = new ArrayList();
    for (Object integer : list) {
      if(set1.add(integer)) {
        newList.add(integer);
      }
    }
    System.out.println ("set set set judgment de duplication:" + NEWLIST);
  }

The results are as follows:

在这里插入图片描述

Num4: judge to assign another list set after traversal

public static void main(String[] args) {
    List list = new ArrayList();
    list.add(10);
    list.add(11);
    list.add(14);
    list.add(10);
    list.add(19);
    list.add(12);
    System.out.println ("initialization set is:" + list);
    List newList2 = new ArrayList();
    for (Object integer : list) {
      if(!newList2.contains(integer)){
        newList2.add(integer);
      }
    }
    System.out.println ("the new set after de duplication is:" + newlist2);
  }

The results are as follows:

在这里插入图片描述

Num5: set and list conversion de duplication

public static void main(String[] args) {
    List list = new ArrayList();
    list.add(10);
    list.add(11);
    list.add(14);
    list.add(10);
    list.add(19);
    list.add(12);
    System.out.println ("initialization set is:" + list);
    Set set2 = new HashSet();
    List newList3 = new ArrayList();
    set2.addAll(list);
    newList3.addAll(set2);
    System.out.println ("set and list conversion de duplication:" + newlist3);
  }

The results are as follows:

在这里插入图片描述

The above is the whole content of this article. I hope it will help you in your study, and I hope you can support developepaer more.