我创建了一个名为result的地图。
在sortByKeys方法中,因为我的键是带有数值的String,我已将它们转换为Integer键类型Map然后对它们进行排序。
Map<String, String> unsortMap = new TreeMap<String, String>();
unsortMap.put("room~1", "e");
unsortMap.put("room~2", "y");
unsortMap.put("room~10", "n");
unsortMap.put("room~4", "j");
unsortMap.put("room~5", "m");
unsortMap.put("room~3", "f");
Set set2 = unsortMap.entrySet();
Iterator iterator2 = set2.iterator();
while (iterator2.hasNext()) {
/* Iterate */
Map.Entry me2 = (Map.Entry) iterator2.next();
String key = (String) me2.getKey();
Object value = (Object) me2.getValue();
System.out.println("Key ==>" + key + " Value ==>" + value);
}
# Current Output:#
/* current result */
Key ==>room~1 Value ==>e
Key ==>room~10 Value ==>n
Key ==>room~2 Value ==>y
Key ==>room~3 Value ==>f
Key ==>room~4 Value ==>j
Key ==>room~5 Value ==>m
#Expected O/p:#
/* required result */
Key ==>room~1 Value ==>e
Key ==>room~2 Value ==>y
Key ==>room~3 Value ==>f
Key ==>room~4 Value ==>j
Key ==>room~5 Value ==>m
Key ==>room~10 Value ==>n
答案 0 :(得分:4)
创建自定义密钥对象
public class Key implements Comparable<Key>{
String name;
int id;
public Key(String name, int id) {
this.name = name;
this.id = id;
}
@Override
public int compareTo(Key o) {
if(Objects.equals(name, o.name)){
return Integer.compare(id, o.id);
}else{
return name.compareTo(o.name);
}
}
@Override
public String toString() {
return name +"~"+ id;
}
@Override
public boolean equals(Object o){
...
@Override
public int hashCode(){
...
}
并像这样使用它:
Map<Key, String> unsortMap = new TreeMap<>();
unsortMap.put(new Key("room", 5), "e");
但是如果String总是空间,你应该在密钥
中使用它答案 1 :(得分:4)
在不更改代码的情况下,您可以这样做。需要编写自己的自定义比较器。请记住,当您需要按照自己的方式进行排序时,始终可以创建比较器登录
Map<String, String> unsortMap = new TreeMap<String, String>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
// TODO Auto-generated method stub
int j=Integer.parseInt(o1.substring(o1.indexOf("~")+1));
int k=Integer.parseInt(o2.substring(o1.indexOf("~")+1));
return j-k;
}
});
unsortMap.put("room~1", "e");
unsortMap.put("room~2", "y");
unsortMap.put("room~10", "n");
unsortMap.put("room~4", "j");
unsortMap.put("room~5", "m");
unsortMap.put("room~3", "f");
Set set2 = unsortMap.entrySet();
Iterator iterator2 = set2.iterator();
while (iterator2.hasNext()) {
/* Iterate */
Map.Entry me2 = (Map.Entry) iterator2.next();
String key = (String) me2.getKey();
Object value = (Object) me2.getValue();
System.out.println("Key ==>" + key + " Value ==>" + value);
}
答案 2 :(得分:0)
使用流和LinkedHashMap
维护订单
Map<String, String> unsortMap = new TreeMap<String, String>();
unsortMap.put("room~1", "e");
unsortMap.put("room~2", "y");
unsortMap.put("room~10", "n");
unsortMap.put("room~4", "j");
unsortMap.put("room~5", "m");
unsortMap.put("room~3", "f");
Comparator<String> c = (s1, s2) -> Integer.parseInt(s1.split("~")[1]) - Integer.parseInt(s2.split("~")[1]);
Map<String, String> sortedMap = unsortMap.keySet()
.stream()
.sorted(c)
.collect(Collectors.toMap(k -> k, k -> unsortMap.get(k), (k, v) -> v, LinkedHashMap::new));
System.out.println(sortedMap);
<强>输出强>
{room~1=e, room~2=y, room~3=f, room~4=j, room~5=m, room~10=n}
答案 3 :(得分:0)
String类型的键的更通用比较器,包含数字和字母:
Map<String, Double> result = new TreeMap<String, Double>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
String numericPart1 = o1.replaceAll("\\D+","");
String numericPart2 = o2.replaceAll("\\D+","");
String alphaPart1 = o1.replace(numericPart1, "");
String alphaPart2 = o2.replace(numericPart2, "");
if(alphaPart1.equals(alphaPart2)) {
return Integer.compare(Integer.parseInt(numericPart1), Integer.parseInt(numericPart2));
} else {
return alphaPart1.compareTo(alphaPart2);
}
}
});