简体   繁体   English

java.util.Set,但对字段具有自定义约束

[英]java.util.Set, but with custom constraint on field

I wonder if there is a nice way to have Set on objects, but constrained on custom fields. 我想知道是否有一种在对象上设置Set的好方法,但是在自定义字段上有约束。

For example I need a Set of POJO's: 例如,我需要一Set POJO:

class MyPojo {
  private String name;
  private Map<Object, Object> stuff;
  //getters, setters, constructor
}

And I want to have a Set where they will be unique by name. 我想要一个Set ,它们在名称上将是唯一的。 I could implement hash and equals based on name property, but then I can't use really equals to compare them. 我可以基于name属性实现hashequals ,但是我不能使用真正的equals来比较它们。

Is there some nice workaround or maybe Guava/apache commons collection for this? 是否有一些不错的解决方法,或者为此提供番石榴/ Apache Commons集合?

You can create wrapper objects. 您可以创建包装对象。 It's like a decorator pattern. 这就像一个装饰器模式。 You wrap your original object with different hash implementations and then wrapped object will be used in the Set. 您用不同的哈希实现包装原始对象,然后包装的对象将在Set中使用。

You can use a HashMap<String,MyPojo> instead of a Set<MyPojo> . 您可以使用HashMap<String,MyPojo>代替Set<MyPojo> This way, each name can appear at most once in the Map . 这样,每个name最多只能在Map出现一次。

如何获取java.util.Set的填充字段<object>或者如果该字段为空,则返回其他内容?<div id="text_translate"><p> 我有一个地址 class,用于客户 class 的两个属性,第一个属性是一个简单的 object,代表客户的默认地址,另一个属性是客户可以拥有的地址集 <>。</p><p> 我需要将postalCode字段发送到另一个class。我想检查Set <>是否有填充的postalCode,如果有,发送这个值,否则(如果Set <>中没有填充的postalCode)那么我应该从默认地址发送 postalCode 值。 这该怎么做? 我以前试过这种方式:</p><pre> Optional.ofNullable(Stream.of(product.getCustomer().getAddresses()).forEach (i -> i.getPostalCode())).orElse(product.getCustomer().getAddressDefault().getPostalCode()))</pre><p> 我的课程如下:</p><pre> public class Customer { private Addresses addressDefault; private Set<Addresses> addresses; } public class Addresses { private String postalCode; private String city; private String state; private String country; }</pre></div></object> - How to get the filled field of java.util.Set <Object> or if the field is empty, return something else?

暂无
暂无

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

相关问题 如何将JSONObject转换为包含java.util.Set字段的对象 - How to convert a JSONObject to a object contains java.util.Set field ConcurrentModificationException on java.util.Set上的for循环 - ConcurrentModificationException on for loop on java.util.Set 用`_`定义java.util.Set的读取 - Defining Reads for java.util.Set with `_` 将 Java.Util.Set 加载到 Spring ConfigurationProperties - Loading Java.Util.Set in to Spring ConfigurationProperties 休眠和Java.util.set问题 - hibernate and Java.util.set problem java.util.Set无法通过ParameterizedType检查 - java.util.Set is is failing ParameterizedType check 如何获取java.util.Set的填充字段<object>或者如果该字段为空,则返回其他内容?<div id="text_translate"><p> 我有一个地址 class,用于客户 class 的两个属性,第一个属性是一个简单的 object,代表客户的默认地址,另一个属性是客户可以拥有的地址集 <>。</p><p> 我需要将postalCode字段发送到另一个class。我想检查Set <>是否有填充的postalCode,如果有,发送这个值,否则(如果Set <>中没有填充的postalCode)那么我应该从默认地址发送 postalCode 值。 这该怎么做? 我以前试过这种方式:</p><pre> Optional.ofNullable(Stream.of(product.getCustomer().getAddresses()).forEach (i -> i.getPostalCode())).orElse(product.getCustomer().getAddressDefault().getPostalCode()))</pre><p> 我的课程如下:</p><pre> public class Customer { private Addresses addressDefault; private Set<Addresses> addresses; } public class Addresses { private String postalCode; private String city; private String state; private String country; }</pre></div></object> - How to get the filled field of java.util.Set <Object> or if the field is empty, return something else? 在Scala中将java.util.Set转换为java.util.List - Converting a java.util.Set to java.util.List in Scala 优化java.util.Map和java.util.Set的实现? - Optimized implementations of java.util.Map and java.util.Set? java.util.Set,java.util.List可序列化的问题 - java.util.Set, java.util.List Serializable issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM