List型別的集合根據map中某個鍵的值排序

2021-08-26 12:25:20 字數 1309 閱讀 6644

collections.sort(list1, new comparator() 

if (o1.get("e_code") == null || "".equals(o1.get("e_code")))

if (o2.get("e_code") == null || "".equals(o2.get("e_code")))

integer e_code1 = integer.valueof(o1.get("e_code").tostring()) ;

integer e_code2 = integer.valueof(o2.get("e_code").tostring()) ;

return e_code1.compareto(e_code2);

}});

按照list1的map中e_code欄位的大小進行排序,從小到大,並且當此字段為空時排到最後,比較兩個map中字段值時當o1大於o2時返回1,o1小於o2時返回-1,當兩個值相等時返回0;

系統使用了一陣子後,突然有一天排序這裡出了問題,通過研究對**進行了修改,此段**為:

collections.sort(list1, new comparator() 

if ((null == o1.get("e_code") && "" .equals(o2.get("e_code")) ) || ("".equals(o1.get("e_code")) && null== o2.get("e_code")))

if (null == o1.get("e_code") || "".equals(o1.get("e_code")))

if (null == o2.get("e_code") || "".equals(o2.get("e_code")))

integer e_code1 = integer.valueof(o1.get("e_code").tostring());

integer e_code2 = integer.valueof(o2.get("e_code").tostring());

return e_code1.compareto(e_code2);

}});

錯誤原因是當null和「」或「」與null進行比較的情況下會報錯,所以新增了如下**,表示null和「」比較時認為他們相等

if ((null == o1.get("e_code") && "" .equals(o2.get("e_code")) ) || ("".equals(o1.get("e_code")) && null== o2.get("e_code")))

遍歷List中的Map集合

list 是傳入的錶身資料 listlist new arraylist for int i 0 itfpss tfpss new tfpss beanutils.copyproperties mfpsswithblobs.getparams get i tfpss list.add tfpss l...

list中有map,根據map的value進行排序

list中有map,需要對map中的value進行從小到大排序,使用lambda表示式可以輕鬆實現。collections.sort list,map1,map2 system.out.println 排序後的list list map1和map2的value進行對比,如果key是string型別,...

Scala中List集合與Map集合的相互轉換

list集合轉換成map集合object demo println tuples 將list轉換成map val map map string,int tuples.tomap println map 結果為 list dog,3 tiger,5 lion,4 cat,3 panther,7 eag...