英文:
How can I search if en element from one List exists in the elements in the other list and then print this element
问题
我有一个从属性文件中读取的字符串元素列表,然后返回一个包含这些元素的列表:
public static ArrayList<String> getExpextedTestDataForHeaderList() throws IOException {
ArrayList<String> testDataList = new ArrayList<String>();
testDataList.add(hpExpectedTxtStoiximaBtn());
testDataList.add(hpExpectedTxtLiveStoiximaBtn());
testDataList.add(hpExpectedTxtVirtualBtn());
testDataList.add(hpExpectedTxtCasinoBtn());
testDataList.add(hpExpectedTxtLiveCasinoBtn());
testDataList.add(hpExpectedTxtOtherGamesBtn());
testDataList.add(hpExpectedTxtLogInBtn());
testDataList.add(hpExpectedTxtRegisterBtn());
for (int i = 0; i < testDataList.size(); i++) {
String actualTitle = testDataList.get(i).toString();
//System.out.println("From File: " + actualTitle);
}
return testDataList;
}
然后我有一个方法来获取这些元素(`getElementsNames`)并将它们转换成第二个列表:
private static ArrayList<String> methodConvertElementToList(String path) {
ArrayList<String> list = getElementsNames(path);
for (int i = 0; i < list.size(); i++) {
return list;
}
return list;
}
到目前为止,当我执行程序时,我会得到两个包含元素的列表。
现在,我如何从第二个列表中搜索是否包含第一个列表的元素,然后将这些元素打印到屏幕上?
我想出了一些代码,但它不起作用。有人可以帮忙吗?
public static void printToScreen(String path, List<String> expectedDataList) throws IOException {
ArrayList<String> expectedNames = (ArrayList<String>) expectedDataList;
ArrayList<String> actualNames = getElementsNames(path);
//ListIterator<String> expectedNamesListIterator = expectedNames.listIterator();
//ListIterator<String> actualNamesListIterator = actualNames.listIterator();
}
使用迭代器吗?还是其他方法?
英文:
I have one List of String Elements reading from a property file and then returning a List of these elements:
public static ArrayList<String> getExpextedTestDataForHeaderList() throws IOException {
ArrayList<String> testDataList = new ArrayList<String>();
testDataList.add(hpExpectedTxtStoiximaBtn());
testDataList.add(hpExpectedTxtLiveStoiximaBtn());
testDataList.add(hpExpectedTxtVirtualBtn());
testDataList.add(hpExpectedTxtCasinoBtn());
testDataList.add(hpExpectedTxtLiveCasinoBtn());
testDataList.add(hpExpectedTxtOtherGamesBtn());
testDataList.add(hpExpectedTxtLogInBtn());
testDataList.add(hpExpectedTxtRegisterBtn());
for (int i = 0; i < testDataList.size(); i++) {
String actualTitle = testDataList.get(i).toString();
//System.out.println("From File: "+actualTitle);
}
return testDataList;
}
Then I have a method to get the elements (getElementsNames
) and convert them in to a second list
private static ArrayList<String> methodConvertElementToList(String path) {
ArrayList<String> list = getElementsNames(path);
for (int i = 0; i < list.size(); i++) {
return list;
}
return list;
}
Up to this point when I execute my program I get back 2 list with elements.
Now, how I can search from the second list if contains elements of the first list and then print those elements in screen?
I came up with some code but it is not working. Can anyone help with this?
public static void printToScreen(String path,List<String> expectedDataList) throws IOException {
ArrayList<String> expectedNames = (ArrayList<String>) expectedDataList;
ArrayList<String> actualNames = getElementsNames(path);
//ListIterator<String> expectedNamesListIterator = expectedNames.listIterator();
//ListIterator<String> actualNamesListIterator = actualNames.listIterator();
}
With iterators? Or some other way?
答案1
得分: 1
如果我理解正确,你可以简单地使用 listA.containsAll(listB)。示例代码如下:
List<String> listA = new ArrayList<>(
List.of("ala",
"adam",
"bocian",
"hello",
"world"));
List<String> listB = new ArrayList<>(
List.of("ala",
"hello"));
if(listA.containsAll(listB)){
System.out.println("YOUR CODE");
}
如果返回值为true,则表示listB中的所有元素都存在于listA中,然后你可以简单地打印出listB的所有元素。
另一种方式是,如果你不想检查所有元素,你可以获取listA的各个元素,然后检查它是否存在于listB中:
listA.stream()
.filter(listB::contains)
.forEach(System.out::println);
英文:
If i correctly understand, you can simply use a listA.containsAll(listB) example code looks like
List<String> listA = new ArrayList<>(
List.of("ala",
"adam",
"bocian",
"hello",
"world"));
List<String> listB = new ArrayList<>(
List.of("ala",
"hello"));
if(listA.containsAll(listB)){
System.out.println("YOUR CODE");
}
It returns true if all element of listB are present in listA, then you can simply print all elements of listB.
Other way if you don't wanna check all elements you can get individual element of listA and check if it present in listB
listA.stream()
.filter(listB::contains)
.forEach(System.out::println);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论