版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_38278330/article/details/79273011
直接上代码:
package com.test.lambda;
import java.util.*;
import java.util.stream.Collectors;
public class StreamTest {
public static void main(String[] args) {
List<Integer> list = Arrays.asList( 11,23,4,5,667 );
list.forEach((it)-> System.out.print (it*2+" ")); //forEach
System.out.println(list.stream().filter((it)->it>5).collect(Collectors.toList())); //filter 过滤, 再collect还原为list
System.out.println(list.stream().map((it)-> String.valueOf(it*2)).collect(Collectors.toList())); //map 映射为String,再collect
System.out.println(list.stream().noneMatch(it->it>900)); //是否都不符合,返回boolean
System.out.println(list.stream().anyMatch(it->it<500)); //是否任意一个符合,返回boolean
/**
* List 映射为Map
* 第一个lambda生成一个新的HashMap Supplier
* 第二个方法中第一个参数是前面生成的HashMap对象,第二个参数是stream中包含的元素,
* 方法体就是把stream中的元素加入HashMap对象中。第二个方法被反复调用直到原stream的元素被消费完毕; BiConsumer
* 第三个方法也是接受两个参数,这两个都是HashMap类型的,
* 方法体就是把第二个HashMap全部加入到第一个中; BiConsumer
*/
Map<Integer,String> map = list.stream().collect(HashMap::new ,(entity,it)-> entity.put(it,String.valueOf(it*2)) ,(my,other)->my.putAll(other));
System.out.println(map);
/**
* 传Collector
* Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper)
*/
Map<Integer,String> map2 = list.stream().collect(Collectors.toMap(it->it,String::valueOf));
System.out.println(map2);
System.out.println(list.stream().max(Integer::compareTo)); //选出最大值
/**
* 叠加,第二个参数为accumulator,计算的累加器 类型 BinaryOperator<T>
* 其函数式方法 R apply(T t, U u) 中:
* 第一个参数t为上次函数计算的返回值,第二个参数u为Stream中的元素
* 这个函数把这两个值计算apply,得到的和会被赋值给下次执行这个方法的第一个参数
*/
System.out.println(list.stream().reduce(100,(total,it)->total+it)); //提供了初始值,返回<T>
System.out.println(list.stream().reduce((total,it)->total+it)); //未提供初始值,返回Optional<T>
Optional<String> opt = list.stream().map(String::valueOf).reduce( (s1, s2)->s1+"_"+s2); //类似于join,返回的是Optional<R> 取决于map怎么转化的
if(opt.isPresent()){
System.out.println(opt.get());
}
String st = list.stream().map(String::valueOf).collect(Collectors.joining(",")); //直接的join
System.out.println(st);
Optional<String> opt2 = opt.map(it->it+"LALALA");
opt2.ifPresent(it-> System.out.println(it)); //直接处理,传入一个 Consumer<? super T> consumer
//opt2.ifPresent(System.out::println); 同上
//MAP test
Map<Integer,String> idInfo = new HashMap<>();
idInfo.put(1,"lili");
idInfo.put(5,"kekeke");
idInfo.put(7,"test");
idInfo.forEach((key,value)-> System.out.println("key is "+key+" value is "+value)); //简单遍历\
//先看 map中 key是否为null ,如果不是,则跳过;为null则计算值,如果计算值不为null, put进去
list.forEach(it->idInfo.computeIfAbsent(it,key-> key+" Map.computeIfAbsent")); //
System.out.println(idInfo);
}
}
结果:
22 46 8 10 1334 [11, 23, 667]
[22, 46, 8, 10, 1334]
true
true
{4=8, 5=10, 23=46, 11=22, 667=1334}
{4=4, 5=5, 23=23, 667=667, 11=11}
Optional[667]
810
Optional[710]
11_23_4_5_667
11,23,4,5,667
11_23_4_5_667LALALA
key is 1 value is lili
key is 5 value is kekeke
key is 7 value is test
{1=lili, 4=4 Map.computeIfAbsent, 5=kekeke, 23=23 Map.computeIfAbsent, 7=test, 667=667 Map.computeIfAbsent, 11=11 Map.computeIfAbsent}
感觉用起来也就那样了,附一个Groovy版本的,神清气爽。
package com.test.lambda
class Test {
static void main(String[] args) {
def list = [11,23,4,5,667]
list.each { print it*2+" "}
println list.grep{it>5}.collect{it}
println list.collect{(it*2).toString()}
println !list.any{it > 900}
//println list.every{ it > 10} //每个都要符合
println list.any{it < 500}
def map = list.collectEntries{[it, (it*2).toString()]}
println map
def map2 = list.collectEntries{[it,it.toString()]}
println map2
println list.max()
println list.inject(100){total,it->total+=it}
println list.inject {total,it->total+=it}
println list.join(",")
def idInfo = [1:'lili',5:"kekeke",7:"test"]
idInfo.each {key,value->
println "key is ${key} value is ${value}"
}
//暂时没找到类似的Groovy方法,但是Groovy可以使用java的语法,把Lambda传为闭包就行了
list.each {
idInfo.computeIfAbsent(it,{key->key+" created by JAVA8.Map.computeIfAbsent"})
}
println idInfo
}
}