美文网首页
lambda例子

lambda例子

作者: Let_me_think | 来源:发表于2018-08-03 22:33 被阅读0次
    
    /**
     * 输出list所有元素
     */
    books().stream().map(Book::getId).collect(Collectors.toList()).forEach(System.out::println);
    System.out.println("--------------分割---");
    /**
     * 获取拼接id,用,分割前后用()包起来
     */
    String str = books().stream().map(Book::getId).collect(Collectors.joining(",", "(", ")"));
    System.out.println(str);
    System.out.println("--------------分割---");
    /**
     * 获取所有id,用,分割前后用()包起来,并且每个id左右加单引号
     */
    String str1 = books().stream().map(book -> "'" + book.getId() + "'").collect(Collectors.joining(",", "(", ")"));
    System.out.println(str1);
    

    输出

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    --------------分割---
    (1,2,3,4,5,6,7,8,9,10,11,12,13)
    --------------分割---
    ('1','2','3','4','5','6','7','8','9','10','11','12','13')
    
    /**
     * 排序
     */
    Comparator<Book> comparable = (book1, book2) -> Double.compare(book1.getPrice(), book2.getPrice());
    /**
     * 按照价格排序升序
     */
    books().stream().sorted(comparable).collect(Collectors.toList()).forEach(System.out::println);
    System.out.println("--------------分割---");
    
    /**
     * 按照价格排序降序
     */
    books().stream().sorted(comparable.reversed()).collect(Collectors.toList()).forEach(System.out::println);
    System.out.println("--------------分割---");
    
    /**
     * 按照价格升序,价格一样按照时间升序(由远到近)
     */
    Comparator<Book> comparable2 = (book1, book2) -> Double.compare(book1.getPrice(), book2.getPrice());
    books().stream().sorted(comparable2.thenComparing((book1, book2) -> book1.getPublishDay().isAfter(book2.getPublishDay()) ? 1 : -1)).forEach(System.out::println);
    

    输出

    Book{id='2', name='jetty', price=16.0, type='服务器', publishDay=2018-02-04}
    Book{id='3', name='nginx', price=26.0, type='服务器', publishDay=2018-03-10}
    Book{id='1', name='tomcat', price=36.0, type='服务器', publishDay=2018-02-03}
    Book{id='4', name='python', price=66.0, type='编程语言', publishDay=2018-04-06}
    Book{id='5', name='mysql', price=66.0, type='数据库', publishDay=2018-05-09}
    Book{id='6', name='oracle', price=66.0, type='数据库', publishDay=2018-06-10}
    Book{id='7', name='redis', price=66.0, type='数据库', publishDay=2018-02-11}
    Book{id='8', name='thymeleaf', price=76.0, type='其他', publishDay=2018-02-12}
    Book{id='9', name='http', price=86.0, type='其他', publishDay=2018-02-13}
    Book{id='10', name='toc', price=96.0, type='其他', publishDay=2018-02-14}
    Book{id='11', name='php', price=106.0, type='其他', publishDay=2018-02-15}
    Book{id='12', name='算法导论', price=116.0, type='其他', publishDay=2018-02-16}
    Book{id='13', name='mysql 5.7', price=116.0, type='数据库', publishDay=2018-05-09}
    --------------分割---
    Book{id='12', name='算法导论', price=116.0, type='其他', publishDay=2018-02-16}
    Book{id='13', name='mysql 5.7', price=116.0, type='数据库', publishDay=2018-05-09}
    Book{id='11', name='php', price=106.0, type='其他', publishDay=2018-02-15}
    Book{id='10', name='toc', price=96.0, type='其他', publishDay=2018-02-14}
    Book{id='9', name='http', price=86.0, type='其他', publishDay=2018-02-13}
    Book{id='8', name='thymeleaf', price=76.0, type='其他', publishDay=2018-02-12}
    Book{id='4', name='python', price=66.0, type='编程语言', publishDay=2018-04-06}
    Book{id='5', name='mysql', price=66.0, type='数据库', publishDay=2018-05-09}
    Book{id='6', name='oracle', price=66.0, type='数据库', publishDay=2018-06-10}
    Book{id='7', name='redis', price=66.0, type='数据库', publishDay=2018-02-11}
    Book{id='1', name='tomcat', price=36.0, type='服务器', publishDay=2018-02-03}
    Book{id='3', name='nginx', price=26.0, type='服务器', publishDay=2018-03-10}
    Book{id='2', name='jetty', price=16.0, type='服务器', publishDay=2018-02-04}
    --------------分割---
    Book{id='2', name='jetty', price=16.0, type='服务器', publishDay=2018-02-04}
    Book{id='3', name='nginx', price=26.0, type='服务器', publishDay=2018-03-10}
    Book{id='1', name='tomcat', price=36.0, type='服务器', publishDay=2018-02-03}
    Book{id='7', name='redis', price=66.0, type='数据库', publishDay=2018-02-11}
    Book{id='4', name='python', price=66.0, type='编程语言', publishDay=2018-04-06}
    Book{id='5', name='mysql', price=66.0, type='数据库', publishDay=2018-05-09}
    Book{id='6', name='oracle', price=66.0, type='数据库', publishDay=2018-06-10}
    Book{id='8', name='thymeleaf', price=76.0, type='其他', publishDay=2018-02-12}
    Book{id='9', name='http', price=86.0, type='其他', publishDay=2018-02-13}
    Book{id='10', name='toc', price=96.0, type='其他', publishDay=2018-02-14}
    Book{id='11', name='php', price=106.0, type='其他', publishDay=2018-02-15}
    Book{id='12', name='算法导论', price=116.0, type='其他', publishDay=2018-02-16}
    Book{id='13', name='mysql 5.7', price=116.0, type='数据库', publishDay=2018-05-09}
    
    /**
     * list 转 map 用id做key
     */
    Map<String, Book> bookMap = books().stream().collect(Collectors.toMap(Book::getId, book -> book));
    System.out.println(bookMap);
    

    输出

    {11=Book{id='11', name='php', price=106.0, type='其他', publishDay=2018-02-15}, 12=Book{id='12', name='算法导论', price=116.0, type='其他', publishDay=2018-02-16}, 1=Book{id='1', name='tomcat', price=36.0, type='服务器', publishDay=2018-02-03}, 13=Book{id='13', name='mysql 5.7', price=116.0, type='数据库', publishDay=2018-05-09}, 2=Book{id='2', name='jetty', price=16.0, type='服务器', publishDay=2018-02-04}, 3=Book{id='3', name='nginx', price=26.0, type='服务器', publishDay=2018-03-10}, 4=Book{id='4', name='python', price=66.0, type='编程语言', publishDay=2018-04-06}, 5=Book{id='5', name='mysql', price=66.0, type='数据库', publishDay=2018-05-09}, 6=Book{id='6', name='oracle', price=66.0, type='数据库', publishDay=2018-06-10}, 7=Book{id='7', name='redis', price=66.0, type='数据库', publishDay=2018-02-11}, 8=Book{id='8', name='thymeleaf', price=76.0, type='其他', publishDay=2018-02-12}, 9=Book{id='9', name='http', price=86.0, type='其他', publishDay=2018-02-13}, 10=Book{id='10', name='toc', price=96.0, type='其他', publishDay=2018-02-14}}
    
    /**
     * 求所有书籍的价格平均数
     */
    System.out.print("价格平均值===============");
    Double aver = books().stream().collect(Collectors.averagingDouble(Book::getPrice));
    System.out.println(aver);
    
    
    System.out.print("价格最高的===============");
    /**
     * 获取价格最贵的书
     */
    Book book = books().stream().max((book1, book2) -> Double.compare(book1.getPrice(), book2.getPrice())).get();
    System.out.println(book);
    
    System.out.print("价格最高的===============");
    /**
     * 获取价格最贵的书-上一种的简略写法
     */
    book = books().stream().max(Comparator.comparing(Book::getPrice)).get();
    System.out.println(book);
    
    System.out.print("价格最高的===============");
    /**
     * 使用收集器获取价格最大的书籍
     */
    book = books().stream().collect(Collectors.maxBy(Comparator.comparing(Book::getPrice))).get();
    System.out.println(book);
    
    System.out.print("价格最低的===============");
    /**
     * 价格最低的书籍
     */
    book = books().stream().min((book1, book2) -> Double.compare(book1.getPrice(), book2.getPrice())).get();
    System.out.println(book);
    
    System.out.print("价格最低的===============");
    /**
     * 价格最低的书籍-上一种的简略写法
     */
    book = books().stream().min(Comparator.comparing(Book::getPrice)).get();
    System.out.println(book);
    
    System.out.print("价格最低的===============");
    /**
     * 使用收集器获取价格最低的书籍
     */
    book = books().stream().collect(Collectors.minBy(Comparator.comparing(Book::getPrice))).get();
    System.out.println(book);
    
    System.out.print("价格最高,并且是最晚出版的===============");
    /**
     * 获取价格最高的书籍,并且是最晚出版的
     */
    Comparator<Book> comp = Comparator.comparing(Book::getPrice);
    book = books().stream().collect(Collectors.maxBy(comp.thenComparing(Book::getPublishDay))).get();
    System.out.println(book);
    
    System.out.print("价格最高,并且是最早出版的===============");
    /**
     * 获取价格最高的书籍,并且是最早出版的
     */
    book = books().stream().collect(Collectors.maxBy(comp.thenComparing((book1, book2) -> book1.getPublishDay().isAfter(book2.getPublishDay()) ? -1 : 1))).get();
    System.out.println(book);
    

    输出

    价格平均值===============72.15384615384616
    价格最高的===============Book{id='12', name='算法导论', price=116.0, type='其他', publishDay=2018-02-16}
    价格最高的===============Book{id='12', name='算法导论', price=116.0, type='其他', publishDay=2018-02-16}
    价格最高的===============Book{id='12', name='算法导论', price=116.0, type='其他', publishDay=2018-02-16}
    价格最低的===============Book{id='2', name='jetty', price=16.0, type='服务器', publishDay=2018-02-04}
    价格最低的===============Book{id='2', name='jetty', price=16.0, type='服务器', publishDay=2018-02-04}
    价格最低的===============Book{id='2', name='jetty', price=16.0, type='服务器', publishDay=2018-02-04}
    价格最高,并且是最晚出版的===============Book{id='13', name='mysql 5.7', price=116.0, type='数据库', publishDay=2018-05-09}
    价格最高,并且是最早出版的===============Book{id='12', name='算法导论', price=116.0, type='其他', publishDay=2018-02-16}
    
    /**
     * 按类别分组收集
     */
    System.out.println("按类别分组:");
    Map<String, List<Book>> map = books().stream().collect(Collectors.groupingBy(Book::getType));
    map.keySet().forEach(key -> {
        System.out.println(key);
        System.out.println(map.get(key));
    });
    System.out.println("--------------分割---");
    
    /**
     * 按类别统计
     */
    System.out.println("按类别统计个数:");
    Map<String, Long> countMap = books().stream().collect(Collectors.groupingBy(Book::getType, Collectors.counting()));
    System.out.println(countMap);
    System.out.println("--------------分割---");
    
    /**
     * 按类别统计价格的sum
     */
    System.out.println("按类别统计价格的sum:");
    Map<String, Double> sumMap = books().stream().collect(Collectors.groupingBy(Book::getType, Collectors.summingDouble(Book::getPrice)));
    System.out.println(sumMap);
    System.out.println("--------------分割---");
    
    /**
     * 按类别统计价格平均数
     */
    System.out.println("按类别统计价格平均数:");
    Map<String, Double> aveMap = books().stream().collect(Collectors.groupingBy(Book::getType, Collectors.averagingDouble(Book::getPrice)));
    System.out.println(aveMap);
    System.out.println("--------------分割---");
    /**
     * 每个类别中价格最高的
     */
    System.out.println("每个类别中价格最高的:");
    Map<String, Optional<Book>> maxMap = books().stream().collect(Collectors.groupingBy(Book::getType, Collectors.maxBy(Comparator.comparing(Book::getPrice))));
    System.out.println(maxMap);
    System.out.println("--------------分割---");
    /**
     * 每个类别中价格最高的
     */
    System.out.println("每个类别中价格最低的:");
    Map<String, Optional<Book>> minMap = books().stream().collect(Collectors.groupingBy(Book::getType, Collectors.minBy(Comparator.comparing(Book::getPrice))));
    System.out.println(minMap);
    System.out.println("--------------分割---");
    /**
     * 每个类别中最晚发布的
     */
    System.out.println("每个类别中最晚发布的:");
    Map<String, Optional<Book>> maxPublishMap = books().stream().collect(Collectors.groupingBy(Book::getType, Collectors.maxBy(Comparator.comparing(Book::getPublishDay))));
    System.out.println(maxPublishMap);
    

    输出

    按类别分组:
    编程语言
    [Book{id='4', name='python', price=66.0, type='编程语言', publishDay=2018-04-06}]
    服务器
    [Book{id='1', name='tomcat', price=36.0, type='服务器', publishDay=2018-02-03}, Book{id='2', name='jetty', price=16.0, type='服务器', publishDay=2018-02-04}, Book{id='3', name='nginx', price=26.0, type='服务器', publishDay=2018-03-10}]
    其他
    [Book{id='8', name='thymeleaf', price=76.0, type='其他', publishDay=2018-02-12}, Book{id='9', name='http', price=86.0, type='其他', publishDay=2018-02-13}, Book{id='10', name='toc', price=96.0, type='其他', publishDay=2018-02-14}, Book{id='11', name='php', price=106.0, type='其他', publishDay=2018-02-15}, Book{id='12', name='算法导论', price=116.0, type='其他', publishDay=2018-02-16}]
    数据库
    [Book{id='5', name='mysql', price=66.0, type='数据库', publishDay=2018-05-09}, Book{id='6', name='oracle', price=66.0, type='数据库', publishDay=2018-06-10}, Book{id='7', name='redis', price=66.0, type='数据库', publishDay=2018-02-11}, Book{id='13', name='mysql 5.7', price=116.0, type='数据库', publishDay=2018-05-09}]
    --------------分割---
    按类别统计个数:
    {编程语言=1, 服务器=3, 其他=5, 数据库=4}
    --------------分割---
    按类别统计价格的sum:
    {编程语言=66.0, 服务器=78.0, 其他=480.0, 数据库=314.0}
    --------------分割---
    按类别统计价格平均数:
    {编程语言=66.0, 服务器=26.0, 其他=96.0, 数据库=78.5}
    --------------分割---
    每个类别中价格最高的:
    {编程语言=Optional[Book{id='4', name='python', price=66.0, type='编程语言', publishDay=2018-04-06}], 服务器=Optional[Book{id='1', name='tomcat', price=36.0, type='服务器', publishDay=2018-02-03}], 其他=Optional[Book{id='12', name='算法导论', price=116.0, type='其他', publishDay=2018-02-16}], 数据库=Optional[Book{id='13', name='mysql 5.7', price=116.0, type='数据库', publishDay=2018-05-09}]}
    --------------分割---
    每个类别中价格最低的:
    {编程语言=Optional[Book{id='4', name='python', price=66.0, type='编程语言', publishDay=2018-04-06}], 服务器=Optional[Book{id='2', name='jetty', price=16.0, type='服务器', publishDay=2018-02-04}], 其他=Optional[Book{id='8', name='thymeleaf', price=76.0, type='其他', publishDay=2018-02-12}], 数据库=Optional[Book{id='5', name='mysql', price=66.0, type='数据库', publishDay=2018-05-09}]}
    --------------分割---
    每个类别中最晚发布的:
    {编程语言=Optional[Book{id='4', name='python', price=66.0, type='编程语言', publishDay=2018-04-06}], 服务器=Optional[Book{id='3', name='nginx', price=26.0, type='服务器', publishDay=2018-03-10}], 其他=Optional[Book{id='12', name='算法导论', price=116.0, type='其他', publishDay=2018-02-16}], 数据库=Optional[Book{id='6', name='oracle', price=66.0, type='数据库', publishDay=2018-06-10}]}
    
    
    /**
     * 过滤价格大于80的并且按照出版时间倒序,收集成list--这里直接输出
     */
    books().stream().filter(book -> book.getPrice() > 80d).sorted(Comparator.comparing(Book::getPublishDay).reversed()).collect(Collectors.toList()).forEach(System.out::println);
    

    输出

    Book{id='13', name='mysql 5.7', price=116.0, type='数据库', publishDay=2018-05-09}
    Book{id='12', name='算法导论', price=116.0, type='其他', publishDay=2018-02-16}
    Book{id='11', name='php', price=106.0, type='其他', publishDay=2018-02-15}
    Book{id='10', name='toc', price=96.0, type='其他', publishDay=2018-02-14}
    Book{id='9', name='http', price=86.0, type='其他', publishDay=2018-02-13}
    

    相关文章

      网友评论

          本文标题:lambda例子

          本文链接:https://www.haomeiwen.com/subject/uqjuvftx.html