java8 stream用法快速上手版

1,从一个集合里面提取对象里面一个字段生成一个新的集合

list.stream().map(SimpleRechargeInfo::getRechargeAccount).collect(Collectors.toList());


2.List to Map
Map productInfosMap= productInfos.stream().collect(Collectors.toMap(ProductInfo::getProductId, ProductInfo::getProductName));

4,简单的过滤和循环
users.stream().filter(a->a.getAge()>5).forEach(p->{
System.out.println(p.getName());
});

5. 输出的元素的顺序与我放入的顺序是一致的。这就是第一种方式:使用 Stream.of() 方法。
UserInfo[] users2={new UserInfo("1",1),new UserInfo("2",2)};
Stream users=Stream.of(users2);
users.forEach(p->{
System.out.println(p.getName());
});

你可能感兴趣的:(java8,stream用法快速上手版)