zoukankan      html  css  js  c++  java
  • Stream系列(七)distinct方法使用

    #java##stream##distinct#

    去重

    视频讲解: https://www.bilibili.com/video/av77530685/

    EmployeeTestCase.java
    package com.example.demo;
    
    import lombok.Data;
    import lombok.ToString;
    import lombok.extern.log4j.Log4j2;
    import one.util.streamex.StreamEx;
    import org.junit.Test;
    
    import java.util.Collection;
    import java.util.Comparator;
    import java.util.List;
    import java.util.Map;
    import java.util.concurrent.ConcurrentHashMap;
    import java.util.function.Function;
    import java.util.function.Predicate;
    import java.util.stream.Collectors;
    import java.util.stream.LongStream;
    import java.util.stream.Stream;
    
    import static org.junit.Assert.assertEquals;
    import static org.junit.Assert.assertTrue;
    
    @Log4j2
    public class EmployeeTestCase extends BaseTest{
        @Test
        public void distinct() {
            //常规实现方式
            List<Employee> employeesDis = list.stream().distinct().collect(Collectors.toList());
            assertEquals(employeesDis.size(),5);
            //StreamEx 实现方式
            List<Employee> employeesDisBySalary2 = StreamEx.of(list).distinct(Employee::getSalary)
                    .peek(System.out::println).collect(Collectors.toList());
            //Stream filter 实现方式
            List<Employee> employeesDisBySalary = list.stream().filter(distinctByKey(Employee::getSalary))
                    .collect(Collectors.toList());
            assertEquals(employeesDisBySalary,employeesDisBySalary2);
        }
        private static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
            Map<Object,Boolean> seen = new ConcurrentHashMap<>();
            return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
        }
    }
    BaseTest.java
    package com.example.demo;
    
    import java.util.Arrays;
    import java.util.List;
    
    public class BaseTest {
        protected static final List<Employee> list = Arrays.asList(
                new Employee(1, "Alex", 1000),
                new Employee(2, "Michael", 2000),
                new Employee(3, "Jack", 1500),
                new Employee(4, "Owen", 1500),
                new Employee(5, "Denny", 2000));
    
        protected static final List<List<Employee>> listFlat = Arrays.asList(
                Arrays.asList(new Employee(1, "Alex", 1000),
                        new Employee(2, "Michael", 2000)),
                Arrays.asList(new Employee(3, "Jack", 1500),
                        new Employee(4, "Owen", 1500)),
                Arrays.asList(new Employee(5, "Denny", 2000)));
    }

     关注公众号,坚持每天3分钟视频学习

  • 相关阅读:
    yolo v1
    012. MVC5中Razor引擎使用模板页
    Centos 7 下, 安装odoo 10
    011. 解决VS2015中CS1528: Expected ; or = (cannot specify constructor arguments in declaration)
    010. VS2015创建MVC项目
    解决IE增强配置的问题
    解决Centos 7 下 tomcat字体异常 Font '宋体' is not available to the JVM
    tomcat7.0在centos7下中文乱码问题解决汇总
    windows服务器审核失败消息:事件ID: 861 进程标识符:904
    使用USBWriter做U盘启动盘后容量变小的解决办法
  • 原文地址:https://www.cnblogs.com/JavaWeiBianCheng/p/11962232.html
Copyright © 2011-2022 走看看