java map集合排序的_Java 实现Map集合排序功能

第一步:Map中新增sort临时键

// 初始化Map集合

List> columns = new ArrayList>();

Map c1 = new HashMap();

c1.put("sort", "8");

c1.put("title", "www.chuweibiao.com");

columns.add(c1);

Map c2 = new HashMap();

c2.put("sort", "5");

c2.put("title", "www.chuweibiao.com");

columns.add(c2);

Map c3 = new HashMap();

c3.put("sort", "13");

c3.put("title", "www.chuweibiao.com");

columns.add(c3);

第二步:进行排序

/**

* Map类型元素集合排序

* @param columns

* Map类型元素集合

*/

private void listSortingForMapTypeElement(List> columns) {

Collections.sort(columns, new Comparator>() {

public int compare(Map last, Map next) {

Object lastSort = last.get("sort");

Object nextSort = next.get("sort");

if (lastSort == null || nextSort == null) {

return 0;

} else {

return Integer.parseInt(String.valueOf(lastSort))

> Integer.parseInt(String.valueOf(nextSort)) ? 1 : 0;

}

}

});

}

第三步:移除临时键

/**

* 移除排序临时键

* @param columns

* Map类型元素集合

*/

private void removeSortKey(List> columns) {

for (Map column : columns) {

column.remove("sort");

}

}

谁有更好的办法请拍砖指教,O(∩_∩)O谢谢~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值