Java8新特性:Stream常用方法
作者:互联网
前言
这里创建一个商品实体类Goods
用于演示stream的常用方法(如下)
import java.util.ArrayList;
import java.util.List;
public class Main {
List<Goods> list = new ArrayList<Goods>(){
{
add(new Goods(1, "女子减震休闲鞋", "鞋子", 999.00));
add(new Goods(2, "星球大战联名系列男子宽松短袖", "衣服", 123.00));
add(new Goods(3, "板鞋女鞋2021秋冬季新", "鞋子", 456.00));
add(new Goods(4, "超轻17男子轻质透气网面运动鞋", "鞋子", 894.00));
add(new Goods(5, "一体织反光速干凉爽短袖T恤", "衣服", 123.00));
add(new Goods(6, "运动短裤男裤2021春夏新品", "裤子", 452.00));
add(new Goods(7, "拖鞋女鞋夏季新品", "鞋子", 789.00));
add(new Goods(8, "运动裤卫裤", "裤子", 231.00));
add(new Goods(9, "小雏菊经典情侣高帮", "鞋子", 652.00));
}
};
}
class Goods {
private int id;
private String name;
private String type;
private double price;
public Goods(int id, String name, String type, double price) {
this.id = id;
this.name = name;
this.type = type;
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
1.过滤
1.1.filter(筛选)
// 筛选类型为“衣服“的商品
List<Goods> result = list.stream().filter(goods -> "衣服".equals(goods.getType())).collect(Collectors.toList());
// 打印结果
result.forEach(goods -> System.out.print(goods.getName() + "\t"));
1.2.distinct(去重)
// 获取商品所有得价格并且去重
List<Double> result = list.stream().map(Goods::getPrice).distinct().collect(Collectors.toList());
1.3.limit
// 返回类型为鞋子得商品的前三个,如果符合条件的商品不足三个,则返回所有的商品
List<Goods> result = list.stream().filter(goods -> "鞋子".equals(goods.getType())).limit(3).collect(Collectors.toList());
1.4.sorted(排序)
// 按照商品价格排序,从小到大g1-g2,从大到小g2-g1
List<Goods> reuslt = list.stream().sorted((g1, g2) -> (int) (g1.getPrice() - g2.getPrice())).collect(Collectors.toList());
1.5.skip
// 跳过前两个商品(忽略前两个商品)
List<Goods> result = list.stream().skip(2).collect(Collectors.toList());
2.查找
2.1.allMatch(需要全部满足)
// 如果所有的商品满足条件则返回true,否则返回false
boolean flag = list.stream().allMatch(goods -> goods.getPrice() > 0);
2.2.anyMatch(满足一个即可)
// 只要存在满足条件的商品就返回true,否则返回false
boolean flag = list.stream().anyMatch(goods -> goods.getPrice() > 0);
2.3.noneMatch
// 判断是否不存在满足指定条件的商品
// 如下,不存在价格为998的商品,因此返回true
boolean flag = list.stream().noneMatch(goods -> goods.getPrice() == 998);
2.4.findFirst(返回第一个符合条件的数据)
// 返回第一个符合条件的数据
Goods result = list.stream().filter(goods -> goods.getPrice() > 0).findFirst().get();
3.收集
3.1.counting(计算个数)
// 计算个数,可以配合filter等方法使用
long result1 = list.stream().collect(Collectors.counting());
// 简化版本
long result2 = list.stream().count();
3.2.maxBy,minBy
// 方法一
double min1 = list.stream().min((g1, g2) -> (int) (g1.getPrice() - g2.getPrice())).get().getPrice();
double max1 = list.stream().max((g1, g2) -> (int) (g1.getPrice() - g2.getPrice())).get().getPrice();
// 方法二
double min2 = list.stream().collect(Collectors.minBy(Comparator.comparing(Goods::getPrice))).get().getPrice();
double max2 = list.stream().collect(Collectors.maxBy(Comparator.comparing(Goods::getPrice))).get().getPrice();
3.3.计算总和
// int使用summingInt,double使用summingDouble,long使用summingLong
double result = list.stream().collect(Collectors.summingDouble(Goods::getPrice));
3.4.计算平均值
double result = list.stream().collect(Collectors.averagingDouble(Goods::getPrice));
3.5.分组
// 按照商品类型进行分组
Map<String, List<Goods>> result = list.stream().collect(Collectors.groupingBy(Goods::getType));
3.6.字符串拼接
// 将所有商品名称拼接,名称之间使用”,“分隔
String result = list.stream().map(Goods::getName).collect(Collectors.joining(","));
标签:Goods,Stream,stream,list,特性,collect,goods,getPrice,Java8 来源: https://blog.csdn.net/weixin_47600880/article/details/122004871