代码去重

1.list  对象依据某一属性去重  代码中Student为对象,属性则为id与name。代码依据id去除id相同的Student实体。

import java.util.*;
import static java.util.Comparator.comparingInt;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;

public class Test {

    public static void main(String[] args) {
        Student s1 = new Student();

        s1.setId(1);
        s1.setName("aaaa");

        Student s2 = new Student();
        s2.setId(2);
        s2.setName("bbbb");

        Student s3 = new Student();
        s3.setName("cccc");
        s3.setId(3);
        Student s4 = new Student();
        s4.setName("ddd");
        s4.setId(4);
        Student s5 = new Student();
        s5.setId(1);
        s5.setName("eee");

        List<Student> list = Arrays.asList(s1,s2,s3,s4,s5);
        List<Student> s = test2(list);
        System.out.println(Arrays.toString(s.toArray()));

    }

    //对象去重
    public static List<Student> test2(List<Student> list){
        List<Student> unique = list.stream().collect(
                collectingAndThen(
                        toCollection(() -> new TreeSet<>(comparingInt(Student::getId))), ArrayList::new)
        );

        return unique;
    }
}

 2.list  对象依据多个属性去重

import java.util.*;

import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;

public class Test {

    public static void main(String[] args) {
        Student s1 = new Student();

        s1.setId(1);
        s1.setName("aaaa");

        Student s2 = new Student();
        s2.setId(2);
        s2.setName("bbbb");

        Student s3 = new Student();
        s3.setName("cccc");
        s3.setId(3);
        Student s4 = new Student();
        s4.setName("ddd");
        s4.setId(4);
        Student s5 = new Student();
        s5.setId(1);
        s5.setName("aaaa");

        List<Student> list = Arrays.asList(s1,s2,s3,s4,s5);
        List<Student> s = test2(list);
        System.out.println(Arrays.toString(s.toArray()));

    }

    //对象去重
    public static List<Student> test2(List<Student> list){
        List<Student> unique = list.stream().collect(
                collectingAndThen(
                        toCollection(() -> new TreeSet<>(Comparator.comparing(s->s.getId()+";"+s.getName()))), ArrayList::new)
        );

        return unique;
    }
}

3.简单的两个list去重,addAll与removeAll联合使用

        Student s1 = new Student();

        s1.setId(1);
        s1.setName("aaaa");

        Student s2 = new Student();
        s2.setId(2);
        s2.setName("bbbb");

        Student s3 = new Student();
        s3.setName("cccc");
        s3.setId(3);

        List<Student> A = new ArrayList<>();
        A.add(s1);
        A.add(s2);
        A.add(s3);

        List<Student> B = new ArrayList<>();
        B.add(s1);


        System.out.println(A.removeAll(B));

        System.out.println(A.size());

3.两个list对象依据某一属性去重

//new 两个新的list,将不同的结果存储到这两个list中

        ArrayList<Student> returna = new ArrayList<>();
        ArrayList<Student> returnb = new ArrayList<>();
 
       

      for (B b: bArrayList){
            for (A a: aArrayList){
                if(b.getId().equals(a.getId())){
                    bReturn.add(b)
                    aArrayList.remove(a)
                    break;
                }
            }
        }

猜你喜欢

转载自blog.csdn.net/IT_LuoSha/article/details/88601908