Java 8 - 根据类的某个或多个属性去重

1. 基本类型数据进行去重
方法1:使用 HashSet

	public class MainDemo {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 5, 5, 5, 6, 7);
        Set<Integer> set = new HashSet<>(numbers);
        System.out.println(set); //1, 2, 3, 4, 5, 6, 7
    }
}

方法2:使用 java 8 stream 中的 distinct() 方法

public class MainDemo {
    public static void main(String[] args) {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 5, 5, 5, 6, 7);
        List<Integer> distinctNumbers = numbers.stream()
                .distinct()
                .collect(Collectors.toList());
        System.out.println(distinctNumbers); //1, 2, 3, 4, 5, 6, 7
    }
}

2. 根据类的某个属性去重
根据对象的某个属性去重

@AllArgsConstructor
@Data
public class People{
    private String name;
    private String phone;
    private Integer age;
}
public class Main {
    public static void main(String[] args) {
        People people1 = new People("张三","18767880909",18);
        People people2 = new People("张四","18767880910",19);
        People people3 = new People("张五","18767880911",20);
        People people4 = new People("张六","18767880912",18);
        People people5 = new People("张三","18767880909",19);
        People people6 = new People("张四","18767880910",20);

        List<People> list = new ArrayList<>();
        list.add(people1);
        list.add(people2);
        list.add(people3);
        list.add(people4);
        list.add(people5);
        list.add(people6);
        
        // 根据People的name属性去重
        List<People> collect = list.stream().distinct()
                .filter(distinctByKey(People::getName))
                .collect(Collectors.toList());
        
        for (People people : collect) {
            System.out.println(people);
        }
    }
    
   // 首先定义一个过滤器 
    public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
        return object -> seen.putIfAbsent(keyExtractor.apply(object), Boolean.TRUE) == null;
    }
}

执行结果:

People(name=张三, phone=18767880909, age=18)
People(name=张四, phone=18767880910, age=19)
People(name=张五, phone=18767880911, age=20)
People(name=张六, phone=18767880912, age=18)

3. 根据类的多个属性去重
下面的两种去重方法都需要People类重写hashcode()方法和equals()方法:

@AllArgsConstructor
@Data
public class People{
    private String name;
    private String phone;
    private Integer age;

    // 重写equals()方法
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        People that = (People) o;
        return Objects.equals(name, that.name) && Objects.equals(phone, that.phone);
    }

    // 重写hashcode()方法
    @Override
    public int hashCode() {
        return Objects.hash(name, phone);
    }
}

方法1:使用 HashSet
我们的类必须实现hashCode()和equals()方法。

public class Main {
    public static void main(String[] args) {
        People people1 = new People("张三","18767880909",18);
        People people2 = new People("张四","18767880910",19);
        People people3 = new People("张五","18767880911",20);
        People people4 = new People("张六","18767880912",18);
        People people5 = new People("张三","18767880909",19);
        People people6 = new People("张四","18767880910",20);

        List<People> list = new ArrayList<>();
        list.add(people1);
        list.add(people2);
        list.add(people3);
        list.add(people4);
        list.add(people5);
        list.add(people6);

        // hashset 
        HashSet<People> set = new HashSet<>(list);
        
        for (People people : set) {
            System.out.println(people);
        }
    }
}
People(name=张三, phone=18767880909, age=18)
People(name=张四, phone=18767880910, age=19)
People(name=张六, phone=18767880912, age=18)
People(name=张五, phone=18767880911, age=20)

方法2:使用 java 8 stream 中的 distinct() 方法
distinct()方法使用hashCode()和equals()方法来获取不同的元素。所以,我们的类必须实现hashCode()和equals()方法。

public class Main {
    public static void main(String[] args) {
        People people1 = new People("张三","18767880909",18);
        People people2 = new People("张四","18767880910",19);
        People people3 = new People("张五","18767880911",20);
        People people4 = new People("张六","18767880912",18);
        People people5 = new People("张三","18767880909",19);
        People people6 = new People("张四","18767880910",20);

        List<People> list = new ArrayList<>();
        list.add(people1);
        list.add(people2);
        list.add(people3);
        list.add(people4);
        list.add(people5);
        list.add(people6);

        // distinct
        List<People> collect = list.stream().distinct().collect(Collectors.toList());
        
        for (People people : collect) {
            System.out.println(people);
        }
    }
}

执行结果:

People(name=张三, phone=18767880909, age=18)
People(name=张四, phone=18767880910, age=19)
People(name=张六, phone=18767880912, age=18)
People(name=张五, phone=18767880911, age=20)

你可能感兴趣的:(Java,java,开发语言)