Solved HashMap Help

Discussion in 'Plugin Development' started by sethrem, Oct 29, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    sethrem

    Hello My Fellow Bukkitians,
    I'am having an issue with hasmaps. I have basically made a hashmap with a String keyset and an Integer values. I am trying to return a players name and a players killcount from the hasmap. I have managed to get the kills but when I try to get the kills from the hashmap to get the players name it doesn't work. Here's the code.


    When I try this code everything is fine but this line which is confusing me because it gives me a nullpointer and I don't think it's the right way to get the string part of the hashmap with the integer.
     
  2. Offline

    mythbusterma

    sethrem

    HashMap#get() can return null.
     
  3. Offline

    TheCodingCat

    the get function in a hashmap returns the value of the key. Either it doesn't exist or you are casting the value to something it can not be cast to
     
  4. Offline

    sethrem

    Is there a function to return the value of the keyset from getting the value? Like using the integer to get the string.
     
  5. Offline

    fireblast709

    Nope. Then again, why would you want to get a player's name from the amount of kills a player has?
     
  6. Offline

    mythbusterma

    fireblast709 sethrem

    While it doesn't exist, it wouldn't be very difficult to write one, just incredibly slow. You might want to consider structuring it differently if the primary lookup you want to do is in the reverse order (the one that comes to mind would be a HashMap of Integers and LinkedLists).
     
  7. Offline

    sethrem

    It's for a minigame Scoreboard showing the Player's kills then name so like
    Code:
    10(For Kills) - NAME
    NVM Solved this problem myself figured out a pretty nice way just took 10 minutes to think of the code. Thanks anways.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  8. Offline

    fireblast709

    sethrem In the case you want a top n (n being 3, 5, whatever ;)), you are either looking for:
    • TreeMultimap<Integer, String>: Since TreeMaps are sorting based on key, you will have to switch around the key and value types. However, this will give you the issue that if two players have the same amount of kills, only one will be in the Map. TreeMultimap allows this, while still sorting properly. It's part of Google Guava, which is shaded into Bukkit. To get the top n, simply iterate over the Set you get from entries() (iirc)
    • A custom TreeMap<String, Integer>: As mentioned above, TreeMaps sort key based. A customised TreeMap however, is capable of sorting value based (example, though untested).
     
  9. Offline

    sethrem

    Thanks dude but I already got it.
     
  10. Offline

    fireblast709

    sethrem yea I had the page open for too long :p.
     
  11. Offline

    sethrem

    Ha, no problem mate thanks anyways.
     
Thread Status:
Not open for further replies.

Share This Page