private static List build() {
return Lists.newArrayList(
new ShopCar(“苹果”, 3.3, 10),
new ShopCar(“香蕉”, 4.4, 20),
new ShopCar(“橘子”, 5.5, 30)
);
}
public static void main(String[] args) {
List shopCarList = build();
//过滤
filter(shopCarList);
//排序
sort(shopCarList);
//统计
sum(shopCarList);
//聚合
collect(shopCarList);
}
private static void filter(List shopCarList) {
//移除集合中的元素
shopCarList.removeIf(s -> s.getPrice() > 4);
//过滤出数量大于0的,然后将名字拼接成字符串
String names = shopCarList.stream().filter(s -> s.getCount() > 0).map(ShopCar::getName).collect(Collectors.joining(“、”));
//只能去完全相同的实体
List shopCarList1 = shopCarList.stream().distinct().collect(Collectors.toList());
//去按指定参数去重
List shopCarList2 = shopCarList.stream().filter(distinctByK