0

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) {

                    }
                }
Community
  • 1
  • 1
Max Kortge
  • 527
  • 7
  • 23
  • 1
    One thing you can do is to get is a `Set` of all keys in the `HashMap` by using the built-in method `.keySet()`. – kazbeel Jun 29 '16 at 06:30
  • @WoozyCoder You can also get an `entrySet` – Michael Markidis Jun 29 '16 at 06:33
  • @MichaelMarkidis my bad, you're right. Comment updated... – kazbeel Jun 29 '16 at 06:34
  • 1
    @MaxKrissigo how is your hashmap defined? – Michael Markidis Jun 29 '16 at 06:34
  • HashMap likes = new HashMap<>(); – Max Kortge Jun 29 '16 at 06:35
  • *That* is not the output of `HashMap.toString()`. Output would start with `{`, have `name=value` pairs, separated by comma, and end with `}`. – Andreas Jun 29 '16 at 06:37
  • 1
    And how do you end up storing something like `"MemorySection[path='rr', root='YamlCofiguration']"` as a key of this map, and `"1"` as the value (just guessing here)? It seems you should instead have a `Map`. Show us the relevant code. – JB Nizet Jun 29 '16 at 06:37
  • or instead have a Map – Michael Markidis Jun 29 '16 at 06:39
  • if you can post source of data as well, will be more easy to solve – bananas Jun 29 '16 at 06:44
  • OK. So, you still haven't shown how the map is defined. But we already know it's not a Map, but a Map. The String is obtained via playersFile.getString(key). That's probably the problem: you shouldn't use getString() to get what looks like a structured object of type MemorySection. Again, we can only guess. – JB Nizet Jun 29 '16 at 06:50
  • I commented it above, I'll put it in the main post so it is clear. – Max Kortge Jun 29 '16 at 06:51
  • `likes.put(playersFile.getString(key), playersFile.getInt(key + ".likes"));` in this line you are putting string as a value – bananas Jun 29 '16 at 06:55
  • @JBNizet I'm currently using the Spigot API so I need to use getString to access it from my config.yml. playersFile is just getting the configuration file. – Max Kortge Jun 29 '16 at 07:01
  • Then your question should be tagged with spigot, should show your YAML file, should explain what you want to get from the file. – JB Nizet Jun 29 '16 at 07:07
  • Although Spigot isn't the problem, it just involves Java and using hash maps. Thats what I though at least. – Max Kortge Jun 29 '16 at 07:13
  • Hey Max. In your code example, where would the `MemorySection[path='rr', root='YamlCofiguration']=1` be? Is that the value of `String key` or an example of the return value from `playersFile.getString(key)`? Or something else? – sotix Jun 29 '16 at 07:14
  • That is the value from String name1 = cookiesList.get(i).getKey(); – Max Kortge Jun 29 '16 at 07:19

1 Answers1

1

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
sotix
  • 822
  • 1
  • 8
  • 23