英文:
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<Pair> list1 = Stream.of(
new Pair("key1", "value1"),
new Pair("key2", "value2")
)
.collect(Collections.toList());
List<Pair> list2 = Stream.of(
new Pair("key1", "value2"),
new Pair("key2", "value3")
)
.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-> list2.stream()
.anyMatch(pair->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 -> list2.stream()
.anyMatch(pair2-> pair.getKey().equals(pair2.getKey()) ) && 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() &&
IntStream.range(0, list1.size())
.allMatch(i -> 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<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.length !=current.length;
return hasCountChanged || hasElementChanged;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论