list轉map Java Stream的使用

2021-08-13 10:04:11 字數 2712 閱讀 2685

一、list ->map

1、有如下表對應表結構,實現

2、先進行一些簡單的操作

定義類

private integer rank;  

private string description;

public movie(integer rank, string description)

public integer getrank()

public string getdescription()

@override

public string tostring()

2.1  listmovies --->  map

直接遍歷將對應值put放入map

//建立乙個 listmovies

listmovies = new arraylist();

movies.add(new movie(1, "the shawshank redemption"));

movies.add(new movie(2, "the godfather"));

for (movie movie : movies)

//列印map

int rank=entry.getkey();

movie movie=entry.getvalue();

system.out.println(rank+":"+movie);

}

2.2使用流

//建立乙個list

listmovies = new arraylist();

movies.add(new movie(1, "the shawshank redemption"));

movies.add(new movie(2, "the godfather"));

listmovies ---> listranklist

listranklist=movies.stream().map(p->p.getrank()).collect(collectors.tolist());
list---> map>

3、回到問題

listredbaggoodslist --->  listsusnapids 即:redbaggoods的ids(redbaggoodslist)獲取susnapinfo表的ids

listsusnapids = redbaggoodslist.stream().map(p->p.getsusnapid()).collect(collectors.tolist());
去重

listgiftstatuslist = redbaggoodslist.stream().map(p -> p.getgiftstatus()).distinct().collect(collectors.tolist());
listsusnapinfolist  --->  map> susnapinfolist2susnapinfomap 即 : 將list轉為map,鍵為所有的id,值為物件列表

map> redbagid2redbagmap = redbaglist.stream().collect(collectors.groupingby(p->p.getredbagid()));
統計數量

long sucount = susnapinfolist.stream().map(p->p.getsuname()).distinct().count();
過濾

listgiftids = redbaggoodslist.stream().map(p -> p.getgiftbagid()).filter(p -> p.equals(firstgiftids))

.collect(collectors.tolist());

二、list去重

比較簡單,只給出**

// 集合去重

listls1 = new arraylist<>();

ls1.add("a");

ls1.add("b");

ls1.add("c");

listls2 = new arraylist<>();

ls2.add("a");

ls2.add("b");

ls2.add("c");

ls2.add("d");

// 是否存在ls1完全包含ls2的情況

boolean exist = ls1.containsall(ls2);

if (!exist)

system.out.println(ls2);// d

List排序轉map map再轉list

根據工單id查詢字段值 listformitemvaluevolist this.produceformdao.listformitemvaluebyworkorderid workorderid if formitemvaluevolist.isempty map formitemvaluevom...

form表單,轉Map,轉List

1 form表單包含,select下拉列表,input輸入框等,轉為map,js如下 var baseinfo var formarray baseinfo serializearray each formarray,function i,item 注 baseinfo baseinfo是form的...

json轉實體,json轉list

json轉換 注 以下全用的是com.alibaba.fastjson 1.實體轉string轉json user user new user user.age 18 user,name 張三 實體轉string string json json.tojsonstring result string...