Strem流
生成Stream流
//Collection
List<String> list = new Arraylist<String>();
list.stream();
//Map
Map<String,Object> map = new HashMap<String,Object>
map.keySet().stream();
map.values().stream();
map.entrySet().stream();
//数组
Stream.of(数组)
Stream.of("d","g","g","g","g")
中间操作
filter
List.stream().filter(s->s.length()==3);//Predicate函数式接口
limit
List.stream().limit(3);//获取前面多少个
skip
List.stream().skip(3);//跳过前面多少个
concat
Stream.concat(stream1,stream2);//合并两个Stream流
distinct
List.stream().distinct();//Stream流去重
sorted
List.stream().sorted();//无参按自然顺序排序 有参实现Comparator接口
map
List.stream().map(s -> _new _Actor(s));//Function函数式接口 返回给定函数结果组成的流
mapToInt
List.stream().mapToInt(Integer::parseInt);//返回一个IntStream其中包含给定函数结果组成的流
IntStream:表示原始int流
终结操作
forEach
List.stream().forEach(System.out::println);//Consumer函数式接口对此流的每个元素执行操作
count
List.stream().count();//返回此流中的元素数
收集操作
List.stream().collect(Collector collector)
Collector接口 有一个工具类 Collectors
public static
public static
public static Collector toMap(Function keyMapper,Function valueMapper):把元素收集到Map集合中
函数式接口
函数式接口注解标识:@FunctionalInterface
Supplier
Supplier
●T get():获得结果
●该方法不需要参数,它会按照某种实现逻辑(由Lambda表达式实现)返回一个数据
●Supplier
Consumer
Consumer
●void accept(T t):对给定的参数执行此操作
●default Consumer
●Consumer
Predicate
Predicate
●boolean test(T t):对给定的参数进行判断(判逻辑由Lambda表达式实现),返回一个布尔值
●default Predicate
●default Predicate
●default Predicate
●Predicate
Function
Function
●R apply(T t):将此函数应用于给定的参数
●default
●Function