Java流比较两个列表之间的属性是否相等,返回true或false。

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

Java streams compare property is equal between two lists and return true or false

问题

class Pair {
    String key;
    String value;
}
List<Pair> list1 = Stream.of(
        new Pair("key1", "value1"),
        new Pair("key2", "value2")
    )
    .collect(Collectors.toList());

List<Pair> list2 = Stream.of(
        new Pair("key1", "value2"),
        new Pair("key2", "value3")
    )
    .collect(Collectors.toList());

我想对list2中的值进行一些更改,然后将其与list1进行比较。

我想要检查在与list1比较时,list2中的所有项目的key属性是否未发生更改。只有value属性可以在list2中更改。

还要求list2中的项目数与list1相同,即 list1.size() = list2.size()

我尝试编写一个返回布尔值的流,但是在某个地方我一定搞错了。

list1.stream()
    .allMatch(pair2 -> list2.stream()
        .anyMatch(pair -> pair.getKey().equals(pair2.getKey())));
    // 还需要添加 list size() 的比较

更新:
我成功地编写了一个类似于这样的流,junit 测试似乎也能工作,尽管它没有像 ernest_k 的答案那样比较相同索引处的项目。

list1.stream()
    .allMatch(pair -> list2.stream()
        .anyMatch(pair2 -> pair.getKey().equals(pair2.getKey()) ) && list1.size() == list2.size());
英文:
class Pair {
    String key;
    String value;
}
List&lt;Pair&gt; list1 = Stream.of(
        new Pair(&quot;key1&quot;, &quot;value1&quot;),
        new Pair(&quot;key2&quot;, &quot;value2&quot;)
    )
    .collect(Collections.toList());

List&lt;Pair&gt; list2 = Stream.of(
        new Pair(&quot;key1&quot;, &quot;value2&quot;),
        new Pair(&quot;key2&quot;, &quot;value3&quot;)
    )
    .collect(Collections.toList());

I want to perform some changes in the values in list2 and then compare it to list1.

I want to check if the property key has not changed in list2 for all items in the list in comparison with list1 . Only value property can be changed in list2.

And that the number of items in list2 is the same as list1 list1.size() = list2.size()

I am trying to write a stream that returns a boolean but somewhere I must have been mistaken

list1.stream()
    .allMatch(pair2-&gt;  list2.stream()
        .anyMatch(pair-&gt;pair.getKey().equals(pair2.getKey())));
    // Need to add list size() comparison too

Update:
I managed to write a stream like this, junit tests seem to work, although it does not compare items at the same index as ernest_k's answer.

   list1.stream()
        .allMatch(pair -&gt; list2.stream()
            .anyMatch(pair2-&gt; pair.getKey().equals(pair2.getKey()) ) &amp;&amp; list1.size()== list2.size()
            ));

答案1

得分: 1

你可以使用:

boolean result = list1.size() == list2.size() && 
    IntStream.range(0, list1.size())
        .allMatch(i -> list1.get(i).getKey().equals(list2.get(i).getKey()));

由于你需要逐个比较列表的元素,所以不能使用嵌套流(就像你的示例中那样),那样会执行一个笛卡尔积操作,并且如果每个list1元素都与任何具有相同键的list2元素匹配,则会返回true(而你希望该比较考虑索引)。

英文:

You can use:

boolean result = list1.size() == list2.size() &amp;&amp; 
        IntStream.range(0, list1.size())
		    .allMatch(i -&gt; list1.get(i).getKey().equals(list2.get(i).getKey()));

As you have to compare the lists element by element, you can't use a nested stream as that (like in your example), would simply do a cartesian join and return true if each list1 element has any list2 element with the same key (whereas you want that comparison to be index-aware)

答案2

得分: 1

你可以构建一个查找表来引用原始键。

public static boolean compareStates(List<Pair> original, List<Pair> current) {
    Map<String, Pair> lookupTable = original.stream().collect(Collectors.toMap(x -> x.key, x -> x));

    boolean hasElementChanged = current.stream().filter(pair -> !lookupTable.containsKey(pair.key)).count() > 0;

    boolean hasCountChanged = original.size() != current.size();

    return hasCountChanged || hasElementChanged;
}

注意:由于代码部分不要翻译,我只提供了代码的翻译部分。

英文:

You may consruct a lookup table to refer original keys.

public static compareStates(List&lt;Pair&gt; original, List&lt;Pair&gt; current){
    
    	Map&lt;String,Pair&gt; lookupTable= original.stream.collect(Collectors.toMap(x -&gt;x.key,x-&gt;x));
    
    	boolean hasElementChanged = current.stream().filter(pair -&gt; !lookupTable.containsKey(pair.key)).count() &gt;0;
    
    	boolean hasCountChanged = original.length !=current.length;
    
    	return hasCountChanged || hasElementChanged;
    }

huangapple
  • 本文由 发表于 2020年6月29日 16:16:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/62633870.html
匿名

发表评论

匿名网友

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

确定