How to get the filled field of java.util.Set <Object> or if the field is empty, return something else?

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

How to get the filled field of java.util.Set <Object> or if the field is empty, return something else?

问题

我有一个 Addresses 类,在 Customer 类的两个属性中使用,第一个属性是一个简单的对象,用于表示客户的默认地址,另一个属性是一个包含可能存在的地址的 Set

我需要将 postalCode 字段发送到另一个类。我想在 Set 中检查是否有填充的 postalCode,如果是,则发送该值,否则(如果 Set 中没有填充的 postalCode),那么我应该发送默认地址的 postalCode 值。如何做到这一点?我之前尝试过这种方式:

  1. Optional.ofNullable(Stream.of(product.getCustomer().getAddresses())
  2. .map(i -> i.getPostalCode())
  3. .filter(Objects::nonNull)
  4. .findFirst()
  5. .orElse(product.getCustomer().getAddressDefault().getPostalCode()));

我的类如下:

  1. public class Customer {
  2. private Addresses addressDefault;
  3. private Set<Addresses> addresses;
  4. }
  5. public class Addresses {
  6. private String postalCode;
  7. private String city;
  8. private String state;
  9. private String country;
  10. }
英文:

I have an Addresses class that is used in two attributes of the Customer class, the first attribute is a simple object to represent the customer's default address and another attribute is a Set <> of addresses that the customer can have.

I need to send the postalCode field to another class. I would like to check in Set <> if there is a filled postalCode, if yes, send this value, otherwise (if there is no filled postalCode within Set <>) then I should send the postalCode value from the default address. How to do this? I tried this way before:

  1. Optional.ofNullable(Stream.of(product.getCustomer().getAddresses()).forEach
  2. (i -&gt; i.getPostalCode())).orElse(product.getCustomer().getAddressDefault().getPostalCode()))

My classes bellow:

  1. public class Customer {
  2. private Addresses addressDefault;
  3. private Set&lt;Addresses&gt; addresses;
  4. }
  5. public class Addresses {
  6. private String postalCode;
  7. private String city;
  8. private String state;
  9. private String country;
  10. }

答案1

得分: 1

使用Java 8的流,您可以使用map将地址转换为其postalCode,使用filter来删除null值,并使用findFirst来获取第一个非null的postalCode

Stream.findFirst会通过返回一个Optional来处理空流。要获取其值或默认值,请使用Optional.orElse方法。

在代码中:

  1. String postalCode = customer.addresses.stream()
  2. .map(addresses -> addresses.postalCode)
  3. .filter(Objects::nonNull)
  4. .findFirst()
  5. .orElse(customer.addressDefault.postalCode);
英文:

Using Java 8 streams, you can use map to transform an address to its postalCode, use filter to remove null values, and use findFirst to get the first non-null postalCode.

Stream.findFirst deals with empty streams by returning an optional. To get its value or a default value, use the Optional.orElse method.

In code:

  1. String postalCode = customer.addresses.stream()
  2. .map(addresses -&gt; addresses.postalCode)
  3. .filter(Objects::nonNull)
  4. .findFirst()
  5. .orElse(customer.addressDefault.postalCode);

huangapple
  • 本文由 发表于 2020年8月19日 09:47:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63478876.html
匿名

发表评论

匿名网友

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

确定