• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

what collection class to use??

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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());
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic