繁体   English   中英

检查列表列表是否包含某个字符串

[英]Checking if a List of Lists contains a certain String

我已经根据名称、人口、地表等列出了国家/地区列表。我使用了 Arrays.asList 方法来添加每个条目。 之后,所有条目都添加到基于大陆的新列表中。 例如

List<String> countriesAfrica = Arrays.asList("Nigeria", "Ethiopia", etc)添加到List<Africa> africaList = new ArrayList<>(); 使用 for 循环

for (int k = 0; k< countriesAfrica.size(); k++) {
        africaList.add(new Africa(countriesAfrica.get(k), 
                              populationAfrica.get(k),
                              surfaceAfrica.get(k), 
                              fertilityAfrica.get(k)));
}

为了按国家名称搜索,我尝试使用 contains() 方法。 我已将非洲国家添加到列表列表中,如下所示:

List<List<String>> list = new ArrayList<>();
for (int i = 0; i < 4; i++) {
    List<String> l = new ArrayList<>();
    for (int j = 0; j < africaList.size(); j++) {
        if (i == 0) {
            l.add(africaList.get(j).getName());
        }
        if (i == 1) {
            l.add(String.valueOf(africaList.get(j).getSurface()));
        }
    }
    list.add(l);
}

使用 contains 方法, list.contains("Nigeria"); 使用 SOUT 时不返回任何内容或返回 false。 我做错了什么想法?

原始答案

您的新列表已使用名称列表、人口列表、表面列表和生育力列表对对象进行重新排序。 这意味着列表中的第一个元素(索引为零)将始终包含国家列表。 因此,您应该检索该列表,并检查它是否.contains

System.out.println(list.get(0).contains("Nigeria"));

编辑以获取更多说明和java.util.stream

看起来您可能对contains方法的工作原理存在根本性的误解。 该方法不是递归的。 因此,当您在列表的列表上使用contains时,您只能检查父列表是否包含子列表。 以下面的代码为例:

List<String> names = new ArrayList<>();
List<String> places = new ArrayList<>();
List<String> things = new ArrayList<>();
List<List<String>> everything = new ArrayList<>();

names.add("Bob");
names.add("Steve");
places.add("California");
places.add("New York");
things.add("Watch");
things.add("cars");

everything.add(names);
everything.add(places);
everything.add(things);

如果我想检查我的所有列表以查看它是否包含places列表,我可以简单地使用 contains 方法检查它。

boolean containsList = everything.contains(places);

这返回true

但是, boolean containsList = everything.contains(places.get(0))返回 false。 事实上,我的 IDE 给了我一个警告:

List<List<String>>不能包含String类型的对象

那么,如果我想检查“Bob”是否存在怎么办? 作为编写这行代码的人,我知道 Bob 是添加到列表中的第一个元素。 由于列表是有序的,我也知道 Bob 将始终位于列表的0索引中的某个位置。 因此,我可以通过对索引本身使用 contains 方法来简单地检查它是否存在于该索引中。

boolean containsBob = everything.get(0).contains("Bob");

这也返回true

但是,如果我不知道列表中的什么位置怎么办? 如果它可以在任何索引中怎么办? 这是流进来的地方。

boolean caExists = everything.stream().anyMatch(list -> list.contains("California"));

这也返回 true。 此方法与之前使用已知索引的方法的不同之处在于 stream 将遍历包含在everything中的列表并在该列表上运行list.contains()方法. Than, if any of the lists return . Than, if any of the lists return true , the result will be true`。

示范

您可以在此处更有效地看到这一点:

boolean bobExists = everything.get(0).contains("Bob");
boolean caExists = everything.stream().anyMatch(list -> list.contains("California"));
boolean nothingExists = everything.stream().anyMatch(list -> list.contains("nothing"));
boolean containsList = everything.contains(places);
boolean invalidCheck = everything.contains("Bob");

System.out.println("Contains Bob: " + bobExists);
System.out.println("Contains California: " + caExists);
System.out.println("Contains nothing: " + nothingExists);
System.out.println("Contains the places list: " + containsList);
System.out.println("Invalid check of String to List: " + invalidCheck);

bobExists boolean 使用已知索引来检查已知字符串是否存在。 接下来的两个变量使用 stream 来检查整个父列表。 containsList检查是否存在子列表。 invalidCheck检查父列表中是否存在 String。 由于父列表只能包含 String 类型的列表,因此这将始终返回 false。

运行上述代码得到的 output 如下:

Contains Bob: true
Contains California: true
Contains nothing: false
Contains the places list: true
Invalid check of String to List: false

如您所见,无论位置如何,stream 都会检查子对象中是否存在字符串。 这是最通用的方法。

我希望这有助于澄清事情。

暂无
暂无

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

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