简体   繁体   English

如何获取哈希图中的arrayList的大小

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

I have one hashmap that contains Player and an ArrayList of Locations. 我有一个哈希图,其中包含Player和Location的ArrayList。 How do I get the size of initialized ArrayList variables? 如何获取初始化的ArrayList变量的大小?

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 例如:我有一个if状态,需要下一个条件:if(X == 2)X应该是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 size:首先,我们通过使用玩家名称调用get来获得玩家的列表(检查null只是因为它可能尚未初始化),然后使用实际的.size()函数来获取金额

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: 例如,如果您只想检查所有播放器是否都具有两个位置,则可以为此使用Java Stream:

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. 如果您只想检查一个玩家,那么您是否已经拥有对该玩家的引用或该角​​色的ID或用户名就很重要。 If you have an object of the player you want to check its as simple as this: who.get(player) . 如果您有播放器的对象, who.get(player)检查它的简单程度: who.get(player) Remember to check if the player exists in the map to prevent a NullPointerException . 记住要检查玩家是否在地图上,以防止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: 如果您只有该播放器的属性(ID,用户名等),我也建议您使用流媒体播放器:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM