3 List去重--distinct

版权声明:看什么?6,你和我,走一波! https://blog.csdn.net/qq_31323797/article/details/89330498
package com.java8.list;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author gp6
 * @date 2019-04-15
 */
public class TestList {
    public static void main(String[] args) {
      List<Integer> integerList = new ArrayList<Integer>() {{
            add(1);
            add(2);
            add(3);
            add(4);
            add(5);
            add(4);
        }};

        // 去重处理,distinct基于Object.equals(Object)实现
        // 过滤出所有偶数,在去重
        List<Integer> integerListDistinct = integerList.stream()
                .filter(num -> num % 2 == 0).distinct()
                .collect(Collectors.toList());

		// [2, 4]
        System.out.println(integerListDistinct);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_31323797/article/details/89330498
0条评论
添加一条新回复