Java 如何获取hashmap中的arrayList的大小

Java 如何获取hashmap中的arrayList的大小,java,Java,我有一个hashmap,其中包含播放器和位置的ArrayList。 如何获取初始化的ArrayList变量的大小 例如: 我有一个需要下一个条件的if状态:if(X==2) X应该是ArrayList中初始化变量的大小 如何访问属于hashmap的arraylist的大小值 HashMap<Player, ArrayList<Location>> who = new HashMap<Player, ArrayList<Location>>();

我有一个hashmap,其中包含播放器和位置的ArrayList。 如何获取初始化的ArrayList变量的大小

例如: 我有一个需要下一个条件的if状态:if(X==2) X应该是ArrayList中初始化变量的大小

如何访问属于hashmap的arraylist的大小值

HashMap<Player, ArrayList<Location>> who = new HashMap<Player, ArrayList<Location>>();
HashMap who=newhashmap();
尺寸: 首先,我们通过调用玩家名称获取玩家列表 然后(检查null只是因为它可能尚未初始化)和实际的.size()函数来获取金额

List<Location> locationList=who.get("<playerName>");
if(locationList!=null&&locationList.size()==2){
    ...
}
List locationList=who.get(“”);
if(locationList!=null&&locationList.size()=2){
...
}
尺寸: 首先,我们通过调用玩家名称获取玩家列表 然后(检查null只是因为它可能尚未初始化)和实际的.size()函数来获取金额

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

这就是您的代码的外观

if(who.get(player).size() == 2) {
    who.get(player).get(0);
    who.get(player).get(1);
}
要对此保持空检查,可以执行以下操作:

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

if(locations != null && locations.size() == 2) {
    locations.get(0);
    locations.get(1);
}
arraylistlocations=who.get(玩家);
if(locations!=null&&locations.size()=2){
位置。获取(0);
位置。获取(1);
}

我希望这就是您想要的。

这就是您的代码应该是什么样子

if(who.get(player).size() == 2) {
    who.get(player).get(0);
    who.get(player).get(1);
}
要对此保持空检查,可以执行以下操作:

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

if(locations != null && locations.size() == 2) {
    locations.get(0);
    locations.get(1);
}
arraylistlocations=who.get(玩家);
if(locations!=null&&locations.size()=2){
位置。获取(0);
位置。获取(1);
}

我希望这就是您要查找的内容。

以下是如何检查每个位置列表的整个数据结构,每个位置列表有两个条目:

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
            }
        }
    }
}
公共类测试{
公共静态void main(字符串…参数){
//数据的表示形式,显然来自其他地方。
HashMap who=新HashMap();
for(播放器p:who.keySet()){
arraylistlocations=who.get(p);
if(locations.size()==2){
//做点什么
}
否则{
//做点别的
}
}
}
}

以下是如何检查每个位置列表(包含两个条目)的整个数据结构:

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
            }
        }
    }
}
公共类测试{
公共静态void main(字符串…参数){
//数据的表示形式,显然来自其他地方。
HashMap who=新HashMap();
for(播放器p:who.keySet()){
arraylistlocations=who.get(p);
if(locations.size()==2){
//做点什么
}
否则{
//做点别的
}
}
}
}

实现这一点有多种选择。在这种情况下,你需要检查它,这很重要。例如,如果您只想检查所有玩家是否有两个位置,您可以使用Java Stream:

boolean allTwoLocations = who.values().stream().allMatch(locations -> locations.size() == 2);
boolean userExistsAndTwoLocations = who.entrySet().stream()
        .filter(p -> p.getKey().getId() == id).findFirst()
        .map(Map.Entry::getValue)
        .filter(locations -> locations.size() == 2)
        .isPresent();
如果您只想检查一个玩家,那么您是否已经拥有该玩家的引用或该玩家的id或用户名就很重要了。如果你有一个玩家的对象,你想检查它是否像这样简单:
who.get(player)
。请记住检查地图中是否存在播放器,以防止出现
NullPointerException
。然后,您可以访问该播放器的位置:

if (who.containsKey(player) && who.get(player).size() ==2) {
    // do whatever you want
}
如果您只有该玩家的属性(id、用户名等),我还建议您使用流:

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

实现这一点有多种选择。在这种情况下,你需要检查它,这很重要。例如,如果您只想检查所有玩家是否有两个位置,您可以使用Java Stream:

boolean allTwoLocations = who.values().stream().allMatch(locations -> locations.size() == 2);
boolean userExistsAndTwoLocations = who.entrySet().stream()
        .filter(p -> p.getKey().getId() == id).findFirst()
        .map(Map.Entry::getValue)
        .filter(locations -> locations.size() == 2)
        .isPresent();
如果您只想检查一个玩家,那么您是否已经拥有该玩家的引用或该玩家的id或用户名就很重要了。如果你有一个玩家的对象,你想检查它是否像这样简单:
who.get(player)
。请记住检查地图中是否存在播放器,以防止出现
NullPointerException
。然后,您可以访问该播放器的位置:

if (who.containsKey(player) && who.get(player).size() ==2) {
    // do whatever you want
}
如果您只有该玩家的属性(id、用户名等),我还建议您使用流:

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

可能的
NullPointerException
。可能的
NullPointerException
。通过迭代hashmap via的键,然后对于每个值,可以通过hashmap via的键获得arraylisterate的长度,然后对于每个值,可以获得arraylisterate的长度,尽管这可以回答问题,您的代码没有解释。请更新您的答案,解释您正在做的事情。谢谢即使这可能回答了这个问题,也没有对代码的解释。请更新您的答案,解释您正在做的事情。谢谢