[英]Getting the path from a hash map key (Java)
我的哈希映射鍵返回以下內容:MemorySection [path ='rr',root ='YamlCofiguration'] = 1
無論如何,我可以獲得path =''的值。 我知道我可以使用getValue()獲得root =''的值,盡管我只是真正地使用它來跟蹤最高和最低值,然后從最高到最低進行排序。
我已經嘗試了該線程,盡管答案假定我會知道這對的名字是什么。 從哈希圖檢索密鑰
編輯:這是我如何設置數據並對其進行排序。 我正在通過LikesList列表訪問它
HashMap<String, Integer> likes = new HashMap<>();
for(String key: playersFile.getKeys(false)) {
likes.put(playersFile.getString(key), playersFile.getInt(key + ".likes"));
}
List<Entry<String, Integer>> likesList = new LinkedList<Entry<String, Integer>>(likes.entrySet());
Collections.sort(likesList, new Comparator<Entry<String, Integer>>() {
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
return o2.getValue() - o1.getValue();
}
});
for (int i = 0; i<45; i++) {
try {
String name1 = cookiesList.get(i).getKey();
item = name(name1);
publicinv.setItem(i, item);
} catch (IndexOutOfBoundsException e) {
}
}
我仍然不太了解您想要什么,但是我做了一個猜測:
// your input
String name1 = "MemorySection[path='rr', root='YamlCofiguration']=1";
// this string indicates the start of the path
String start = "path='";
// where the path starts
int pathStart = name1.indexOf(start)+start.length();
// where the path ends
int pathEnd = name1.substring(pathStart).indexOf("'") + pathStart;
// get the path
System.out.println( name1.substring(pathStart, pathEnd) ); // prints: rr
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.