lambda表达式实战

/**
 * ClassName:LambdaInAction <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON. <br/>
 * Date: 2017910日 下午8:12:49 <br/>
 * 
 * @author: Lelonta
 * @version
 * @see
 */
public class LambdaInAction {

//  @Test
    public void map() {
        //转换
        String queryString = "name=lelonta&id=100000&password=123456";
        Map<String, String> map = Stream.of(queryString.split("&"))
                .map(str -> str.split("="))
                .collect(Collectors.toMap(str -> str[0], str -> str[1]));

        System.out.println("map=" + map);
    }

    // str -> str.split("=")
    public String[] getSplit(String str) {

        return str.split("=");
    }

//  @Test
    public void getId() {
        List<Integer> list = books().stream().map(book -> book.getId())
                .collect(Collectors.toList());
        System.out.println("list=" + list);
        list = books().stream().map(Book::getId).collect(Collectors.toList());

        System.out.println("list=" + list);

        String str = books().stream().map(book -> book.getId() + "")
                .collect(Collectors.joining(","));
        System.err.println("str=" + str);
    }

//  @Test
    public void getType() {
        List<String> list = books().stream().map(book -> book.getType())
                .collect(Collectors.toList());
        System.out.println("list=" + list);

        list = books().stream().map(book -> book.getType()).distinct()
                .collect(Collectors.toList());
        System.out.println("去重后的list=" + list);

    }


    public void setOrder() {
        // 价格从低到高
        // books().stream()
        // .sorted((book1, book2) -> Double.compare(book1.getPrice(),
        // book2.getPrice())).forEach(System.out::println);

        // 从高到低
        Comparator<Book> comparator = (book1, book2) -> Double.compare(
                book1.getPrice(), book2.getPrice());
        books().stream().sorted(comparator.reversed())
                .forEach(System.out::println);

        // 简化的方法
        books().stream().sorted(Comparator.comparing(Book::getPrice))
                .forEach(System.out::println);
    }

    // 多条件排序
    public void setOrderCom() {
        Comparator<Book> comparator = (book1, book2) -> Double.compare(
                book1.getPrice(), book2.getPrice());
        books().stream()
                .sorted(comparator.thenComparing((book1, book2) -> book1
                        .getPublishDate().isAfter(book2.getPublishDate()) ? 1
                        : -1)).forEach(System.out::println);

        books().stream()
                .sorted(Comparator.comparing(Book::getPrice).thenComparing(
                        Comparator.comparing(Book::getPublishDate)))
                .forEach(System.out::println);

    }


    public void bookToMap() {
        Map<Integer, Book> map = books().stream().collect(
                Collectors.toMap(book -> book.getId(), book -> book));
        System.out.println(map);
    }

//  @Test
    public void avage() {

        Double d = books().stream().collect(
                Collectors.averagingDouble(Book::getPrice));
        System.out.println(d);
    }

//  @Test
    public void maxormin() {

        Optional<Book> bookOptional = books().stream().collect(
                Collectors.maxBy(Comparator.comparing(Book::getPrice)));
        System.out.println(bookOptional);
        bookOptional = books().stream().collect(
                Collectors.minBy(Comparator.comparing(Book::getPrice)));
        System.out.println(bookOptional);
    }

//  @Test
    public void group() {
        // 按类型分组
        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("---------------------------");
        });
    }

    // 统计数量
//  @Test
    public void count() {

        Map<String, Long> map = books().stream().collect(
                Collectors.groupingBy(Book::getType, Collectors.counting()));
        System.out.println(map);
    }

//  @Test
    public void sum() {
        // 每种类型的总价格
        Map<String, Double> d = books().stream().collect(
                Collectors.groupingBy(Book::getType,
                        Collectors.summingDouble(Book::getPrice)));

        System.out.println(d);
    }

//  @Test
    public void high() {

        // 每种类型最贵的
        Map<String, Optional<Book>> d = books().stream()
                .collect(
                        Collectors.groupingBy(Book::getType, Collectors
                                .maxBy(Comparator.comparing(Book::getPrice))));
        System.out.println(d);
    }

//  @Test
    public void filter() {

        books().stream().filter(book -> book.getPrice() >= 50)
                .sorted(Comparator.comparing(Book::getPublishDate).reversed())
                .forEach(System.out::println);

    }

    private List<Book> books() {

        List<Book> books = new ArrayList<Book>();

        books.add(new Book(1, "java", 50, "java", LocalDate.parse("2017-09-10")));
        books.add(new Book(2, "python", 51, "python", LocalDate
                .parse("2017-09-10")));
        books.add(new Book(3, "jsp", 52, "jsp", LocalDate.parse("2017-09-10")));
        books.add(new Book(4, "php", 53, "php", LocalDate.parse("2017-09-10")));
        books.add(new Book(5, "js", 54, "js", LocalDate.parse("2017-09-10")));
        return books;
    }

    public static void main(String[] args) {

        String queryString = "name=lelonta&id=100000&password=123456";
        Map<String, String> map = Stream.of(queryString.split("&"))
                .map(str -> str.split("="))
                .collect(Collectors.toMap(str -> str[0], str -> str[1]));

        System.out.println("map=" + map);//map={password=123456, name=lelonta, id=100000}

        String queryString1 = "name=lelonta&id=100000&password=123456";
        String[] str = queryString.split("&");
//      System.out.println(str);
        Map map1 = new HashMap();
        for (String str1:str) {
//          System.out.println(str1);
            String[] str2 = str1.split("=");
            map.put("key",str2[0]);
            map.put("value",str2[1]);

        }
        System.out.println(map);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乐观的Terry

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值