简体   繁体   中英

Getting the path from a hash map key (Java)

My hashmap key returns this: MemorySection[path='rr', root='YamlCofiguration']=1

Is there anyway I can get the value of 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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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