简体   繁体   English

从哈希映射键获取路径(Java)

[英]Getting the path from a hash map key (Java)

My hashmap key returns this: MemorySection[path='rr', root='YamlCofiguration']=1 我的哈希映射键返回以下内容:MemorySection [path ='rr',root ='YamlCofiguration'] = 1

Is there anyway I can get the value of path=''. 无论如何,我可以获得path =''的值。 I know that I can get the value of the root='' using getValue(), although I only really use this for keeping track of the highest and lowest values to then order them from highest to lowest. 我知道我可以使用getValue()获得root =''的值,尽管我只是真正地使用它来跟踪最高和最低值,然后从最高到最低进行排序。

I have tried this thread although the answers presume that I would know what the pair's name is. 我已经尝试了该线程,尽管答案假定我会知道这对的名字是什么。 Retrieving Key from the Hash Map 从哈希图检索密钥

EDIT: Here is how I'm setting the data and sorting it as well. 编辑:这是我如何设置数据并对其进行排序。 I am accessing it through the likesList List 我正在通过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) {

                    }
                }

I still don't really know what you want, but here is a guess I made: 我仍然不太了解您想要什么,但是我做了一个猜测:

// 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM