Stream流
package com.scfsoft.test.test;
import cn.hutool.core.lang.Console;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
public class Test {
public static void main(String[] args) {
//构造100个user对象
ArrayList<User> users = new ArrayList<>();
for (int i = 0; i < 100; i++) {
Random random = new Random();
int i1 = random.nextInt(2);
String sex ="";
if (i1==0){
sex="男";
}else{
sex = "女";
}
users.add(new User(i,"test"+i,i,sex));
}
ArrayList<Score> scores = new ArrayList<>();
for (int i = 0; i < 100; i++) {
Random random = new Random();
double v1 = random.nextInt(100);
double v2 = random.nextInt(100);
double v3 = random.nextInt(100);
scores.add(new Score(i,v1,v2,v3));
}
ArrayList<Info> infos = new ArrayList<>();
for (int i = 0; i < 100; i++) {
infos.add(new Info(i,i,i+"班",scores));
}
/**
*filter
*找出所有年龄大于20岁小于30岁的女性
*/
List<User> users1 = users.stream()
.filter(x -> x.getAge() > 20 && x.getAge() < 30)
.filter(b -> "女".equals(b.getSex()))
.collect(Collectors.toList());
System.out.println(users1);
/**
* map mapping
* 找出所有年龄大于20岁小于30岁的女性,只取姓名
*/
List<String> users2 =users.stream()
.filter(x -> x.getAge() > 20 && x.getAge() < 30)
.filter(b -> "女".equals(b.getSex()))
.map(User::getName)
.collect(Collectors.toList());
System.out.println(users2);
//方法2
List<String> users3 = users.stream()
.filter(x -> x.getAge() > 20 && x.getAge() < 30)
.filter(b -> "女".equals(b.getSex()))
.collect(Collectors.mapping(User::getName, Collectors.toList()));
System.out.println(users3);
/**
* 无线流上下游写法
* 先找出性别为男的用户
* 按照年龄倒叙排序
* 跳过2位
* 取前10位
* 最后将这些用户分别求出他们的总成绩、按照成绩高低排序
*/
users.stream()
.filter(x->"男".equals(x.getSex()))
.sorted(Comparator.comparing(User::getAge).reversed())
.skip(2)
.limit(10)
.collect(Collectors.collectingAndThen(Collectors.toList(),list->{
list.stream().collect(
Collectors.collectingAndThen(
Collectors.groupingBy(User::getId),map->{
map.forEach((k,v)->{
List<Double> collect = scores.stream()
.filter(x -> k.equals(x.getUid()))
.map(c -> c.getChinese() + c.getHistory() + c.getMath())
.collect(Collectors.toList());
System.out.println(collect);
});
return map;
}));
return list;
}));
}
}
user实体类
package com.scfsoft.test.test;
public class User {
private Integer id;
private String name;
private Integer age;
private String sex;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public User() {
}
public User(Integer id, String name, Integer age, String sex) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
'}';
}
}
package com.scfsoft.test.test;
import java.util.List;
public class Info {
private Integer mid;
private Integer uid;
private String className;
private List<Score> scoreList;
public Integer getMid() {
return mid;
}
public void setMid(Integer mid) {
this.mid = mid;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public List<Score> getScoreList() {
return scoreList;
}
public void setScoreList(List<Score> scoreList) {
this.scoreList = scoreList;
}
public Info() {
}
public Info(Integer mid, Integer uid, String className, List<Score> scoreList) {
this.mid = mid;
this.uid = uid;
this.className = className;
this.scoreList = scoreList;
}
@Override
public String toString() {
return "Info{" +
"mid=" + mid +
", uid=" + uid +
", className='" + className + '\'' +
", scoreList=" + scoreList +
'}';
}
}
package com.scfsoft.test.test;
public class Score {
private Integer uid;
private Double chinese;
private Double history;
private Double math;
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public Double getChinese() {
return chinese;
}
public void setChinese(Double chinese) {
this.chinese = chinese;
}
public Double getHistory() {
return history;
}
public void setHistory(Double history) {
this.history = history;
}
public Double getMath() {
return math;
}
public void setMath(Double math) {
this.math = math;
}
public Score() {
}
public Score(Integer uid, Double chinese, Double history, Double math) {
this.uid = uid;
this.chinese = chinese;
this.history = history;
this.math = math;
}
@Override
public String toString() {
return "Score{" +
"uid=" + uid +
", chinese=" + chinese +
", history=" + history +
", math=" + math +
'}';
}
}