aspose file tools*
The moose likes Java in General and the fly likes what collection class to use?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "what collection class to use??" Watch "what collection class to use??" New topic
Author

what collection class to use??

Dar Var
Ranch Hand

Joined: Oct 12, 2004
Posts: 74
I have a method that returns a TreeMap.

The TreeMap stores values as follows:
{a=String[] of values,
b=String[] of values,
c=String[] of values}

However now I need to return an alias string aswell i.e. I want to return a meaningful name for each key a, b and c etc.

Is there any Collection that can store something like (key, keyName, values) or do I have to use an Object for each key to store the key:alias pairs.
If so how can TreeMap sort by these Object keys

Hope I've made sense.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
If you want to store same groups of values, you'd better to wrap the values in a DTO (Data Transfer Object). Such a DTO is nothing more than a Class with the desired fields and the appropriate getters and setters. Then you can store more DTO's in any Collection or Map object.

Sorting can be done by a Comparator. Or implement Comparable in the DTO.

DTO example:

A map of DTO's
[ October 17, 2006: Message edited by: Bauke Scholtz ]
Dar Var
Ranch Hand

Joined: Oct 12, 2004
Posts: 74
You cannot add an Object to a Map like this.

dtos.add(new MyData(1, "value1a", "value1a"));

The only method TreeMap provides is put(Object o1, Object o2)
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
This was just a stupid example Use a Set instead. HashSet for example. Or a List. Or if you want to use keys, then put the identifier as key in the map, like: map.put(1, new MyDto());
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by Dar Var:
I have a method that returns a TreeMap.

The TreeMap stores values as follows:
{a=String[] of values,
b=String[] of values,
c=String[] of values}

However now I need to return an alias string aswell i.e. I want to return a meaningful name for each key a, b and c etc.

Is there any Collection that can store something like (key, keyName, values) or do I have to use an Object for each key to store the key:alias pairs.
If so how can TreeMap sort by these Object keys

Hope I've made sense.


Using a simple container for the key that can also provide a meaningful String is definitely one option. Another option would be to use the meaningful String as the key though that may not be feasible or desirable for a number of reasons. Yet another possibility is to have a second Map that maps each key to a meaningful String value.

So to answer your question: no, there's nothing like that in Collections. Bauke already alluded to using Comparable with the container object to solve your sorting issue. You might find something like that available somewhere like apache.org under Commons. There's a number of other options as well, a few of which I expressed earlier.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what collection class to use??