How can I search if en element from one List exists in the elements in the other list and then print this element

huangapple go评论71阅读模式
英文:

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&lt;String&gt; getExpextedTestDataForHeaderList() throws IOException {
ArrayList&lt;String&gt; testDataList = new ArrayList&lt;String&gt;();
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 &lt; testDataList.size(); i++) {
String actualTitle = testDataList.get(i).toString();
//System.out.println(&quot;From File: &quot;+actualTitle);
}
return testDataList;
}

Then I have a method to get the elements (getElementsNames) and convert them in to a second list

private static ArrayList&lt;String&gt; methodConvertElementToList(String path) {
ArrayList&lt;String&gt; list = getElementsNames(path);
for (int i = 0; i &lt; 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&lt;String&gt; expectedDataList) throws IOException {
ArrayList&lt;String&gt; expectedNames = (ArrayList&lt;String&gt;) expectedDataList;
ArrayList&lt;String&gt; actualNames = getElementsNames(path);
//ListIterator&lt;String&gt; expectedNamesListIterator = expectedNames.listIterator();
//ListIterator&lt;String&gt; 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&lt;String&gt; listA = new ArrayList&lt;&gt;(
List.of(&quot;ala&quot;,
&quot;adam&quot;,
&quot;bocian&quot;,
&quot;hello&quot;,
&quot;world&quot;));
List&lt;String&gt; listB = new ArrayList&lt;&gt;(
List.of(&quot;ala&quot;,
&quot;hello&quot;));
if(listA.containsAll(listB)){
System.out.println(&quot;YOUR CODE&quot;);
}

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);

huangapple
  • 本文由 发表于 2020年8月22日 20:41:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63536311.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定