简体   繁体   English

如何根据键进行过滤以选择两个值中的任何一个

[英]how to filter based on the key to pick either of the two value

I am facing a challenge where my use case is:我面临一个挑战,我的用例是:

OrderItem orderItem = ServicesUtil.getOrderItemValue(drawStore.getOrderItem().getItem(), "Item1");

if(null==orderItem){
    orderItem = ServicesUtil.getOrderItemValue(drawStore.getOrderItem().getItem(), "Item2");
}

Here, as per the key with "Item1" if not found, my order item will be null, so I had checked for "Item2", and subsequent I can get the whole payload in case, key "Item1" is not found.在这里,如果没有找到带有“Item1”的键,我的订单项目将是 null,所以我检查了“Item2”,随后我可以获得整个有效负载,以防找不到键“Item1”。

But this brings a term, where, my order item is now getting a reference to null which should never be dereferenced/accessed.但这带来了一个术语,我的订单项现在得到对 null 的引用,它永远不应该被取消引用/访问。

In this case, how can I filter based on the key and not really go for checking order item as null to avoid Null pointers dereferencing issue?在这种情况下,如何根据键而不是真正的 go 进行过滤,以检查订单项为 null 以避免 Null 指针取消引用问题?

So for resolving this, used:所以为了解决这个问题,使用:

Optional.ofNullable(ServicesUtil.getOrderItemValue(drawStore.getOrderItem().getItem(), "pickle")).orElseGet(()->ServicesUtil.getOrderItemValue(drawStore.getOrderItem().getItem(), "ketchup"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM