目录
一、遍历
1.属性遍历
2.对象遍历
3.遍历设值
二、过滤
1.简单过滤
2.多条件过滤
三、去重
四、统计
五、截取
六、跳过
七、排序
1.普通排序
2.指定排序
八、最值
1.集合确定不为空
2.集合可能为空
九、统计
十、List结构转换Map结构
1.map值是对象
2.map值是字段
十一、判断
1.anyMatch(只要有一个匹配则返回true)编辑
2.allMatch(需要全部匹配条件则返回true)
3.noneMatch(需要全部不匹配条件则返回true)
十二、转换
1.对象转成字段
2.对象转成对象
3.对象转成字符串
十三、分组
1.list结构分组成map结构(值是对象)
2.list结构分组成map结构(值是集合)
3.list结构分组成map结构(值是字段)
4.list结构分组成map结构(多条件分组)
5.list结构分组成二级list结构
package com.saas.demo;
import java.util.ArrayList;
import java.util.List;
/**
* Stream流遍历
*/
public class StreamDemo {
public static void main(String[] args) {
List list = new ArrayList<>();
list.add("小明");
list.add("小红");
list.add("小兰");
list.stream().forEach(s -> System.out.println("value:"+s));
}
}
package com.saas.demo;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
/**
* Stream流遍历
*/
public class StreamDemo {
public static void main(String[] args) {
List peopleList = new ArrayList();
peopleList.add(new People("小明",23,new BigDecimal("3000")));
peopleList.add(new People("小红",24,new BigDecimal("2800")));
peopleList.add(new People("小兰",22,new BigDecimal("3200")));
//遍历对象
peopleList.stream().forEach(s -> System.out.println("value:"+s));
System.out.println("------------------------------------------------");
//遍历属性
peopleList.stream().forEach(s -> System.out.println("value:"+s.getName()));
}
}
@Data
@AllArgsConstructor
class People {
//姓名
private String name;
//年龄
private Integer age;
//工资
private BigDecimal salary;
}
package com.saas.demo;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
/**
* Stream设值
*/
public class StreamDemo {
public static void main(String[] args) {
List peopleList = new ArrayList();
peopleList.add(new People("小明",23,new BigDecimal("3000")));
peopleList.add(new People("小红",24,new BigDecimal("2800")));
peopleList.add(new People("小兰",22,new BigDecimal("3200")));
//遍历对象
peopleList.stream().forEach(s -> System.out.println("value:"+s));
System.out.println("------------------------------------------------");
//统一更改年龄成25
peopleList.stream().forEach(s -> s.setAge(25));
//判断如果姓名叫小明的,则工资加100
peopleList.stream().forEach(s ->{
if("小明".equals(s.getName())){
s.setSalary(s.getSalary().add(new BigDecimal("100")));
}
});
//遍历对象
peopleList.stream().forEach(s -> System.out.println("value:"+s));
}
}
@Data
@AllArgsConstructor
class People {
//姓名
private String name;
//年龄
private Integer age;
//工资
private BigDecimal salary;
}
package com.saas.demo;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* Stream 过滤
*/
public class StreamDemo {
public static void main(String[] args) {
List peopleList = new ArrayList();
peopleList.add(new People("小明",23,new BigDecimal("3000")));
peopleList.add(new People("小红",24,new BigDecimal("2800")));
peopleList.add(new People("小兰",22,new BigDecimal("3200")));
//查找年龄大于等于23岁的人
List newPeopleList = peopleList.stream().filter(s ->s.getAge()>=23).collect(Collectors.toList());
System.out.println("value:"+newPeopleList);
}
}
@Data
@AllArgsConstructor
class People {
//姓名
private String name;
//年龄
private Integer age;
//工资
private BigDecimal salary;
}
package com.saas.demo;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* Stream 多条件过滤
*/
public class StreamDemo {
public static void main(String[] args) {
List peopleList = new ArrayList();
peopleList.add(new People("小明",23,new BigDecimal("3000")));
peopleList.add(new People("小红",24,new BigDecimal("2800")));
peopleList.add(new People("小兰",22,new BigDecimal("3200")));
//查找工资低于3000,年龄大于23岁的人
List newPeopleList1 = peopleList.stream().filter(s ->
s.getSalary().compareTo(new BigDecimal("3000"))<0 && s.getAge()>23
).collect(Collectors.toList());
System.out.println("value:"+newPeopleList1);
//查找年龄大于等于23岁并且工资不低于3000的人
List newPeopleList2 = peopleList.stream().filter(s ->{
if(s.getAge()>=23 && s.getSalary().compareTo(new BigDecimal("3000"))>=0){
return true;
}
return false;
}).collect(Collectors.toList());
System.out.println("value:"+newPeopleList2);
}
}
@Data
@AllArgsConstructor
class People {
//姓名
private String name;
//年龄
private Integer age;
//工资
private BigDecimal salary;
}
package com.saas.demo;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* Stream 去重
*/
public class StreamDemo {
public static void main(String[] args) {
List list = new ArrayList<>();
list.add("小明");
list.add("小红");
list.add("小兰");
list.add("小红");
list = list.stream().distinct().collect(Collectors.toList());
System.out.println("value:"+list);
}
}
package com.saas.demo;
import java.util.ArrayList;
import java.util.List;
/**
* Stream 统计
*/
public class StreamDemo {
public static void main(String[] args) {
List list = new ArrayList<>();
list.add("小明");
list.add("小红");
list.add("小兰");
long count = list.stream().count();
System.out.println("count:"+count);
}
}
package com.saas.demo;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* Stream 截取
*/
public class StreamDemo {
public static void main(String[] args) {
List list = new ArrayList<>();
list.add("小明");
list.add("小红");
list.add("小兰");
list = list.stream().limit(2).collect(Collectors.toList());
System.out.println("value:"+list);
}
}
package com.saas.demo;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* Stream 跳过
*/
public class StreamDemo {
public static void main(String[] args) {
List list = new ArrayList<>();
list.add("小明");
list.add("小红");
list.add("小兰");
//跳过前面两个
list = list.stream().skip(2).collect(Collectors.toList());
System.out.println("value:"+list);
}
}
package com.saas.demo;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
/**
* Stream 排序
*/
public class StreamDemo {
public static void main(String[] args) {
List peopleList = new ArrayList();
peopleList.add(new People("小明",23,new BigDecimal("3000")));
peopleList.add(new People("小红",24,new BigDecimal("2800")));
peopleList.add(new People("小兰",22,new BigDecimal("3200")));
//根据年龄从小到大排序
peopleList =peopleList.stream()
.sorted(Comparator.comparing(People::getAge))
.collect(Collectors.toList());
System.out.println("value:"+peopleList);
System.out.println("------------------------------------------------");
//根据年龄从大到小排序
peopleList =peopleList.stream()
.sorted(Comparator.comparing(People::getAge).reversed())
.collect(Collectors.toList());
System.out.println("value:"+peopleList);
}
}
@Data
@AllArgsConstructor
class People {
//姓名
private String name;
//年龄
private Integer age;
//工资
private BigDecimal salary;
}
package com.saas.demo;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
/**
* Stream 排序
*/
public class StreamDemo {
public static void main(String[] args) {
List peopleList = new ArrayList();
peopleList.add(new People("小明",23,new BigDecimal("3000")));
peopleList.add(new People("小红",24,new BigDecimal("2800")));
peopleList.add(new People("小兰",22,new BigDecimal("3200")));
System.out.println("value:"+peopleList);
System.out.println("------------------------------------------------");
//根据名称指定排序
List nameList = Arrays.asList("小红","小明","小兰");
peopleList = peopleList.stream().sorted(Comparator.comparing(People::getName,(x,y)->{
for (String name : nameList) {
if (name.equals(x) || name.equals(y)){
if (x.equals(y)){
return 0;
}else if (name.equals(x)){
return -1;
}else {
return 1;
}
}
}
return 0;
})).collect(Collectors.toList());
System.out.println("value:"+peopleList);
//根据年龄指定排序
List ageList = Arrays.asList(24,22,23);
Collections.sort(peopleList, new Comparator() {
public int compare(People p1, People p2) {
int x = ageList.indexOf(p1.getAge());
int y = ageList.indexOf(p2.getAge());
return x - y;
}
});
System.out.println("value:"+peopleList);
}
}
@Data
@AllArgsConstructor
class People {
//姓名
private String name;
//年龄
private Integer age;
//工资
private BigDecimal salary;
}
package com.saas.demo;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
/**
* Stream 获取最大值和最小值
*/
public class StreamDemo {
public static void main(String[] args) {
List peopleList = new ArrayList();
peopleList.add(new People("小明",23,new BigDecimal("3000")));
peopleList.add(new People("小红",24,new BigDecimal("2800")));
peopleList.add(new People("小兰",22,new BigDecimal("3200")));
//获取年龄最大的人
People maxPeople =peopleList.stream().max(Comparator.comparing(People::getAge)).get();
System.out.println("value:"+maxPeople);
System.out.println("------------------------------------------------");
//获取年龄最小的人
People minPeople =peopleList.stream().min(Comparator.comparing(People::getAge)).get();
System.out.println("value:"+minPeople);
}
}
@Data
@AllArgsConstructor
class People {
//姓名
private String name;
//年龄
private Integer age;
//工资
private BigDecimal salary;
}
package com.saas.demo;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
/**
* Stream 获取最大值和最小值
*/
public class StreamDemo {
public static void main(String[] args) {
List peopleList = new ArrayList();
peopleList.add(new People("小明",23,new BigDecimal("3000")));
peopleList.add(new People("小红",24,new BigDecimal("2800")));
peopleList.add(new People("小兰",22,new BigDecimal("3200")));
//获取年龄最大的人
Optional max =peopleList.stream().max(Comparator.comparing(People::getAge));
if(max.isPresent()){
System.out.println("value:"+max.get());
}
System.out.println("------------------------------------------------");
//获取年龄最小的人
Optional min =peopleList.stream().min(Comparator.comparing(People::getAge));
if(min.isPresent()) {
System.out.println("value:" + min.get());
}
}
}
@Data
@AllArgsConstructor
class People {
//姓名
private String name;
//年龄
private Integer age;
//工资
private BigDecimal salary;
}
package com.saas.demo;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
/**
* Stream 统计
*/
public class StreamDemo {
public static void main(String[] args) {
List peopleList = new ArrayList();
peopleList.add(new People("小明",23,new BigDecimal("3000"),new BigDecimal("1.1")));
peopleList.add(new People("小红",24,new BigDecimal("2800"),new BigDecimal("1.2")));
peopleList.add(new People("小兰",22,new BigDecimal("3200"),new BigDecimal("1.3")));
//统计年龄总和
int totalAge =peopleList.stream().mapToInt(People::getAge).sum();
System.out.println("value:"+totalAge);
System.out.println("------------------------------------------------");
//统计工资总和
BigDecimal totalSalary = peopleList.stream()
.map(People::getSalary)
.reduce(BigDecimal.ZERO,BigDecimal::add);
System.out.println("value:"+totalSalary);
//统计工资乘以各自系数的总和(向上保留两位)
BigDecimal totalRatioSalary = peopleList.stream()
.map(s->s.getSalary().multiply(s.getRatio()).setScale(2,BigDecimal.ROUND_HALF_UP))
.reduce(BigDecimal.ZERO,BigDecimal::add);
System.out.println("value:"+totalRatioSalary);
}
}
@Data
@AllArgsConstructor
class People {
//姓名
private String name;
//年龄
private Integer age;
//工资
private BigDecimal salary;
//比率
private BigDecimal ratio;
}
package com.saas.demo;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
/**
* Stream list转map
*/
public class StreamDemo {
public static void main(String[] args) {
List peopleList = new ArrayList();
peopleList.add(new People("小明",23,new BigDecimal("3000"),new BigDecimal("1.1")));
peopleList.add(new People("小红",24,new BigDecimal("2800"),new BigDecimal("1.2")));
peopleList.add(new People("小兰",22,new BigDecimal("3200"),new BigDecimal("1.3")));
//根据姓名转map
Map map= peopleList.stream()
.collect(Collectors.toMap(s-> s.getName(), object -> object,(oldValue, newValue) -> newValue));
System.out.println("value:"+map);
}
}
@Data
@AllArgsConstructor
class People {
//姓名
private String name;
//年龄
private Integer age;
//工资
private BigDecimal salary;
//比率
private BigDecimal ratio;
}
package com.saas.demo;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
/**
* Stream list转map
*/
public class StreamDemo {
public static void main(String[] args) {
List peopleList = new ArrayList();
peopleList.add(new People("小明",23,new BigDecimal("3000"),new BigDecimal("1.1")));
peopleList.add(new People("小红",24,new BigDecimal("2800"),new BigDecimal("1.2")));
peopleList.add(new People("小兰",22,new BigDecimal("3200"),new BigDecimal("1.3")));
//根据姓名转map
Map map = peopleList.stream().collect(Collectors.toMap(People::getName
, People::getAge));
System.out.println("value:"+map);
}
}
@Data
@AllArgsConstructor
class People {
//姓名
private String name;
//年龄
private Integer age;
//工资
private BigDecimal salary;
//比率
private BigDecimal ratio;
}
package com.saas.demo;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
/**
* Stream 判断
*/
public class StreamDemo {
public static void main(String[] args) {
List peopleList = new ArrayList();
peopleList.add(new People("小明",23,new BigDecimal("3000"),new BigDecimal("1.1")));
peopleList.add(new People("小红",24,new BigDecimal("2800"),new BigDecimal("1.2")));
peopleList.add(new People("小兰",22,new BigDecimal("3200"),new BigDecimal("1.3")));
//判断集合里面是否超过23岁的人
boolean flag= peopleList.stream().anyMatch(s -> s.getAge() > 23);
System.out.println("value:"+flag);
}
}
@Data
@AllArgsConstructor
class People {
//姓名
private String name;
//年龄
private Integer age;
//工资
private BigDecimal salary;
//比率
private BigDecimal ratio;
}
package com.saas.demo;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
/**
* Stream 判断
*/
public class StreamDemo {
public static void main(String[] args) {
List peopleList = new ArrayList();
peopleList.add(new People("小明",23,new BigDecimal("3000"),new BigDecimal("1.1")));
peopleList.add(new People("小红",24,new BigDecimal("2800"),new BigDecimal("1.2")));
peopleList.add(new People("小兰",22,new BigDecimal("3200"),new BigDecimal("1.3")));
//判断集合里面的人是否全部超过23岁
boolean flag= peopleList.stream().allMatch(s -> s.getAge() > 23);
System.out.println("value:"+flag);
}
}
@Data
@AllArgsConstructor
class People {
//姓名
private String name;
//年龄
private Integer age;
//工资
private BigDecimal salary;
//比率
private BigDecimal ratio;
}
package com.saas.demo;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
/**
* Stream 判断
*/
public class StreamDemo {
public static void main(String[] args) {
List peopleList = new ArrayList();
peopleList.add(new People("小明",23,new BigDecimal("3000"),new BigDecimal("1.1")));
peopleList.add(new People("小红",24,new BigDecimal("2800"),new BigDecimal("1.2")));
peopleList.add(new People("小兰",22,new BigDecimal("3200"),new BigDecimal("1.3")));
//判断集合里面的人是否全部不超过23岁
boolean flag= peopleList.stream().noneMatch(s -> s.getAge() > 23);
System.out.println("value:"+flag);
}
}
@Data
@AllArgsConstructor
class People {
//姓名
private String name;
//年龄
private Integer age;
//工资
private BigDecimal salary;
//比率
private BigDecimal ratio;
}
package com.saas.demo;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
/**
* Stream 转换
*/
public class StreamDemo {
public static void main(String[] args) {
List peopleList = new ArrayList();
peopleList.add(new People("小明",23,new BigDecimal("3000"),new BigDecimal("1.1")));
peopleList.add(new People("小红",24,new BigDecimal("2800"),new BigDecimal("1.2")));
peopleList.add(new People("小兰",22,new BigDecimal("3200"),new BigDecimal("1.3")));
//获取姓名集合
List nameList = peopleList.stream().map(s-> s.getName()).collect(Collectors.toList());
System.out.println("value:"+nameList);
}
}
@Data
@AllArgsConstructor
class People {
//姓名
private String name;
//年龄
private Integer age;
//工资
private BigDecimal salary;
//比率
private BigDecimal ratio;
}
package com.saas.demo;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
/**
* Stream 转换
*/
public class StreamDemo {
public static void main(String[] args) {
List peopleList = new ArrayList();
peopleList.add(new People("小明",23,new BigDecimal("3000"),new BigDecimal("1.1")));
peopleList.add(new People("小红",24,new BigDecimal("2800"),new BigDecimal("1.2")));
peopleList.add(new People("小兰",22,new BigDecimal("3200"),new BigDecimal("1.3")));
//对象转对象
List studentList = peopleList.stream().map(s->{
return new Student(s.getName(),s.getAge());
}).collect(Collectors.toList());
System.out.println("value:"+studentList);
}
}
@Data
@AllArgsConstructor
class People {
//姓名
private String name;
//年龄
private Integer age;
//工资
private BigDecimal salary;
//比率
private BigDecimal ratio;
}
@Data
@AllArgsConstructor
class Student {
//姓名
private String name;
//年龄
private Integer age;
}
package com.saas.demo;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
/**
* Stream 转换
*/
public class StreamDemo {
public static void main(String[] args) {
List peopleList = new ArrayList();
peopleList.add(new People("小明",23,new BigDecimal("3000"),new BigDecimal("1.1")));
peopleList.add(new People("小红",24,new BigDecimal("2800"),new BigDecimal("1.2")));
peopleList.add(new People("小兰",22,new BigDecimal("3200"),new BigDecimal("1.3")));
//获取姓名字符串,逗号隔开
String name = peopleList.stream().map(s-> s.getName()).collect(Collectors.joining(","));
System.out.println("value:"+name);
}
}
@Data
@AllArgsConstructor
class People {
//姓名
private String name;
//年龄
private Integer age;
//工资
private BigDecimal salary;
//比率
private BigDecimal ratio;
}
package com.saas.demo;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* Stream 分组
*/
public class StreamDemo {
public static void main(String[] args) {
List peopleList = new ArrayList();
peopleList.add(new People("小明",23,new BigDecimal("3200"),new BigDecimal("1.1"),"开发"));
peopleList.add(new People("小明",23,new BigDecimal("3100"),new BigDecimal("1.1"),"开发"));
peopleList.add(new People("小明",23,new BigDecimal("3000"),new BigDecimal("1.1"),"开发"));
peopleList.add(new People("小红",24,new BigDecimal("2800"),new BigDecimal("1.2"),"产品"));
peopleList.add(new People("小兰",22,new BigDecimal("3200"),new BigDecimal("1.3"),"开发"));
//根据名称分组(前提是名称是唯一的,要不然获取的是重复名称的最后一条)
Map studentMap = peopleList.stream().collect(Collectors.toMap(People::getName, Function.identity(), (key1, key2) -> key2));
System.out.println("value:"+studentMap);
}
}
@Data
@AllArgsConstructor
class People {
//姓名
private String name;
//年龄
private Integer age;
//工资
private BigDecimal salary;
//比率
private BigDecimal ratio;
//职业
private String occupation;
}
package com.saas.demo;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
/**
* Stream 分组
*/
public class StreamDemo {
public static void main(String[] args) {
List peopleList = new ArrayList();
peopleList.add(new People("小明","开发"));
peopleList.add(new People("小红","产品"));
peopleList.add(new People("小兰","开发"));
//根据职业分组无序
Map> studentMap = peopleList.stream().collect(Collectors.groupingBy(People::getOccupation));
System.out.println("value:"+studentMap);
//根据职业分组有序
Map> studentSortMap = peopleList.stream().collect(Collectors.groupingBy(People::getOccupation, LinkedHashMap::new,Collectors.toList()));
System.out.println("value:"+studentSortMap);
}
}
@Data
@AllArgsConstructor
class People {
//姓名
private String name;
//职业
private String occupation;
}
package com.saas.demo;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
/**
* Stream 分组
*/
public class StreamDemo {
public static void main(String[] args) {
List peopleList = new ArrayList();
peopleList.add(new People(1,"小明",23,new BigDecimal("3000"),new BigDecimal("1.1"),"开发"));
peopleList.add(new People(2,"小红",24,new BigDecimal("2800"),new BigDecimal("1.2"),"产品"));
peopleList.add(new People(3,"小兰",22,new BigDecimal("3200"),new BigDecimal("1.3"),"开发"));
//根据id分组
Map map = peopleList.stream().collect(Collectors.toMap(People::getId, People::getName,(oldValue, newValue) -> newValue));
System.out.println("value:"+map);
}
}
@Data
@AllArgsConstructor
class People {
//主键id
private Integer id;
//姓名
private String name;
//年龄
private Integer age;
//工资
private BigDecimal salary;
//比率
private BigDecimal ratio;
//职业
private String occupation;
}
package com.saas.demo;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.*;
import java.util.stream.Collectors;
/**
* Stream 分组
*/
public class StreamDemo {
public static void main(String[] args) {
List peopleList = new ArrayList();
peopleList.add(new People("小明",23,"开发"));
peopleList.add(new People("小红",24,"产品"));
peopleList.add(new People("小兰",22,"开发"));
peopleList.add(new People("小紫",22,"开发"));
//根据年龄和职业分组
Map> map = peopleList.stream().collect(Collectors.groupingBy(People::getAgeAndOccupation));
System.out.println("value:"+map);
}
}
@Data
@AllArgsConstructor
class People {
//姓名
private String name;
//年龄
private Integer age;
//职业
private String occupation;
public static String getAgeAndOccupation(People people){
return people.getAge() + "#" + people.getOccupation();
}
}
package com.saas.demo;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
/**
* Stream 分组
*/
public class StreamDemo {
public static void main(String[] args) {
List peopleList = new ArrayList();
peopleList.add(new People("小明","开发"));
peopleList.add(new People("小红","产品"));
peopleList.add(new People("小兰","开发"));
//根据职业分组无序
List> newList = new ArrayList<>();
peopleList.stream().collect(Collectors.groupingBy(People::getOccupation, Collectors.toList()))
.forEach((occupation, listOccupation) -> {
newList.add(listOccupation);
});
System.out.println("value:"+newList);
//根据职业分组有序
List> newSortList = new ArrayList<>();
peopleList.stream().collect(Collectors.groupingBy(People::getOccupation, LinkedHashMap::new, Collectors.toList()))
.forEach((occupation, listOccupation) -> {
newSortList.add(listOccupation);
});
System.out.println("value:"+newSortList);
}
}
@Data
@AllArgsConstructor
class People {
//姓名
private String name;
//职业
private String occupation;
}