• 【Java】Java8的Lambda入门记录


    简化定义匿名实现类

    匿名实现类的传统方式

    创建一个线程,需要实现Runnable接口,并实现public void run()方法,用传统的方式是这样的:

        public static void main(String[] args) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println("hello world");
                }
            }).start();
        }
    

    无参数、单语句方法体

    用lambda可以简化成这样:

    Runnable r = () -> System.out.print("hello world");
    new Thread(r).start();
    

    所以也可以这样:

        public static void main(String[] args) {
            new Thread(() -> System.out.println("hello world")).start();
        }
    

    所以,没参数、单语句的是这样的:

    public class NoParameter {
        
        public static void main(String[] args) {
            MyInterface myInterface = () -> System.out.println("Hello World");
            myInterface.methodA();
        }
        
        public interface MyInterface {
            public void methodA();
        }
    
    }
    

    无参数、多语句方法体

    当然实现方法中有多条语句的情况下,应该是这样的:

        public static void main(String[] args) {
            new Thread(() -> {
                System.out.println("1");
                System.out.println("2");
            }).start();
        }
    

    多参数、多语句方法体

    多参数、多语句的是这样的:

    public class MoreParameterMoreStatement {
    
        public static void main(String[] args) {
            MyInterface myInterface = (x, y) -> {
                System.out.println("1st parameter : " + x);
                System.out.println("2nd parameter : " + y);
            };
            myInterface.methodA(1, 5);
        }
    
        public interface MyInterface {
            public void methodA(Integer x, Integer y);
        }
    
    }
    

    流,Stream

    这里的流,并非Java IO的流,是简化处理Java Collection的流。

    过滤条件

        public static void main(String[] args) {
            List<String> list = Arrays.asList(new String[] {"1", "2", "3"});
            
            Stream<String> stream = list.stream();
            Predicate<String> predicate = s -> s != null && s.equals("1"); // 断言,Predicate。入参对象,出参boolean,用于判别一个对象
            System.out.println("count -> " + stream.filter(predicate).count()); // 过滤并统计
        }
    

    简写:

            /**
    	 * 过滤
    	 */
    	@Test
    	public void predicateTestx1() {
    		List<String> list = Arrays.asList(new String[] {"1", "2", "3"});
    		
    		List<String> resultList = list.stream().filter(s -> s != null && s.equals("1")).collect(Collectors.toList());
    		this.logger.info("resultList -> " + resultList);
    	}
    

    Predicate有一个抽象方法:boolean test(T t);,入参是对象,出参是布尔值。
    filter方法会调用test方法:

        @Override
        public final Stream<P_OUT> filter(Predicate<? super P_OUT> predicate) {
            Objects.requireNonNull(predicate);
            return new StatelessOp<P_OUT, P_OUT>(this, StreamShape.REFERENCE,
                                         StreamOpFlag.NOT_SIZED) {
                @Override
                Sink<P_OUT> opWrapSink(int flags, Sink<P_OUT> sink) {
                    return new Sink.ChainedReference<P_OUT, P_OUT>(sink) {
                        @Override
                        public void begin(long size) {
                            downstream.begin(-1);
                        }
    
                        @Override
                        public void accept(P_OUT u) {
                            if (predicate.test(u)) // 调用test()的逻辑
                                downstream.accept(u); // 加入下沉集合
                        }
                    };
                }
            };
        }
    

    转换为不同类型的集合

        public static void main(String[] args) {
            List<String> list = Arrays.asList(new String[] {"1", "2", "3"});
            
            Stream<String> stream = list.stream();
            Function<String, Integer> function = i -> Integer.valueOf(i); // Function<T, R>,转换成不同的类型
            List<Integer> resultList = stream.map(function).collect(Collectors.toList());
            
            System.out.println(resultList);
        }
    

    简写:

            /**
    	 * 转换类型
    	 */
    	@Test
    	public void functionTestx1() {
    		List<String> list = Arrays.asList(new String[] {"1", "2", "3"});
    		
    		List<Integer> resultList = list.stream().map(i -> Integer.valueOf(i)).collect(Collectors.toList());
    		this.logger.info("resultList -> " + resultList);
    	}
    

    合并多个集合

        public static void main(String[] args) {
            List<String> list1 = Arrays.asList(new String[] {"1", "2", "3"});
            List<String> list2 = Arrays.asList(new String[] {"4", "5", "6"});
            
            Function<List<String>, Stream<String>> function = list -> list.stream(); // List<String>转换为Stream<String>
            List<String> allList = Stream.of(list1, list2).flatMap(function).collect(Collectors.toList()); // 合并多个集合
            System.out.println(allList);
        }
    

    简写:

            /**
    	 * 转换类型
    	 */
    	@Test
    	public void mergeTestx1() {
    		List<String> list1 = Arrays.asList(new String[] {"1", "2", "3"});
    		List<String> list2 = Arrays.asList(new String[] {"4", "5", "6"});
    		
    		// 这个不是我想要的结果
    		/*
    		List<List<String>> resultList1 = Stream.of(list1, list2).collect(Collectors.toList());
    		this.logger.info("resultList1 -> " + resultList1);
    		*/
    		
    		List<String> resultList2 = Stream.of(list1, list2).flatMap(i -> i.stream()).collect(Collectors.toList());
    		this.logger.info("resultList2 -> " + resultList2);
    	}
    

    获取集合最大、最小值

        public static void main(String[] args) {
            List<String> list = Arrays.asList(new String[] {"1", "2", "3"});
            
            String max = list.stream().max(String::compareTo).get(); // 使用compareTo
            String min = list.stream().min((x, y) -> x.compareTo(y)).get(); // 手动调用compareTo
            
            System.out.println("max -> " + max);
            System.out.println("min -> " + min);
        }
    

    分解操作,Reduce

        /**
         * 计算1-10总和
         */
        public static void main(String[] args) throws Exception {
            List<Integer> list = Arrays.asList(new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); // 声明集合
    
            BinaryOperator<Integer> binaryOperator = (x, y) -> {
                int temp = x + y;
                logger.info("temp sum -> {}", temp);
                return temp;
            };
    
            int sum = list.stream().reduce(binaryOperator).get();
            logger.info("sum -> {}", sum);
        }
    

    日志:

    2017-09-17 21:53:33.529 [main] INFO  c.n.exercise.stream.ReduceExercise - temp sum -> 3 
    2017-09-17 21:53:33.535 [main] INFO  c.n.exercise.stream.ReduceExercise - temp sum -> 6 
    2017-09-17 21:53:33.535 [main] INFO  c.n.exercise.stream.ReduceExercise - temp sum -> 10 
    2017-09-17 21:53:33.535 [main] INFO  c.n.exercise.stream.ReduceExercise - temp sum -> 15 
    2017-09-17 21:53:33.535 [main] INFO  c.n.exercise.stream.ReduceExercise - temp sum -> 21 
    2017-09-17 21:53:33.535 [main] INFO  c.n.exercise.stream.ReduceExercise - temp sum -> 28 
    2017-09-17 21:53:33.535 [main] INFO  c.n.exercise.stream.ReduceExercise - temp sum -> 36 
    2017-09-17 21:53:33.535 [main] INFO  c.n.exercise.stream.ReduceExercise - temp sum -> 45 
    2017-09-17 21:53:33.535 [main] INFO  c.n.exercise.stream.ReduceExercise - temp sum -> 55 
    2017-09-17 21:53:33.536 [main] INFO  c.n.exercise.stream.ReduceExercise - sum -> 55 
    

    并行操作

    这里例子的关键在于parallel()设置了并行处理,具体对比与reduce的日志。

        /**
         * 计算1-10总和
         */
        public static void main(String[] args) throws Exception {
            List<Integer> list = Arrays.asList(new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); // 声明集合
    
            BinaryOperator<Integer> binaryOperator = (x, y) -> {
                int temp = x + y;
                logger.info("temp sum -> {}", temp);
                return temp;
            };
    
            int sum = list.stream().parallel().reduce(binaryOperator).get();
            logger.info("sum -> {}", sum);
        }
    

    日志:

    2017-09-17 21:48:32.018 [ForkJoinPool.commonPool-worker-2] INFO  c.n.exercise.stream.ReduceExercise - temp sum -> 19 
    2017-09-17 21:48:32.018 [ForkJoinPool.commonPool-worker-1] INFO  c.n.exercise.stream.ReduceExercise - temp sum -> 9 
    2017-09-17 21:48:32.018 [main] INFO  c.n.exercise.stream.ReduceExercise - temp sum -> 13 
    2017-09-17 21:48:32.018 [ForkJoinPool.commonPool-worker-3] INFO  c.n.exercise.stream.ReduceExercise - temp sum -> 3 
    2017-09-17 21:48:32.023 [ForkJoinPool.commonPool-worker-2] INFO  c.n.exercise.stream.ReduceExercise - temp sum -> 27 
    2017-09-17 21:48:32.023 [ForkJoinPool.commonPool-worker-1] INFO  c.n.exercise.stream.ReduceExercise - temp sum -> 12 
    2017-09-17 21:48:32.023 [ForkJoinPool.commonPool-worker-2] INFO  c.n.exercise.stream.ReduceExercise - temp sum -> 40 
    2017-09-17 21:48:32.023 [ForkJoinPool.commonPool-worker-1] INFO  c.n.exercise.stream.ReduceExercise - temp sum -> 15 
    2017-09-17 21:48:32.023 [ForkJoinPool.commonPool-worker-1] INFO  c.n.exercise.stream.ReduceExercise - temp sum -> 55 
    2017-09-17 21:48:32.024 [main] INFO  c.n.exercise.stream.ReduceExercise - sum -> 55 
    

    两个集合的操作工具类:减去、交集、并集

    import java.util.List;
    import java.util.Set;
    import java.util.stream.Collectors;
    import java.util.stream.Stream;
    
    import org.assertj.core.util.Lists;
    import org.junit.Test;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    public class CollectionUtils {
    	
    	private static Logger logger = LoggerFactory.getLogger(CollectionUtils.class);
    
    	/**
    	 * 计算在list1存在,而在list2不存在的记录的集合
    	 */
    	public static <T> List<T> subtract(List<T> list1, List<T> list2) {
    		// 转换为Set
    		Set<T> set = list2.stream().collect(Collectors.toSet());
    		
    		// 计算在list1存在,而在list2不存在的记录的集合
    		List<T> resultList = list1.stream().filter(i -> !set.contains(i)).collect(Collectors.toList());
    		return resultList;
    	}
    	
    	/**
    	 * 计算交集,在list1存在,并且在list2也存在的记录的集合
    	 */
    	public static <T> List<T> intersect(List<T> list1, List<T> list2) {
    		// 转换为Set
    		Set<T> set = list2.stream().collect(Collectors.toSet());
    				
    		// 计算在list1存在,而在list2不存在的记录的集合
    		List<T> resultList = list1.stream().filter(i -> set.contains(i)).collect(Collectors.toList());
    		return resultList;
    	}
    	
    	/**
    	 * 计算并集,在list1和list2的记录合并,并且去重的集合
    	 */
    	public static <T> List<T> union(List<T> list1, List<T> list2) {
    		Set<T> set = Stream.of(list1, list2).flatMap(i -> i.stream()).collect(Collectors.toSet());
    		List<T> resultList = set.stream().collect(Collectors.toList());
    		
    		return resultList;
    	}
    	
    	@Test
    	public void subtractTest() {
    		List<Integer> list1 = Lists.newArrayList(1, 2, 3, 4, 5);
    		List<Integer> list2 = Lists.newArrayList(1, 5);
    		
    		List<Integer> resultList = CollectionUtils.subtract(list1, list2);
    		
    		this.logger.info("list1 -> {}", list1);
    		this.logger.info("resultList -> {}", resultList);
    	}
    	
    	@Test
    	public void intersectTest() {
    		List<Integer> list1 = Lists.newArrayList(1, 2, 3, 4, 5);
    		List<Integer> list2 = Lists.newArrayList(1, 5);
    		
    		List<Integer> resultList = CollectionUtils.intersect(list1, list2);
    		
    		this.logger.info("list1 -> {}", list1);
    		this.logger.info("resultList -> {}", resultList);
    	}
    	
    	@Test
    	public void unionTest() {
    		List<Integer> list1 = Lists.newArrayList(1, 2, 3, 4, 5);
    		List<Integer> list2 = Lists.newArrayList(1, 5, 6);
    		
    		List<Integer> resultList = CollectionUtils.union(list1, list2);
    		
    		this.logger.info("list1 -> {}", list1);
    		this.logger.info("resultList -> {}", resultList);
    	}
    
    }
    
  • 相关阅读:
    软件测试基本功之——概念篇
    bug描述注意点
    软件测试模型汇总-V模型,W模型,X模型,H模型
    软件测试分类(自动化测试暂不描述)
    黑盒测试用例设计方法&理论结合实际 -> 场景法
    前端学习(36)~js学习(十三):this
    前端学习(35)~js学习(十二):预编译
    前端学习(34)~js学习(十一):作用域和变量提升
    前端学习(33)~js学习(十):函数
    前端学习(32)~js学习(九):对象简介和对象的基本操作
  • 原文地址:https://www.cnblogs.com/nick-huang/p/7538094.html
Copyright © 2020-2023  润新知