本文实例讲述了java实现对两个list快速去重并排序操作。分享给大家供大家参考,具体如下:
1:去重并排序
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
package twolist; import java.util.collections; import java.util.comparator; import java.util.hashmap; import java.util.hashset; import java.util.linkedlist; import java.util.list; import java.util.map; import java.util.set; public class listmapsort { /** * @param args */ public static void main(string[] args) { // todo 自动生成方法存根 list<map<string,object>> listmap1 = new linkedlist<map<string,object>>(); map<string,object> map = new hashmap<string, object>(); map.put( "date" , 20121010 ); listmap1.add(map); map = new hashmap<string, object>(); map.put( "date" , 20011213 ); listmap1.add(map); listmap1.add(map); map = new hashmap<string, object>(); map.put( "date" , 20130502 ); listmap1.add(map); system.out.println( "原始" +listmap1); list<map<string,object>> listmap2 = new linkedlist<map<string,object>>(); set<map> setmap = new hashset<map>(); for (map<string,object> map1 : listmap1){ if (setmap.add(map1)){ listmap2.add(map1); } } system.out.println( "去重" +listmap2); collections.sort(listmap2, new comparator<map<string,object>>(){ public int compare(map<string,object> o1,map<string,object> o2){ return o1.get( "date" ).tostring().compareto(o2.get( "date" ).tostring()); } }); system.out.println( "排序:" +listmap2); } } |
2:去掉重复的后,相同的部分
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
package twolist; import java.util.arraylist; import java.util.hashmap; import java.util.list; import java.util.map; import java.util.set; public class removelist { private <k, v> void cleanlistbymapkey(list<map<k,v>> list, k toberemoved) { list<map<k,v>> tmplist= new arraylist<>(); for (map<k,v> m: list){ if (m.containskey(toberemoved)) tmplist.add(m); } list.removeall(tmplist); } public void testcleanlistbymapkey(){ list<map<string,string>> list= new arraylist<>(); for ( int i= 0 ;i< 10 ;i++){ map<string, string> m= new hashmap<>(); m.put( "key" +i, "value" +i); list.add(m); } map<string, string> m= new hashmap<>(); m.put( "key100" , "value100" ); list.add(m); system.out.println(list.contains(m)); cleanlistbymapkey(list, "key100" ); system.out.println(list.contains(m)); } public static void main(string[] args) { /*removelist remove = new removelist(); remove.testcleanlistbymapkey();*/ // todo auto-generated method stub 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" , "1" ); map1.put( "name" , "p" ); map<string, object> map2 = new hashmap<string, object>(); map2.put( "id" , "3" ); map2.put( "name" , "h" ); map<string, object> map3 = new hashmap<string, object>(); map3.put( "id" , "3" ); map3.put( "name" , "f" ); list.add(map1); list.add(map3); list.add(map2); system.out.println( "初始数据:" + list.tostring()); //把list中的数据转换成msp,去掉同一id值多余数据,保留查找到第一个id值对应的数据 for ( int i = list.size()- 1 ; i>= 0 ; i--){ map map = list.get(i); string id = (string)map.get( "id" ); map.remove( "id" ); msp.put(id, map); } //把msp再转换成list,就会得到根据某一字段去掉重复的数据的list<map> set<string> mspkey = msp.keyset(); for (string key: mspkey){ map newmap = msp.get(key); newmap.put( "id" , key); listmap.add(newmap); } system.out.println( "去掉重复数据后的数据:" + listmap.tostring()); } } |
曾经去某平安面试的时候面试官问的问题,当时回答是冒泡循环,哈哈,现在想想觉得好笑,跟冒泡循环个毛关系~~
切记,利用set的不重复,可以快速去除重复
用一个list的某个属性作为map的key,可以找到是否在list存在,这样你就可以做响应的处理
利用上面的demo,得到一个方案,解决下面的需求:
订单中商品的集合如下:
退款中的商品集合如下:
那么其它的商品就应该是xl:2件 m:2件
把其中一个list转成map,然后在用第二个list中的相关属性作为第一个map的key来判断,最终问题得以引刃而解,哈哈,牛逼!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
//第一步:先将原始订单中跟退款中一模一样的移除出来 map<string,object> wsmap = new hashmap<string, object>(); for (applyreturn applyreturn : groupitemlist) { //格式itemid_color_size qua wsmap.put(applyreturn.getitemid()+ "_" +applyreturn.getcolor()+ "_" +applyreturn.getsize(), applyreturn.getqua()); } list<orderdetail> newlistorderdetails = new arraylist<orderdetail>(); list<orderdetail> listorderdetail = order.getdetails(); //第二步:再来遍历剩下的 int mapqua = 0 ; for (orderdetail orderdetail : listorderdetail) { if (wsmap.get(orderdetail.gettid()+ "_" +orderdetail.getkcolor()+ "_" +orderdetail.getksize())!= null ){ mapqua = integer.parseint(wsmap.get(orderdetail.gettid()+ "_" +orderdetail.getkcolor()+ "_" +orderdetail.getksize()).tostring()); if (mapqua<orderdetail.getqua()){ orderdetail neworderdetail = orderdetail; neworderdetail.setqua(orderdetail.getqua()-mapqua); newlistorderdetails.add(neworderdetail); } } else { newlistorderdetails.add(orderdetail); } } |
ps:这里再为大家提供1款相关工具供大家参考使用:
在线去除重复项工具:https://tool.zzvips.com/t/quchong/
希望本文所述对大家java程序设计有所帮助。
原文链接:https://blog.csdn.net/xb12369/article/details/51499743