public class TestEntity {
private String c1;
private String c2;
public TestEntity(){}
public TestEntity(String a,String b){
this.c1=a;
this.c2=b;
}
public String getC1() {
return c1;
}
public void setC1(String c1) {
this.c1 = c1;
}
public String getC2() {
return c2;
}
public void setC2(String c2) {
this.c2 = c2;
}
public String toString(){
return "TestEntity{c1="+c1+","+"c2="+c2+"}";
}
}
public class java8Test {
public static void main(String[] args){
TestEntity t1=new TestEntity("a","1");
TestEntity t2=new TestEntity("a","2");
TestEntity t3=new TestEntity("b","3");
List<TestEntity> list=new ArrayList<>();
list.add(t1);
list.add(t2);
list.add(t3);
//1、分组
Map<String,List<TestEntity>> map=list.stream().collect(Collectors.groupingBy(TestEntity::getC1));//按照c1分组
System.out.println(map);
//{a=[TestEntity{c1=a,c2=1}, TestEntity{c1=a,c2=2}], b=[TestEntity{c1=b,c2=3}]}
//2、List转Map
//如果有重复的key,则保留k1,舍弃k2。可以用(k1,k2)->k1来设置
Map<String,TestEntity> map1=list.stream().collect(Collectors.toMap(TestEntity::getC1,a->a,(k1,k2)->k1));
System.out.println(map1);
//{a=TestEntity{c1=a,c2=1}, b=TestEntity{c1=b,c2=3}}
//3、过滤Filter
List<TestEntity> list1=list.stream().filter(a->a.getC1().equals("a")).collect(Collectors.toList());
System.out.println(list1);
//[TestEntity{c1=a,c2=1}, TestEntity{c1=a,c2=2}]
//4、求和
//5、求最大值和最小值
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!