[英]Checking if a List of Lists contains a certain String
I have made a list of Countries based on Name, Population, Surface etc. I have used the Arrays.asList Methods to add each entry.我已经根据名称、人口、地表等列出了国家/地区列表。我使用了 Arrays.asList 方法来添加每个条目。 After that, all entries were added to a new list based on continents.之后,所有条目都添加到基于大陆的新列表中。 Eg例如
List<String> countriesAfrica = Arrays.asList("Nigeria", "Ethiopia", etc)
are added to List<Africa> africaList = new ArrayList<>();
List<String> countriesAfrica = Arrays.asList("Nigeria", "Ethiopia", etc)
添加到List<Africa> africaList = new ArrayList<>();
using a for loop使用 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)));
}
In order to search by Country Name, I have tried using the contains() method.为了按国家名称搜索,我尝试使用 contains() 方法。 I have added the country Africa to a List of Lists, as follows:我已将非洲国家添加到列表列表中,如下所示:
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);
}
Using the contains Method, list.contains("Nigeria");
使用 contains 方法, list.contains("Nigeria");
returns nothing or false when using SOUT.使用 SOUT 时不返回任何内容或返回 false。 Any ideas where I did wrong?我做错了什么想法?
Your new list has reordered the objects with a list of names, a list of populations, a list of surfaces, and a list of fertility.您的新列表已使用名称列表、人口列表、表面列表和生育力列表对对象进行重新排序。 This means the first element in the list, at index zero, will always contain a list of countries.这意味着列表中的第一个元素(索引为零)将始终包含国家列表。 Therefore, you should retrieve that list, and check it for .contains
因此,您应该检索该列表,并检查它是否.contains
System.out.println(list.get(0).contains("Nigeria"));
java.util.stream
编辑以获取更多说明和java.util.stream
It looks like you may have a fundamental misunderstanding of how the contains
method works.看起来您可能对contains
方法的工作原理存在根本性的误解。 The method is not recursive.该方法不是递归的。 So, when you use contains
on a list of lists, you can only check to see if the parent list contains a child list.因此,当您在列表的列表上使用contains
时,您只能检查父列表是否包含子列表。 Take for example the following code:以下面的代码为例:
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);
If I want to check my everything list to see if it contains the places
list, I can simply check it with the contains method.如果我想检查我的所有列表以查看它是否包含places
列表,我可以简单地使用 contains 方法检查它。
boolean containsList = everything.contains(places);
this returns true
.这返回true
。
However, boolean containsList = everything.contains(places.get(0))
returns false.但是, boolean containsList = everything.contains(places.get(0))
返回 false。 In fact, my IDE gives me a warning:事实上,我的 IDE 给了我一个警告:
List<List<String>>
may not contain objects of typeString
List<List<String>>
不能包含String
类型的对象
So, what if I want to check for the presence of "Bob"?那么,如果我想检查“Bob”是否存在怎么办? As the person who wrote this line of code, I know that Bob is the first element added to the list.作为编写这行代码的人,我知道 Bob 是添加到列表中的第一个元素。 Since Lists are ordered, I also know that Bob will always be located somewhere in the 0
index of my list.由于列表是有序的,我也知道 Bob 将始终位于列表的0
索引中的某个位置。 So, I can simply check if it exists in that index by using the contains method on the index itself.因此,我可以通过对索引本身使用 contains 方法来简单地检查它是否存在于该索引中。
boolean containsBob = everything.get(0).contains("Bob");
This also returns true
.这也返回true
。
But what if I don't know where in the list something exists at?但是,如果我不知道列表中的什么位置怎么办? What if it could be in any index?如果它可以在任何索引中怎么办? This is where streams come in.这是流进来的地方。
boolean caExists = everything.stream().anyMatch(list -> list.contains("California"));
This returns true as well.这也返回 true。 The difference in this and the previous method, which uses a known index, is that the stream will iterate through the lists contained within everything
and run the list.contains()
method on that list . Than, if any of the lists return
此方法与之前使用已知索引的方法的不同之处在于 stream 将遍历包含在everything
中的列表并在该列表上运行list.contains()
方法. Than, if any of the lists return
. Than, if any of the lists return
true , the result will be
true`. . Than, if any of the lists return
true , the result will be
true`。
You can see this demonstrated more effectively here:您可以在此处更有效地看到这一点:
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);
The bobExists
boolean uses a known index to check the existence of a known String. bobExists
boolean 使用已知索引来检查已知字符串是否存在。 The next two variables use stream to check the entire parent list.接下来的两个变量使用 stream 来检查整个父列表。 The containsList
checks the existence of a child list. containsList
检查是否存在子列表。 The invalidCheck
checks the existence of a String in the parent list. invalidCheck
检查父列表中是否存在 String。 Since the parent list can only contain a list of type String, this will always return false.由于父列表只能包含 String 类型的列表,因此这将始终返回 false。
The resulting output from running the above code is as follows:运行上述代码得到的 output 如下:
Contains Bob: true
Contains California: true
Contains nothing: false
Contains the places list: true
Invalid check of String to List: false
As you can see, the stream works to check the existence of a string in the child objects, regardless of location.如您所见,无论位置如何,stream 都会检查子对象中是否存在字符串。 This is the most versatile approach.这是最通用的方法。
I hope this helps clarify things.我希望这有助于澄清事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.