話不多說,看代碼和效果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
/** * 根據map中的某個key 去除list中重復的map * @author shijing * @param list * @param mapkey * @return */ public static list<map<string, object>> removerepeatmapbykey(list<map<string, object>> list, string mapkey){ if (collectionutils.isnullorempty(list)) return null ; //把list中的數據轉換成msp,去掉同一id值多余數據,保留查找到第一個id值對應的數據 list<map<string, object>> listmap = new arraylist<>(); map<string, map> msp = new hashmap<>(); for ( int i = list.size()- 1 ; i>= 0 ; i--){ map map = list.get(i); string id = (string)map.get(mapkey); map.remove(mapkey); msp.put(id, map); } //把msp再轉換成list,就會得到根據某一字段去掉重復的數據的list<map> set<string> mspkey = msp.keyset(); for (string key: mspkey){ map newmap = msp.get(key); newmap.put(mapkey, key); listmap.add(newmap); } return listmap; } |
測試:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public static void main(string[] args) { map<string, map> msp = new hashmap<string, map>(); list<map<string, object>> list = new arraylist<map<string, object>>(); list<map<string, object>> listmap = new arraylist<map<string,object>>(); map<string, object> map1 = new hashmap<string, object>(); map1.put( "id" , "1123" ); map1.put( "name" , "張三" ); map<string, object> map2 = new hashmap<string, object>(); map2.put( "id" , "2" ); map2.put( "name" , "李四" ); map<string, object> map3 = new hashmap<string, object>(); map3.put( "id" , "1123" ); map3.put( "name" , "王五" ); map<string, object> map4 = new hashmap<string, object>(); map4.put( "id" , "3" ); map4.put( "name" , "趙六" ); list.add(map1); list.add(map2); list.add(map3); list.add(map4); system.out.println( "初始數據:" + list.tostring()); system.out.println( "去重之后:" + removerepeatmapbykey(list, "id" )); } |
結果:
初始數據:[{name=張三, id=1123}, {name=李四, id=2}, {name=王五, id=1123}, {name=趙六, id=3}]
去重之后:[{name=李四, id=2}, {name=趙六, id=3}, {name=張三, id=1123}]
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。如果你想了解更多相關內容請查看下面相關鏈接
原文鏈接:https://blog.csdn.net/moneyshi/article/details/81220421