[英]How to sort a Map in Java by its key but if the Key is combination of (String + numeric)
我已經創建了一個名為result的地圖。
在sortByKeys方法中,因為我的鍵是帶有數字值的字符串,所以我將它們轉換為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
創建一個自定義鍵對象
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始終是空格,則應在密鑰中使用它
無需更改代碼即可執行此操作。需要編寫自己的自定義比較器。請始終牢記,當需要按自己的方式進行排序時,始終可以創建比較器登錄名
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);
}
使用流和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}
包含數字和字母的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);
}
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.