首页 > 编程语言 > 详细

java8 stream编程

时间:2019-09-07 23:59:37      阅读:172      评论:0      收藏:0      [点我收藏+]

说明:这是基于同事(cal,多谢)的培训材料做的练习,记录下来,以作日后自己coding分析和改进

1.准备

Dish.java

package com.example.demo;

import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * Created by Administrator on 2019/9/7.
 */
@Data
@AllArgsConstructor
public class Dish {
    private int calories;
    private String name;

}
List<Dish> list;
list = Lists.newArrayList(
new Dish( 1,"aa"),
new Dish(13,"bb"),
new Dish(7,"cc"),
new Dish( 6,"aa"),
new Dish(9,"dd"),
new Dish(1,"ee"),
new Dish( 3,"aa"),
new Dish(1,"ff"),
new Dish(1,"gg")
);
 

2.stream的创建

Integer[] ints={1,2};
        List<Integer> list2=Lists.newArrayList(ints);
        List<String> collect3 = list2.stream().map(o -> "1" + o).collect(Collectors.toList());
        List<String> collect4 = Stream.of(1,2).map(o -> "1" + o).collect(Collectors.toList());
        List<String> collect5 = Stream.of(ints).map(o -> "1" + o).collect(Collectors.toList());
        List<String> collect6 = Stream.of(list2).map(o -> "1" + o).collect(Collectors.toList());
 System.out.println(collect3);
        System.out.println(collect4);
        System.out.println(collect5);
        System.out.println(collect6);

输出:

[11, 12]
[11, 12]
[11, 12]
[1[1, 2]]

3.list转map并排序

list排序参照java8排序 java8 list转Map报错Collectors.toMap :: results in "Non-static method cannot be refernced from static context"

TreeMap<String, String> collect1 = list.stream()
                .collect(Collectors.toMap(Dish::getName, o -> "", (key1, key2) -> key2, TreeMap<String, String>::new));
System.out.println(collect1);

输出:{aa=, bb=, cc=, dd=, ee=, ff=, gg=}

4.list转linkedList

LinkedList<Dish> collect2 = list.stream().collect(Collectors.toCollection(LinkedList::new));

输出:[Dish(calories=1, name=aa), Dish(calories=13, name=bb), Dish(calories=7, name=cc), Dish(calories=6, name=aa), Dish(calories=9, name=dd), Dish(calories=1, name=ee), Dish(calories=3, name=aa), Dish(calories=1, name=ff), Dish(calories=1, name=gg)]

5.filter,map,sorted,limit,collect的使用

List<String> collect = list.stream()
                .filter(d ->d.getCalories() < 9)
                .sorted(Comparator.comparing(Dish::getCalories).reversed())
                .map(d->d.getName())
                .limit(3)

输出:[cc, aa, aa]

打印日志:

        List<String> collect = list.stream()
                .filter(d ->{
                                System.out.println("filter "+d.getName());
                        return d.getCalories() < 9;})
                .sorted(Comparator.comparing(Dish::getCalories).reversed())
                .map(d->{
                    System.out.println("map "+d.getName());
                    return d.getName();
                })
                .limit(3)
                .collect(Collectors.toList());
System.out.println(collect);
 

输出:

filter aa
filter bb
filter cc
filter aa
filter dd
filter ee
filter aa
filter ff
filter gg
map cc
map aa
map aa
[cc, aa, aa]

 

java8 stream编程

原文:https://www.cnblogs.com/pu20065226/p/11483698.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!