演示类:
package com;
import lombok.Data;
import java.math.BigDecimal;
public class User {
//姓名
private String name;
//年龄
private Integer age;
//性别
private Integer sex;
//地址
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public BigDecimal getMoney() {
return money;
}
public void setMoney(BigDecimal money) {
this.money = money;
}
//赏金
private BigDecimal money;
public User(String name, Integer age, Integer sex, String address,BigDecimal money) {
this.name = name;
this.age = age;
this.sex = sex;
this.address = address;
this.money = money;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", sex=" + sex +
", money=" + money +
", address='" + address + '\'' +
'}';
}
}
静态方法添加数据:
public static List<User> users(){
List<User> list = Arrays.asList(
new User("李星云", 18, 0, "渝州",new BigDecimal(1000)),
new User("陆林轩", 16, 1, "渝州",new BigDecimal(500)),
new User("姬如雪", 17, 1, "幻音坊",new BigDecimal(800)),
new User("袁天罡", 99, 0, "藏兵谷",new BigDecimal(100000)),
new User("张子凡", 19, 0, "天师府",new BigDecimal(900)),
new User("陆佑劫", 45, 0, "不良人",new BigDecimal(600)),
new User("张天师", 48, 0, "天师府",new BigDecimal(1100)),
new User("蚩梦", 18, 1, "万毒窟",new BigDecimal(800))
);
return list;
}
过滤方法:
/**年龄大于20的*/
List<User> collect = users().stream().filter(user -> user.getAge() > 20).collect(Collectors.toList());
System.out.println(collect);
user -> user.getAge()>20直接跟条件,不需要写{}作为方法体
去重方法:
List<User> collect1 = users().stream().distinct().collect(Collectors.toList());
去重方法不能做多余的判断,只能去除完全重复的数据,而且对象必须要重写hash和equals方法
排序方法:
List<User> collect2 = users().stream().sorted(Comparator.comparing(user -> user.getAge())).collect(Collectors.toList());
可以通过Comparator.comparing(user -> user.getAge()写lambe表达式限定条件
显示前几个数据:
List<User> collect3 = users().stream().limit(2).collect(Collectors.toList());
这里是只取出前两个数据limit(2),根据数字判定
去除前几个数据:
List<User> collect4 = users().stream().skip(2).collect(Collectors.toList());
和过滤排序搭配有奇效,使用方法同上
批量操作子元素内容再整合成新的集合:
List<User> collect5 = users().stream().map(user -> {
user.setAddress("渝州");
return user;
}).collect(Collectors.toList());
这个是映射出子元素再批量操作,需要写返回值。
对集合所有元素进行判断是否都满足条件:
boolean match = users().stream().allMatch(user -> user.getAge() == 18);
allMatch(user -> user.getAge() == 18),括号内直接跟条件,返回boolen型的返回值
判断集合是否有满足条件的元素:
boolean match1 = users().stream().anyMatch(user -> user.getAge() == 18);
使用同上
判断集合是否不含有某个元素:
boolean match2 = users().stream().noneMatch(user -> user.getAddress() == "郑州");
使用同上
找到第一个元素:
Optional<User> first = users().stream().findFirst();
通过optional容器进行判断。
找到任意一个元素:
Optional<User> any = users().stream().findAny();
使用方法同上
计算集合总数:
long count = users().stream().count();
输出集合的元素个数
最大最小值:
Optional<User> max = users().stream().max(Comparator.comparing(user -> user.getAge()));
Optional<User> min = users().stream().min(Comparator.comparing(user -> user.getAge()));
计算指定属性的最大最小值
一次性得到元素的最大,最小,平均数,个数,总和:
IntSummaryStatistics collect6 = users().stream().collect(Collectors.summarizingInt(user -> user.getAge()));
这种简单一些,可以通过collect6.getxxx,再获取对应的结果。
将元素的属性进行拼接:
String collect7 = users().stream().map(User::getName).collect(Collectors.joining(","));
输出结果就是字符串的拼接。且只能字符串拼接
求和,平均数:
Double collect8 = users().stream().collect(Collectors.averagingInt(User::getAge));
Integer collect9 = users().stream().collect(Collectors.summingInt(User::getAge));
其中Collectors中的方法可以单独求和,平均数,个数之类的
分组和条件分组:
Map<Integer, List<User>> collect10 = users().stream().collect(Collectors.groupingBy(User::getSex));
Map<Boolean, List<User>> collect11 = users().stream().collect(Collectors.groupingBy(user -> user.getAge() > 20));
第一种按照元素分组,第二种按照条件分组,不符合条件为flase{分组结果},符合条件为true{结果}
分组合计:
Map<Integer, Long> collect12 = users().stream().collect(Collectors.groupingBy(User::getSex, Collectors.counting()));
User::getSex是分组条件, Collectors.counting()是求出当前条件下的分组数量。
分区:
Map<Boolean, List<User>> collect13 = users().stream().collect(Collectors.partitioningBy(user -> user.getAge() > 20));
分组和分区基本差不多。
构建集合:
List<String> list = new ArrayList<>();
list.add("常宣灵,常昊灵");
list.add("孟婆,判官红,判官蓝");
List<String> collect14 = list.stream().map(s -> s.split(",")).flatMap(Arrays::stream).collect(Collectors.toList());
flatMap比较特殊,只能操作steam
stream流经常是组合一起使用的下面是全部代码示例:
总代码示例:
package com;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
public class hxzn {
public static void main(String[] args) {
/**年龄大于20的*/
List<User> collect = users().stream().filter(user -> user.getAge() > 20).collect(Collectors.toList());
System.out.println(collect);
System.out.println("==============================");
/**去重,两个渝州的不显示*/
List<User> collect1 = users().stream().distinct().collect(Collectors.toList());
System.out.println(collect1);
System.out.println("==============================");
/**排序方法*/
List<User> collect2 = users().stream().sorted(Comparator.comparing(user -> user.getAge())).collect(Collectors.toList());
System.out.println(collect2);
System.out.println("==============================");
/**分段显示*/
List<User> collect3 = users().stream().limit(2).collect(Collectors.toList());
System.out.println(collect3);
System.out.println("==============================");
/**去除钱几个元素*/
List<User> collect4 = users().stream().skip(2).collect(Collectors.toList());
System.out.println(collect4);
System.out.println("==============================");
/**操作子元素*/
List<User> collect5 = users().stream().map(user -> {
user.setAddress("渝州");
return user;
}).collect(Collectors.toList());
System.out.println(collect5);
System.out.println("==============================");
/**对集合所有元素进行判断是否全部满足条件*/
boolean match = users().stream().allMatch(user -> user.getAge() == 18);
System.out.println(match);
System.out.println("==============================");
/**判断集合是否有满足条件的元素*/
boolean match1 = users().stream().anyMatch(user -> user.getAge() == 18);
System.out.println(match1);
System.out.println("==============================");
/**判断是否含有某个元素*/
boolean match2 = users().stream().noneMatch(user -> user.getAddress() == "郑州");
System.out.println(match2);
System.out.println("==============================");
/**找到第一个元素*/
Optional<User> first = users().stream().findFirst();
System.out.println(first);
System.out.println("==============================");
/**找到任意一个元素*/
Optional<User> any = users().stream().findAny();
System.out.println(any);
System.out.println("==============================");
/**计算集合总数*/
long count = users().stream().count();
System.out.println(count);
System.out.println("==============================");
/**最大值最小值*/
Optional<User> max = users().stream().max(Comparator.comparing(user -> user.getAge()));
Optional<User> min = users().stream().min(Comparator.comparing(user -> user.getAge()));
System.out.println(max);
System.out.println(min);
System.out.println("==============================");
/**得到元素的个数,总和,最大,最小*/
IntSummaryStatistics collect6 = users().stream().collect(Collectors.summarizingInt(user -> user.getAge()));
System.out.println(collect6);
System.out.println("==============================");
/**拼接元素*/
String collect7 = users().stream().map(User::getName).collect(Collectors.joining(","));
System.out.println(collect7);
System.out.println("==============================");
/**求和平均值*/
Double collect8 = users().stream().collect(Collectors.averagingInt(User::getAge));
Integer collect9 = users().stream().collect(Collectors.summingInt(User::getAge));
System.out.println(collect8);
System.out.println(collect9);
System.out.println("==============================");
/**分组*/
Map<Integer, List<User>> collect10 = users().stream().collect(Collectors.groupingBy(User::getSex));
Map<Boolean, List<User>> collect11 = users().stream().collect(Collectors.groupingBy(user -> user.getAge() > 20));
System.out.println(collect10);
System.out.println(collect11);
System.out.println("==============================");
/**分组合计*/
Map<Integer, Long> collect12 = users().stream().collect(Collectors.groupingBy(User::getSex, Collectors.counting()));
System.out.println(collect12);
System.out.println("==============================");
/**分区*/
Map<Boolean, List<User>> collect13 = users().stream().collect(Collectors.partitioningBy(user -> user.getAge() > 20));
System.out.println(collect13);
System.out.println("==============================");
/**构建集合*/
List<String> list = new ArrayList<>();
list.add("常宣灵,常昊灵");
list.add("孟婆,判官红,判官蓝");
List<String> collect14 = list.stream().map(s -> s.split(",")).flatMap(Arrays::stream).collect(Collectors.toList());
System.out.println(collect14);
}
public static List<User> users(){
List<User> list = Arrays.asList(
new User("李星云", 18, 0, "渝州",new BigDecimal(1000)),
new User("陆林轩", 16, 1, "渝州",new BigDecimal(500)),
new User("姬如雪", 17, 1, "幻音坊",new BigDecimal(800)),
new User("袁天罡", 99, 0, "藏兵谷",new BigDecimal(100000)),
new User("张子凡", 19, 0, "天师府",new BigDecimal(900)),
new User("陆佑劫", 45, 0, "不良人",new BigDecimal(600)),
new User("张天师", 48, 0, "天师府",new BigDecimal(1100)),
new User("蚩梦", 18, 1, "万毒窟",new BigDecimal(800))
);
return list;
}
}