Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Collections -- Help please...

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's my problem:
2 attributes, Attr1 & Attr2, of an object OBJ come from a database DB1.
In DB1, for a single Attr1 multiple records with different Attr2 would exist.
In my application, I need to define a base class containing these 2 Attributes Attr1 & Attr2.
Should this be with HashMap or HashSet or ArrayList or a combination? Please suggest.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Im not sure what you are asking
A class with those two attributes in it is easy, but probably not what you want .. if you are thinking about using a collection you probably want to do something like "find all attr2 that have attr1" ... and to do that i would use:
a HashMap with key attr1 and value=a vector of attr2s
You need to think about what functions you want on the class before you can decide how to implement it.

 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Carl says, you could have a Map with key: attr1 and value: a List or Vector of attr2 values. Or if there is no duplication in attr2 values, you might want key: attr2 and value: attr1. But this is only useful if you want to be able to look up attr1 from attr2.
Really, there are a lot of different ways to organize the data here. What types of operations do you need to be able to do? Look up attr2 using attr1? Look up attr1 using attr2? Look up either one using an index number? Sort the entries somehow? Etc. This will help determine what sort of data structure makes sense here.
P.S. Hi Carl - welcome back!
Note - the first time through I misread your previous message, and had a "correction" for you which I subsequently removed when I realized my mistake. Sorry for the confusion. - Jim
[This message has been edited by Jim Yingst (edited December 12, 2000).]
 
Sree Shan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I forgot to mention the operations.
I would need to look up both ways == for a given Attr1, I need to get all Attr2 and vice versa.
Once all Attr2 is retreived for a given Attr1, it needs to be sorted in ascending order (its a string).
Hope this gives a clear picture.
 
Carl Desborough
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok for "find all attr2 for a given attr1" with the return values sorted, i would use:
a map from attr1 -> a TreeSet of matching attr2 values.
The treeset will be sorted by the natural ordering of the attr2 values, but if you need a different ordering, you can write your own Comparator for it.
Another consideration is what to return from the get method ... some choices are: a collection, an iterator or an Attr2[].
hmmm now ive lost the original message ... i think you also wanted to be able to "find all attr2 for a given attr1" ... so you could use the same approach in reverse for that.

So your class would look something like:
public class Stuff {
private Map attr1ToAttr2 = new HashMap(); // unsorted
private Map attr2ToAttr1 = new HashMap(); // unsorted
public void add( Attr1, Attr2 );
public Iterator listAttr2( Attr1 );
public Iterator listAttr1( Attr2 );
}
Well thats how id do it anyway.

ps. Hi Jim
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd probably be inclined to leave the data in the database, and use two different queries to get the info you need:
<code><pre>select attr2
from table
where attr1=?
order by attr2</pre></code>
and
<code><pre>select attr1
from table
where attr2=?
order by attr1</pre></code>
If you're going to be accessing these results repeatedly, and want to minimize the recurring access time at the expense of taking more time and memory up front, you could save the results of these queries in some sort of data structures. You might want two different HashMaps - one with key attr1 and value a Collection of attr2 Strings, and the other with key attr2 and value a Collection of attr1 Strings. What sort of Collection? If you get the database to do the sorting for you in advance (as above), then an ArrayList is probably best; if not, then probably a TreeSet is simplest, which will sort the values for you as you add them, but will be slower to access.
As you see, many choices are still possible depending on what your priorities are - execution speed, simplicity, memory usage, etc. Hopefully this gives you some good ideas.
 
Sree Shan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Carl / Jim.. You both did point out some directions for me to take... I'll try it out. Thanks again
 
Let nothing stop you! Not even this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic