Map排序

package com.huawei.common;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.huawei.bean.MapBean;

public class CollectionUtil {
public static List<Map<String, MapBean>> sortLetter(
List<Map<String, String>> list) {
List<String> listValue = null;
List<Map<String, MapBean>> mapList = new ArrayList<Map<String, MapBean>>();
List<MapBean> listMapBean = new ArrayList<MapBean>();
if (list != null) {
for (Map<String, String> map : list) {
listValue = new ArrayList<String>();
List<Map.Entry<String, String>> infold = new ArrayList<Map.Entry<String, String>>(
map.entrySet());
Collections.sort(infold,
new Comparator<Map.Entry<String, String>>() {

@Override
public int compare(Entry<String, String> entry1,
Entry<String, String> entry2) {
String o1 = entry1.getValue();
String o2 = entry2.getValue();
for (int i = 0; i < o1.length(); i++) {
if (i == o1.length() && i < o2.length()) {
return -1;
} else if (i == o2.length()
&& i < o1.length()) {
return 1;
}
char ch1 = o1.charAt(i);
char ch2 = o2.charAt(i);
if (ch1 >= '0' && ch2 <= '9') {
int i1 = getNumber(o1.substring(i));
int i2 = getNumber(o2.substring(i));
if (i1 == i2) {
continue;
} else {
return i1 - i2;
}
} else if (ch1 != ch2) {
return ch1 - ch2;
}
}
return 0;
}

private int getNumber(String str) {
int num = Integer.MAX_VALUE;
int bits = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) >= '0'
&& str.charAt(i) <= '9') {
bits++;
} else {
break;
}
}
if (bits > 0) {
num = Integer.parseInt(str.substring(0,
bits));
}
return num;
}
});
Map<String, MapBean> newMap = new HashMap<String, MapBean>();
List<String> list2 = new ArrayList<String>();
int newkey = 0;
for (Entry entry : infold) {
newkey++;
MapBean mapbean = new MapBean();
mapbean.setKey(entry.getKey().toString());
mapbean.setValue(entry.getValue().toString());
newMap.put(String.valueOf(newkey), mapbean);
}
mapList.add(newMap);
System.out.println(infold);
}
}
return mapList;
}

public static void main(String[] args) {
Map<String, String> map1 = new HashMap<String, String>();
map1.put("1", "wu");
map1.put("y", "lvxinbo");
map1.put("g", "wa");
Map<String, String> map2 = new HashMap<String, String>();
map2.put("w", "zhangjun");
map2.put("h", "fff");
map2.put("l", "mmmm");
Map<String, String> map3 = new HashMap<String, String>();
map3.put("8", "wangbo");
map3.put("m", "abc");
map3.put("v", "");
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
list.add(map1);
list.add(map2);
list.add(map3);
List<Map<String, MapBean>> newList = CollectionUtil.sortLetter(list);
for (Map<String, MapBean> map : newList) {
Set<Entry<String, MapBean>> set = map.entrySet();
for (Iterator<Entry<String, MapBean>> iter = set.iterator(); iter
.hasNext();) {
MapBean mapBean = new MapBean();
Map.Entry<String, MapBean> entry = (Map.Entry<String, MapBean>)iter.next();
mapBean = entry.getValue();
System.out.println("key = "+entry.getKey()+","+"value = "+mapBean.getKey()+"\t"+mapBean.getValue());
}
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值