[英]Get unique Object from a Stream if present
Starting with a bean class MyBean
with a single relevant propterty: 从具有单个相关属性的bean类
MyBean
开始:
@Data
class MyBean {
private String myProperty;
}
Now I have got a set of these beans Set<MyBean> mySet
usually with 0, 1, or 2 elements. 现在,我有了一组这些bean
Set<MyBean> mySet
通常带有0、1或2个元素。
The question is: How do I retrieve myProperty
from this set if it is equal for all elements, or else null. 问题是:如果所有元素均相等,则如何从该集中检索
myProperty
,否则为null。 Preferably in a single line with effort O(n). 优选地,在单线中具有努力O(n)。
I found several examples to determine the boolean if all properties are equal. 我发现了几个确定所有属性是否相等的布尔值的示例。 But I want to know the corresponding property.
但是我想知道相应的属性。
Is there something smarter than this? 有比这更聪明的东西吗?
String uniqueProperty = mySet.stream().map(MyBean::getMyProperty).distinct().count() == 1
? mySet.stream().map(MyBean::getMyProperty).findAny().orElse(null)
: null;
Your version is already O(n)
. 您的版本已经是
O(n)
。
It's possible to do this with a one-liner (although yours is too depending on how you write it). 可以使用单线执行此操作(尽管您也取决于编写方式)。
String uniqueProperty = mySet.stream()
.map(MyBean::getMyProperty)
.map(Optional::ofNullable)
.reduce((a, b) -> a.equals(b) ? a : Optional.empty()) // Note: equals compares 2 Optionals here
.get() // unwraps first Optional layer
.orElse(null); // unwraps second layer
The only case this doesn't work for is when all property values are null
. 唯一不起作用的情况是所有属性值均为
null
。 You cannot distinguish the set (null, null)
from (null, "A")
for example, they both return null
. 例如,您无法将
(null, null)
与(null, "A")
区分,它们都返回null
。
Just a single iteration without the use of streams looks much better for such a use case : 对于这样的用例,仅一次迭代而不使用流看起来更好:
Iterator<MyBean> iterator = mySet.iterator();
String uniqueProperty = iterator.next().getMyProperty();
while (iterator.hasNext()) {
if (!iterator.next().getMyProperty().equals(uniqueProperty)) {
uniqueProperty = null; // some default value possibly
break;
}
}
You use the findAny()
first and check mySet
again with allMatch()
to require all items to match the first one in a filter()
: 您首先使用
findAny()
, mySet
再次使用allMatch()
检查mySet
,以要求所有项目都与filter()
中的第一个项目匹配:
String uniqueProperty = mySet.stream().findAny().map(MyBean::getMyProperty)
.filter(s -> mySet.stream().map(MyBean::getMyProperty).allMatch(s::equals))
.orElse(null);
The advantage of this is, that allMatch()
will only evaluate all elements if necessary ( docs ). 这样做的好处是,
allMatch()
仅在必要时评估所有元素( docs )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.