简体   繁体   中英

How to get size of an arrayList which is within the hashmap

I have one hashmap that contains Player and an ArrayList of Locations. How do I get the size of initialized ArrayList variables?

For an example: I have an if state that requires next condition: if(X==2) X is supposed to be the size of initialized variables within the ArrayList

How can I access the size value of an arraylist that belongs to the hashmap?

HashMap<Player, ArrayList<Location>> who = new HashMap<Player, ArrayList<Location>>();

size : fist we get the list for the player which we get by calling get with the playername then (checking null just cause it might not have been initialized ) and the actual .size() function to get the amount

List<Location> locationList=who.get("<playerName>");
if(locationList!=null&&locationList.size()==2){
    ...
}

This is how your code should look like

if(who.get(player).size() == 2) {
    who.get(player).get(0);
    who.get(player).get(1);
}

To keep a null check on this, you can do something like this:

ArrayList<Location> locations = who.get(player);

if(locations != null && locations.size() == 2) {
    locations.get(0);
    locations.get(1);
}

I hope this is what you're looking for.

Here's how you could check your whole data structure for each Location list having two entries:

public class Test {
    public static void main(String... args) {

        // Representation of your data, which would obviously come fromo elsewhere.
        HashMap<Player, ArrayList<Location>> who = new HashMap<Player, ArrayList<Location>>();

        for (Player p : who.keySet()) {
            ArrayList<Location> locations = who.get(p);
            if (locations.size() == 2) {
                // Do something
            }
            else {
                // Do something else
            }
        }
    }
}

There are multiple options to achieve this. It matters in which case you want to check that. Eg if you only want to check if all players have two locations you can use Java Stream for that:

boolean allTwoLocations = who.values().stream().allMatch(locations -> locations.size() == 2);

If you want to check only a single player it matters if you already have the reference to that player or only an id or username of it. If you have an object of the player you want to check its as simple as this: who.get(player) . Remember to check if the player exists in the map to prevent a NullPointerException . You then can access the locations of that player:

if (who.containsKey(player) && who.get(player).size() ==2) {
    // do whatever you want
}

If you only have a property of that player (id, username, etc.) I also would recommend to use a Stream for that:

boolean userExistsAndTwoLocations = who.entrySet().stream()
        .filter(p -> p.getKey().getId() == id).findFirst()
        .map(Map.Entry::getValue)
        .filter(locations -> locations.size() == 2)
        .isPresent();

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