java jdk1.8 使用stream流進(jìn)行l(wèi)ist 分組歸類操作
我就廢話不多說了,大家還是直接看代碼吧~
import com.alibaba.fastjson.JSON;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author czw
*/
public class Foo{
private String name;
private String type;
private Double typeValue;
private Integer count;
public Foo(String name, String type, Double typeValue, Integer count) {
this.name = name;
this.type = type;
this.typeValue = typeValue;
this.count = count;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Double getTypeValue() {
return typeValue;
}
public void setTypeValue(Double typeValue) {
this.typeValue = typeValue;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
@Override
public String toString() {
return "Foo{" +
"name='" + name + '\'' +
", type='" + type + '\'' +
", typeValue=" + typeValue +
", count=" + count +
'}';
}
public static void main(String[] args) {
List<Foo> fooList = new ArrayList<Foo>();
fooList.add(new Foo("A","san",1.0,2)) ;
fooList.add( new Foo("A","nas",13.0,1)) ;
fooList.add(new Foo("B","san",112.0,3)) ;
fooList.add(new Foo("C","san",43.0,5)) ;
fooList.add(new Foo("B","nas",77.0,7)) ;
List<List<Foo>> groupList = new ArrayList<>();
fooList.stream()
.collect(Collectors.groupingBy(Foo::getName,Collectors.toList()))
.forEach((name,fooListByName)->{
groupList.add(fooListByName);
});
System.out.println(JSON.toJSONString(groupList));
}
}
輸出結(jié)果
[
[{
"count": 2,
"name": "A",
"type": "san",
"typeValue": 1
}, {
"count": 1,
"name": "A",
"type": "nas",
"typeValue": 13
}],
[{
"count": 3,
"name": "B",
"type": "san",
"typeValue": 112
}, {
"count": 7,
"name": "B",
"type": "nas",
"typeValue": 77
}],
[{
"count": 5,
"name": "C",
"type": "san",
"typeValue": 43
}]
]
補(bǔ)充知識:java jdk1.8的stream復(fù)雜和簡單的分組
獲取List對象中的某個參數(shù)時:
List<Map<String,String>> param = new ArrayList<>();
Map<String,String> map = new HashMap<>();
map.put("id","1213");
map.put("name","test");
List<String> strList = param.stream().map(key ->key.get("name")).collect(Collectors.toList());
簡單參數(shù)分組:
List<DamoForm> damoformList = new ArrayList<>();
Map<String, Map<String, List<DamoForm>>> collect = damoformList.stream()
.collect(Collectors.groupingBy(DamoForm::getId()))
.entrySet()
.stream()
.collect(Collectors.toMap(
entry -> entry.getKey(),
entry -> entry.getValue().stream().collect(Collectors.groupingBy(DamoForm::getName()))
));
針對List復(fù)雜排序,多個條件進(jìn)行排序:
應(yīng)用場景:針對List中某個字段的數(shù)據(jù)進(jìn)行雙重倒序的方式排序,代碼有點復(fù)雜,不明白的可以留言。
List<DamoForm> damoformList = new ArrayList<>();
List<Map<String, Object>> result = damoformList.stream()
.collect(Collectors.groupingBy(DamoForm::getPartClass))
.entrySet()
.stream()
.sorted((o1, o2) -> {
/*
* 這里排序,任何有1的排在前,全部是0排在后
*/
Integer sort1 = o1.getValue().stream().anyMatch(item -> item.getIsFlag() > 0) ? -1 : 1;
Integer sort2 = o2.getValue().stream().anyMatch(item -> item.getIsFlag() > 0) ? -1 : 1;
return sort1.compareTo(sort2);
})
.map(entry -> {
Map<String, Object> map = Maps.newHashMapWithExpectedSize(2);
map.put("repairItemTypeName", entry.getKey());
/*
* 這里排序,1排在前,0排在后
*/
List<DamoVO> damoVOList = entry.getValue().stream()
.sorted(Comparator.comparingInt(o -> (o.getIsFlag() * -1)))
.collect(Collectors.toList());
map.put("repairTypeList", itemDescFormList);
return map;
})
.collect(Collectors.toList());
以上這篇java jdk1.8 使用stream流進(jìn)行l(wèi)ist 分組歸類操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
spring-boot 如何實現(xiàn)單次執(zhí)行程序
這篇文章主要介紹了spring-boot 實現(xiàn)單次執(zhí)行程序方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
超詳細(xì)講解Java秒殺項目用戶驗證模塊的實現(xiàn)
這是一個主要使用java開發(fā)的秒殺系統(tǒng),項目比較大,所以本篇只實現(xiàn)了用戶驗證模塊,代碼非常詳盡,感興趣的朋友快來看看2022-03-03
使用SpringBoot創(chuàng)建一個RESTful API的詳細(xì)步驟
使用 Java 的 Spring Boot 創(chuàng)建 RESTful API 可以滿足多種開發(fā)場景,它提供了快速開發(fā)、易于配置、可擴(kuò)展、可維護(hù)的優(yōu)點,尤其適合現(xiàn)代軟件開發(fā)的需求,幫助你快速構(gòu)建出高性能的后端服務(wù),需要的朋友可以參考下2025-01-01
一文講透為什么遍歷LinkedList要用增強(qiáng)型for循環(huán)
這篇文章主要為大家介紹了為什么遍歷LinkedList要用增強(qiáng)型for循環(huán)的透徹詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
淺析Android系統(tǒng)中HTTPS通信的實現(xiàn)
這篇文章主要介紹了淺析Android系統(tǒng)中HTTPS通信的實現(xiàn),實現(xiàn)握手的源碼為Java語言編寫,需要的朋友可以參考下2015-07-07
Spring?框架中的?Bean?作用域(Scope)使用詳解
Spring框架中的Bean作用域(Scope)決定了在應(yīng)用程序中創(chuàng)建和管理的Bean對象的生命周期和可見性。本文將詳細(xì)介紹Spring框架中的Bean作用域的不同類型,包括Singleton、Prototype、Request、Session和Application,并解釋它們的特點和適用場景。2023-09-09

