1

I am trying to add SelectItem objects to Set collection. However, duplicate values are being added. Is there any way to prevent these duplicate values?

for(String s: list) {       
  Set<SelectItem> typeSet = new HashSet<SelectItem>();
  typeSet.add(new SelectItem(s));
}
4

1 回答 1

2

只需使用Set<String>而不是Set<SelectItem>. /根本没有实施以考虑实际值SelectItem#equals()#hashCode()

private Set<String> typeSet;

@PostConstruct
public void init() {
    List<String> list = getItSomehow();
    typeSet = new LinkedHashSet<String>(list);
}

这是因为 JSF2 也可以使用<f:selectItems>

<f:selectItems value="#{bean.typeSet}" />

也可以看看:

于 2013-09-23T19:47:51.807 回答