views:

49

answers:

1

I have a data model in which I have an Item that can belong to one or more Categories. I'm trying to display a <rich:listShuttle /> allowing the user to select which Categories the Item should belong to. I'm using RichFaces and Seam 2.2.x, and haven't run into any problems in setting the values initially. My source is a list of the defined Categories, while my target is the Item.values() attribute. Everything works fine when setting the Categories for the first time.

However, when I go in to edit an Item and change the Categories it is in, my <rich:listShuttle /> source list contains all of the Categories, not just the one the Item is not currently in. As a result, it is possible to move a duplicate Category from the source over to the target. This creates problems when I try and commit my Item to the database.

Is there any way to prevent these duplicates from being set? I would think there was a flag I could set, but haven't found anything yet.

A: 

Without seeing any code or truly understanding your problem, you can remove duplicates by using a Set instead of a List.

If your Categories has implemented equals() and hashCode() you can easily remove duplicates by putting everything in a Set before adding it to the list.

Set<Categories> uniqueSet = new HashSet<Categories>(categoriesCollection);
List<Categories> backToList = new ArrayList<Categories>(uniqueSet);
Shervin