aspose file tools
The moose likes Java in General and the fly likes Sortable Collection with indexed properties Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Sortable Collection with indexed properties" Watch "Sortable Collection with indexed properties" New topic
Author

Sortable Collection with indexed properties

Gregg Bolinger
Ranch Hand

Joined: Jul 11, 2001
Posts: 15229

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

Joined: Sep 17, 2006
Posts: 1282
Try Tree Map and tell me what happens.


"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Gregg Bolinger
Ranch Hand

Joined: Jul 11, 2001
Posts: 15229

I was hoping for an Interface.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
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.


"I'm not back." - Bill Harding, Twister
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
[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

Joined: Jul 11, 2001
Posts: 15229

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

Joined: Feb 05, 2003
Posts: 234
What about wrapping your SortedSet with a ListOrderedSet?
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
[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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sortable Collection with indexed properties
 
Similar Threads
Struts: How does indexed data get into a Form Bean?
Please Help.Is it not possible in Struts to map indexed element ?
DynaActionForms (struts 1.2) - form-property type
Javascript and Struts Indexed properties
how to do submit the Dynamic produced element in Struts