Solved Looping through an HasMap

Discussion in 'Spigot Plugin Development' started by Alex0589, Apr 17, 2018.

  1. Hi spigot users,
    for my plugin I would need to create a method that returns all the teams of an Arena. My hash map looks like this:
    Code (Text):
    HashMap<Arena, Team> teams = new HashMap<>() ;
    My method so should return a List of Teams or null if there are no teams. How can I do it?
  2. A List of Teams? You're only storing a Team for each Arena.
  3. Sorry I meant a Team
  4. Assuming you want to use the keys, then do this:

    Code (Text):
    for(Arena arena : teams.keyset()){
    return teams.get(arena);
  5. I believe you would have to use Lambda Expressions in forEach()?
  6. Also in checking if no teams just use
    Code (Text):
    if(teams.size() == 0){
      return null;
  7. Iterate over Map#entrySet.
  8. If you didn't know it, there's a HUGE difference between null and 0 in java
  9. I don’t know where you got teams.size() from.
  10. Anyway I'm not using the API's team system
  11. That is the method?, How else do you get a hashmap's size?
  12. lmao idk why i read "teams" as a Team object
  13. Not quite sure if this is what you want, but if you want to get all the teams from the map as a list you can do this
    Code (Text):
        public List<Team> getTeams() {
            List<Team> list = new ArrayList<>();
            return list;
    If there is no teams it will return an empty list.

Share This Page