Check how many values a key has in a hashmap?

Discussion in 'Plugin Development' started by BurnerDiamond, Apr 11, 2015.

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

    BurnerDiamond

    Titles says it all!
     
  2. Offline

    Scullyking

    Loop through hashmap and if key = keyLookingFor then increment
     
  3. Offline

    BurnerDiamond

    I'm not checking for the key.

    I'm checking if a KEY has more then 2 VALUES.

    Something like:

    if(hashmap.get(Player).size() == 1) {
     
  4. Offline

    Skionz

    A key can only have one value.
     
  5. Offline

    BurnerDiamond

    Ah you're right!

    Unless I make something like this.

    Map<Integer,ArrayList<Player>> map =newHashMap<Integer,ArrayList<Player>>();
     
  6. Offline

    nverdier

    @BurnerDiamond There is still only one value. But that value happens to be an ArrayList. Just get how many objects are in the array list.
     
  7. you can get the arraylist from the key.
    and get the size of the arraylist with .size()
     
  8. Offline

    Konato_K

    If you want to have your Maps having more than one key (and not explicitly using a Collection as the value) you can use a MultiMap, which is part of Apache Commons, internally the values are stored as a Collection, but the MultiMap#put method will add values instead of replacing the ones that already exist.
     
  9. Offline

    BurnerDiamond

    How am I supposed to use it?

    It doesn't give me the choice of using a MultiMap meaning it doesn't exist.
     
  10. Offline

    mythbusterma

    @BurnerDiamond

    Since you obviously don't know enough Java to be doing what he said, just use a Map of something to a List or Set.
     
  11. Offline

    nj2miami

    Why would you do this though? Create a custom class 'Manager', for instance. Place a HashMap<Int, Player> inside, add some methods to set/get/return size, store a List<Manager>, done.
     
  12. @nj2miami Why don't let him do it.
    Its simple to solve your problem:
    just get the value (ArrayList) of the hashmap with hashmap.get(Object key) and do stuff with it, like gettings its size with .size()
     
  13. Offline

    BurnerDiamond

    The problem with this is that I need to make UHC teams. I could maybe make a constructor

    private ChatColor color;
    private List<String> players;

    public Team(ChatColor colorOfTeam, ArrayList<String> playersInTeam) {
    this.color = colorOfTeam;
    this.players = playersInTeam;


    Sound like an idea?
     
  14. Offline

    SuperOriginal

    That's called Object Oriented Programming, so yes.
     
Thread Status:
Not open for further replies.

Share This Page