• java 函数式编程


     

    比较金典的例子:

    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.Getter;
    import lombok.NoArgsConstructor;
    import lombok.experimental.Accessors;
    
    import java.util.Arrays;
    import java.util.List;
    import java.util.concurrent.CompletableFuture;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.ThreadLocalRandom;
    import java.util.concurrent.TimeUnit;
    import java.util.stream.Collectors;
    
    public class CompletableFutureMallDemo {
    
        static List<NetMall> list= Arrays.asList(
                new NetMall("jd"),
                new NetMall("dangdang"),
                new NetMall("taobao")
        );
    
        public static List<String> getPrice(List<NetMall> list,String productName){
            return list.stream().map(
                    netMall -> String.format(productName + " in %s price is %.2f", netMall.getNetMallName(),
                            netMall.calcPrice(productName)))
                    .collect(Collectors.toList());
        }
    
        public static  List<String> getPriceByCompletableFuture(List<NetMall> list,String productName){
            return list.stream().map(netMall ->
                            CompletableFuture.supplyAsync(() ->
                    String.format(productName + " in %s price is %.2f", netMall.getNetMallName(),
                            netMall.calcPrice(productName))
            )).collect(Collectors.toList())
                    .stream().map(s -> s.join()).collect(Collectors.toList());
        }
    
        public static void main(String[] args) {
            long starTime=System.currentTimeMillis();
           List<String> list1= getPrice(list,"mysql");
           for (String element:list1){
               System.out.println(element);
           }
            long endTime=System.currentTimeMillis();
            System.out.println("-------------costTime:"+(endTime-starTime)+"毫秒");
    
            System.out.println("-----------------");
    
            long starTime1=System.currentTimeMillis();
            List<String> list2= getPriceByCompletableFuture(list,"mysql");
            for (String element:list2){
                System.out.println(element);
            }
            long endTime1=System.currentTimeMillis();
            System.out.println("-------------costTime:"+(endTime1-starTime1)+"毫秒");
        }
    
    }
    
    class NetMall{
        @Getter
        private String netMallName;
    
        public NetMall(String netMallName) {
            this.netMallName = netMallName;
        }
    
        public double calcPrice(String productName){
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
           return ThreadLocalRandom.current().nextDouble()*2+productName.charAt(0);
        }
    }
    
    @AllArgsConstructor
    @NoArgsConstructor
    @Data
    @Accessors(chain = true)
    class Student{
        private Integer id;
        private String studentName;
        private String major;
    }
    

      

  • 相关阅读:
    linux内核分析第八周理解进程调度时机跟踪分析进程调度与进程切换的过程
    linux内核分析第六周分析Linux内核创建一个新进程的过程
    Linux内核学习总结
    Linux内核分析第一周通过分析汇编代码理解计算机是如何工作的
    linux内核分析第五周分析system_call中断处理过程
    linux内核分析第三周跟踪分析Linux内核的启动过程
    转载:Understanding WPF Application Lifecycle
    Quick Sort 快速排序
    C#中的Immutable(不变的)
    一个lock和一个deadlock的例子
  • 原文地址:https://www.cnblogs.com/sunliyuan/p/16646561.html
Copyright © 2020-2023  润新知