Stream流的出现极大的便利的开发人员对一些数据的处理,提供了大量便利的API,Stream是对集合对象功能的增强。Stream 使用一种类似用 SQL 语句从数据库查询数据的直观方式来对 Java 集合运算和表达的高阶抽象。
Stream API
练习相关类Property
@Data
public class Property {
String name;
// 距离,单位:米
Integer distance;
// 销量,月售
Integer sales;
// 价格,这里简单起见就写一个级别代表价格段
Integer priceLevel;
public Property(String name, int distance, int sales, int priceLevel) {
this.name = name;
this.distance = distance;
this.sales = sales;
this.priceLevel = priceLevel;
}
// getter setter 省略
@Override
public String toString() {
return "Property{" +
"name='" + name + '\'' +
", distance=" + distance +
", sales=" + sales +
", priceLevel=" + priceLevel +
'}';
}
}
public static void main(String[] args) {
Property p1 = new Property("叫了个鸡", 1000, 500, 2);
Property p2 = new Property("张三丰饺子馆", 2300, 1500, 3);
Property p3 = new Property("永和大王", 580, 3000, 1);
Property p4 = new Property("肯德基", 6000, 200, 4);
Property p5 = new Property("肯德基", 6000, 200, 5);
List<Property> list = Arrays.asList(p1, p2, p3, p4, p5);
// 遍历list集合中的元素
list.stream().forEach(listItem -> {
System.out.println(listItem);
});
}
public static void main(String[] args) {
Property p1 = new Property("叫了个鸡", 1000, 500, 2);
Property p2 = new Property("张三丰饺子馆", 2300, 1500, 3);
Property p3 = new Property("永和大王", 580, 3000, 1);
Property p4 = new Property("肯德基", 6000, 200, 4);
Property p5 = new Property("肯德基", 6000, 200, 5);
List<Property> list = Arrays.asList(p1, p2, p3, p4, p5);
// 筛选出sales大于400数据并输出
list.stream().filter(s -> s.sales > 400).forEach(x -> {
System.out.println(x);
});
}
public static void main(String[] args) {
Property p1 = new Property("叫了个鸡", 1000, 500, 2);
Property p2 = new Property("张三丰饺子馆", 2300, 1500, 3);
Property p3 = new Property("永和大王", 580, 200, 1);
Property p4 = new Property("肯德基", 6000, 200, 4);
List<Property> list = Arrays.asList(p1, p2, p3, p4);
System.out.println("============================================================");
// 按照sales属性进行排序进行从小到大排序
list.stream().sorted(Comparator.comparing(Property::getSales)).forEach(t -> {
System.out.println(t);
});
System.out.println("============================================================");
// 按照sales属性进行排序进行降序排序
list.stream().sorted(Comparator.comparing(Property::getSales).reversed()).forEach(t -> {
System.out.println(t);
});
System.out.println("============================================================");
// 多条件排序
// 先按照sales升序,一致在按照priceLevel升序
list.stream().sorted(Comparator.comparing(Property::getSales).thenComparing(Property::getPriceLevel)).forEach(t -> {
System.out.println(t);
});
}
public static void main(String[] args) {
Property p1 = new Property("叫了个鸡", 1000, 500, 2);
Property p2 = new Property("张三丰饺子馆", 2300, 1500, 3);
Property p3 = new Property("永和大王", 580, 200, 1);
Property p4 = new Property("肯德基", 6000, 200, 4);
List<Property> list = Arrays.asList(p1, p2, p3, p4);
// 返回对应数量数据
list.stream().limit(2).forEach(s -> {
System.out.println(s);
});
}
public static void main(String[] args) {
Property p1 = new Property("叫了个鸡", 1000, 500, 2);
Property p2 = new Property("张三丰饺子馆", 2300, 1500, 3);
Property p3 = new Property("永和大王", 580, 200, 1);
Property p4 = new Property("肯德基", 6000, 200, 4);
Property p5 = new Property("肯德基", 6000, 200, 4);
List<Property> list = Arrays.asList(p1, p2, p3, p4,p5);
// 去除相同元素
list.stream().distinct().forEach(s -> {
System.out.println(s);
});
}
public static void main(String[] args) {
Property p1 = new Property("叫了个鸡", 1000, 500, 2);
Property p2 = new Property("张三丰饺子馆", 2300, 1500, 3);
Property p3 = new Property("永和大王", 580, 200, 1);
Property p4 = new Property("肯德基", 6000, 200, 4);
Property p5 = new Property("肯德基", 6000, 200, 4);
List<Property> list = Arrays.asList(p1, p2, p3, p4,p5);
// 从指定位置元素之后开始
list.stream().skip(3).forEach(s -> {
System.out.println(s);
});
}
public static void main(String[] args) {
Property p1 = new Property("叫了个鸡", 1000, 500, 2);
Property p2 = new Property("张三丰饺子馆", 2300, 1500, 3);
Property p3 = new Property("永和大王", 580, 200, 1);
Property p4 = new Property("肯德基", 6000, 200, 4);
Property p5 = new Property("肯德基", 6000, 200, 4);
List<Property> list = Arrays.asList(p1, p2, p3, p4, p5);
// 取出队首元素
System.out.println(list.stream().findFirst().get());
// 随机取出一个元素parallelStream需要在并行流下,使用stream一般会取出第一个
System.out.println(list.parallelStream().findAny().get());
}
public static void main(String[] args) {
Property p1 = new Property("叫了个鸡", 1000, 500, 2);
Property p2 = new Property("张三丰饺子馆", 2300, 1500, 3);
Property p3 = new Property("永和大王", 580, 200, 1);
Property p4 = new Property("肯德基", 6000, 200, 4);
Property p5 = new Property("肯德基", 6000, 200, 4);
List<Property> list = Arrays.asList(p1, p2, p3, p4, p5);
// 判断是否有 叫了个鸡 这家店
System.out.println(list.stream().anyMatch(s -> s.getName().equals("叫了个鸡")));
// 判断是否有门店Distance小于5000
System.out.println(list.stream().anyMatch(s -> s.getDistance() < 2000));
// 判断是否有 叫了个鸭 这家店
System.out.println(list.stream().anyMatch(s -> s.getName().equals("叫了个鸭")));
// 判断是否有门店Distance小于500
System.out.println(list.stream().anyMatch(s -> s.getDistance() < 500));
}
public static void main(String[] args) {
Property p1 = new Property("叫了个鸡", 1000, 500, 2);
Property p2 = new Property("张三丰饺子馆", 2300, 1500, 3);
Property p3 = new Property("永和大王", 580, 200, 1);
Property p4 = new Property("肯德基", 6000, 200, 4);
Property p5 = new Property("肯德基", 6000, 200, 4);
List<Property> list = Arrays.asList(p1, p2, p3, p4, p5);
// 映射,也就是取出对应属性元素,返回类型也变成List
list.stream().map(s -> s.getName()).collect(Collectors.toList()).forEach(s -> {
System.out.println(s);
});
}
public static void main(String[] args) {
Property p1 = new Property("叫了个鸡", 1000, 500, 2);
Property p2 = new Property("张三丰饺子馆", 2300, 1500, 3);
Property p3 = new Property("永和大王", 580, 200, 1);
Property p4 = new Property("肯德基", 6000, 200, 4);
Property p5 = new Property("肯德基", 6000, 200, 4);
List<Property> list = Arrays.asList(p1, p2, p3, p4, p5);
// 可以把list类型转换成我们想要的类型
List<Property> collect = list.stream().filter(s -> s.getSales() > 400).collect(Collectors.toList());
Set<Property> collect1 = list.stream().filter(s -> s.getSales() > 400).collect(Collectors.toSet());
Map<String, Integer> collect2 = list.stream().filter(s -> s.getSales() > 400).collect(Collectors.toMap(Property::getName, Property::getSales));
// 求Sales平均值
System.out.println(list.stream().collect(Collectors.averagingDouble(Property::getSales)));
// 求PriceLevel最大值和最小值得店
System.out.println(list.stream().collect(Collectors.maxBy(comparing(Property::getPriceLevel))));
System.out.println(list.stream().collect(Collectors.minBy(comparing(Property::getPriceLevel))));
// 求和
System.out.println(list.stream().collect(Collectors.summingInt(Property::getPriceLevel)));
// 获取PriceLevel改列的和、最大值、最小值、平均值等信息
System.out.println(list.stream().collect(Collectors.summarizingInt(Property::getPriceLevel)));
}
public static void main(String[] args) {
Property p1 = new Property("叫了个鸡", 1000, 500, 2);
Property p2 = new Property("张三丰饺子馆", 2300, 1500, 3);
Property p3 = new Property("永和大王", 580, 200, 1);
Property p4 = new Property("肯德基", 6000, 200, 4);
Property p5 = new Property("肯德基", 6000, 200, 4);
List<Property> list = Arrays.asList(p1, p2, p3, p4, p5);
//对Sales求和
System.out.println(list.stream().map(Property::getSales).reduce(Integer::sum));
// 求Sales乘积
System.out.println(list.stream().map(Property::getPriceLevel).reduce((x, y) -> x * y));
}
以上只是Stream流里面很小的一部分,各位大佬有好的用法也欢迎在评论分享。