import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
public class TestMap {
public static void main(String[] args) {
Map<String, List<Map<String, String>>> testMap = new TreeMap<String, List<Map<String, String>>>();
init(testMap);
System.out.println("start=============1");
for(Map.Entry< String,List<Map<String, String>>> temp : testMap.entrySet()) {
System.out.println(temp.getKey()+"==" + temp.getValue());
}
System.out.println("end=============1");
List<Entry< String,List<Map<String, String>>>> list = new ArrayList<Entry<String, List<Map<String, String>>>>(testMap.entrySet());
System.out.println("start=============2");
for(Entry< String,List<Map<String, String>>> temp : list) {
System.out.println(temp.getKey()+"==" + temp.getValue());
}
System.out.println("end=============2");
Collections.sort(list, new Comparator<Map.Entry<String,List<Map<String, String>>>>() {
@Override
public int compare(Entry<String, List<Map<String, String>>> o1,
Entry<String, List<Map<String, String>>> o2) {
// TODO Auto-generated method stub
List<Map<String, String>> first = o1.getValue();
List<Map<String, String>> second = o2.getValue();
if ((first == null || first.size() == 0) && (second == null || second.size() == 0)) {
return 0;
}
if ((first == null || first.size() == 0) && (second != null && second.size() > 0)) {
return -1;
}
if ((first != null && first.size() > 0) && (second == null || second.size() == 0)) {
return 1;
}
//first.
Collections.sort(first, new Comparator<Map<String, String>>() {
@Override
public int compare(Map<String, String> first, Map<String, String> second) {
// TODO Auto-generated method stub
if ((first == null || first.size() == 0) && (second == null || second.size() == 0)) {
return 0;
}
if ((first == null || first.size() == 0) && (second != null && second.size() > 0)) {
return -1;
}
if ((first != null && first.size() > 0) && (second == null || second.size() == 0)) {
return 1;
}
String strPre = "pre";
if (first.containsKey(strPre) && first.get(strPre) != null
&& (!second.containsKey(strPre) || second.get(strPre) == null || second.get(strPre).trim() == "")) {
return -1;
}
if (second.containsKey(strPre) && second.get(strPre) != null
&& (!first.containsKey(strPre) || first.get(strPre) == null || first.get(strPre).trim() == "")) {
return 1;
}
Double one = Double.valueOf(first.get("pre"));
Double two = Double.valueOf(second.get("pre"));
if (one > two) {
return 1;
}
if (one < two) {
return -1;
}
return 0;
}
});
Collections.sort(second, new Comparator<Map<String, String>>() {
@Override
public int compare(Map<String, String> first, Map<String, String> second) {
// TODO Auto-generated method stub
if ((first == null || first.size() == 0) && (second == null || second.size() == 0)) {
return 0;
}
if ((first == null || first.size() == 0) && (second != null && second.size() > 0)) {
return -1;
}
if ((first != null && first.size() > 0) && (second == null || second.size() == 0)) {
return 1;
}
String strPre = "pre";
if (first.containsKey(strPre) && first.get(strPre) != null
&& (!second.containsKey(strPre) || second.get(strPre) == null || second.get(strPre).trim() == "")) {
return -1;
}
if (second.containsKey(strPre) && second.get(strPre) != null
&& (!first.containsKey(strPre) || first.get(strPre) == null || first.get(strPre).trim() == "")) {
return 1;
}
Double one = Double.valueOf(first.get("pre"));
Double two = Double.valueOf(second.get("pre"));
if (one > two) {
return 1;
}
if (one < two) {
return -1;
}
return 0;
}
});
Map<String, String> firstMap = first.get(0);
Map<String, String> secondMap = second.get(0);
if ((firstMap == null || firstMap.size() == 0) && (secondMap == null || secondMap.size() == 0)) {
return 0;
}
if ((firstMap == null || firstMap.size() == 0) && (secondMap != null && secondMap.size() > 0)) {
return -1;
}
if ((firstMap != null && firstMap.size() > 0) && (secondMap == null || secondMap.size() == 0)) {
return 1;
}
String strPre = "pre";
if (firstMap.containsKey(strPre) && firstMap.get(strPre) != null && (!secondMap.containsKey(strPre)
|| secondMap.get(strPre) == null || secondMap.get(strPre).trim() == "")) {
return -1;
}
if (secondMap.containsKey(strPre) && secondMap.get(strPre) != null && (!firstMap.containsKey(strPre)
|| firstMap.get(strPre) == null || firstMap.get(strPre).trim() == "")) {
return 1;
}
Double one = Double.valueOf(firstMap.get("pre"));
Double two = Double.valueOf(secondMap.get("pre"));
if (one > two) {
return 1;
}
if (one < two) {
return -1;
}
if (one == two) {
//如果值相等,则比较key值
//return -1;
String key1 = o1.getKey();
String key2 = o2.getKey();
return key1.compareTo(key2);
}
return 0;
}
});
System.out.println("start=============3");
for(Entry< String,List<Map<String, String>>> temp : list) {
System.out.println(temp.getKey()+"==" + temp.getValue());
}
System.out.println("end=============3");
}
public static void init(Map<String, List<Map<String, String>>> map) {
List<Map<String, String>> temp = new ArrayList<Map<String, String>>();
Map<String, String> tempMap01 = new HashMap<String,String>();
tempMap01.put("pre", "0.1");
Map<String, String> tempMap02 = new HashMap<String,String>();
tempMap02.put("pre", "0.9");
temp.add(tempMap01);
temp.add(tempMap02);
map.put("aaaaa", temp);
temp = new ArrayList<Map<String, String>>();
tempMap01 = new HashMap<String,String>();
tempMap01.put("pre", "0.01");
tempMap02 = new HashMap<String,String>();
tempMap02.put("pre", "0.001");
temp.add(tempMap01);
temp.add(tempMap02);
map.put("uuuuu", temp);
temp = new ArrayList<Map<String, String>>();
tempMap01 = new HashMap<String,String>();
tempMap01.put("pre", "0.01");
tempMap02 = new HashMap<String,String>();
tempMap02.put("pre", "0.001");
temp.add(tempMap01);
temp.add(tempMap02);
map.put("bbbbbbb", temp);
temp = new ArrayList<Map<String, String>>();
tempMap01 = new HashMap<String,String>();
tempMap01.put("pre", "1.01");
tempMap02 = new HashMap<String,String>();
tempMap02.put("pre", "2.001");
temp.add(tempMap01);
temp.add(tempMap02);
map.put("ppppppp", temp);
}
}