• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sortable Collection with indexed properties  RSS feed

 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure my subject actually states what I mean. Most of the DAO layer on the current project uses SortedSet's. However, the web framework we are using allows you to bind indexed properties. So...

<input type="text" name="names[0]" />
<input type="text" name="names[1]" />

would easily bind to:

List<User> names;

However, if using a SortedSet I cannot make the binding work. Is there a Collection implementation class that acts like SortedSet but has indexes like a List?

Thanks.
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try Tree Map and tell me what happens.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was hoping for an Interface.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you want either

SortedMap<String, List<String>>

or the new improved version

NavigableMap<String, List<String>>

TreeMap now implements both.

You still have to do some extra work to handle nulls, e.g. to add an element


I would probably create a custom class that wraps a TreeMap<String, List<String>> to make it easier to use.

Alternately, Apache Commons has MultiMap. That gives you a Collection rather than a List. Though it looks like you can supply a factory to ensure the Collection is specifically a List. But offhand, without much research, I suspect the custom class suggested above is easier.
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Gregg Bolinger: ]   indexes like a List?

interface List extends Collection:   An ordered collection (also known as a sequence). The user of List interface has precise control over where in the list each element i inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.

How exactly to you intend to implement names[] ? Controlling position and how the elements are retrieved is an important design decision.

TreeMap class guarantees that the map will be in ascending key order, sorted according to the natural order for the key's class (see Comparable), or by the comparator provided at creation time, depending on which constructor is used. String will likely order on: "Compares two strings lexicographically", which will not be effective if you are expecting the n-th list to be the n-th list. If you only be able to get name / list pairs, then using a List Interface may require keeping a separate index, introducing complexity. Thus: interface Map<K,V> provides power of retrieval for large datasets, and is an interface, (as asked for), but you then get Iterations that likely do not behave like [];( i.e. 1,2,3,4,5,6,7,8,9 etc)
[ October 17, 2007: Message edited by: Nicholas Jordan ]
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nicholas Jordan:
[Gregg Bolinger: ]   indexes like a List?

interface List extends Collection:   An ordered collection (also known as a sequence). The user of List interface has precise control over where in the list each element i inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.

How exactly to you intend to implement names[] ? Controlling position and how the elements are retrieved is an important design decision.

TreeMap class guarantees that the map will be in ascending key order, sorted according to the natural order for the key's class (see Comparable), or by the comparator provided at creation time, depending on which constructor is used. String will likely order on: "Compares two strings lexicographically", which will not be effective if you are expecting the n-th list to be the n-th list. If you only be able to get name / list pairs, then using a List Interface may require keeping a separate index, introducing complexity. Thus: interface Map<K,V> provides power of retrieval for large datasets, and is an interface, (as asked for), but you then get Iterations that likely do not behave like [];( i.e. 1,2,3,4,5,6,7,8,9 etc)

[ October 17, 2007: Message edited by: Nicholas Jordan ]


Umm, I need to get selected elements from an HTML select into my list in the controller. Sequence does not matter. I wonder if you could have said all that without saying all that. :roll:
 
bart zagers
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about wrapping your SortedSet with a ListOrderedSet?
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Gregg Bolinger:]   Umm, I need to get selected elements from an HTML select into my list in the controller. Sequence does not matter. I wonder if you could have said all that without saying all that.
Well yes and no. But you said DAO, and that is what thoses fancy things are supposed to do for (us) - and why they sometimes don't is why I said all that by saying all that. ( emphasis just to set the sentence off from the rest of the reply )

Simplified answer is: Use a TreeSet, apply the html form index as keys and the value as the elements of the tree set, but I must restrict this advice because if the tool did not do the work for you, then the tool did not do the work for you.

Please, do not laugh. I do not mind being the subject of a boiling rejoinder, it's that this is what is going on here and I have been through thousands of these, and they always eventuate to the same nexus. You can, for example, do a .size() or .length() or whatever sizing/element count is provided for whatever they are giving you to work with. It seems pretty simple and obvious until ( whomever, for whatever reason ) implements a framework attempts to hide workings as much as possible. The reason people and designs do this is the same reason my replies get detailed. There is a foreach that has been implemented in 1.5 and if the people who wrote "the current project" will have been careful to implement copy semantics before giving you the Collection. Consider this:


I have been through enough of these to see smoke 3-6 months from now.

Either stick with the data-abstraction layer model, or be prepared to ice/heat your wrists and do fingertip pushups to prevent carpal-tunnel.

That is not a joke. Sounds like you should take a closer look at what Jim put up and make that work one way or another. If you think what I posted is verbose, then you are iron-clad there to work...


[ October 22, 2007: Message edited by: Nicholas Jordan ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!