Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > impl a collection

Reply
Thread Tools

impl a collection

 
 
vicky
Guest
Posts: n/a
 
      06-19-2004
I want to know if there is any efficient way to implement something
;like a hashmap which has the entries in the form
(key,valu1,value2).The structure should also work for getting the key
using any one of the values.

any thought on this?? is there any java libraries which implements
this??

-vick
 
Reply With Quote
 
 
 
 
Michael Borgwardt
Guest
Posts: n/a
 
      06-19-2004
vicky wrote:

> I want to know if there is any efficient way to implement something
> ;like a hashmap which has the entries in the form
> (key,valu1,value2).The structure should also work for getting the key
> using any one of the values.
>
> any thought on this?? is there any java libraries which implements
> this??


HashMap implements this. Use three instances, one with your key as key
and an array with the two values as elements as value, and two with
the respective values as key and the key as value.
 
Reply With Quote
 
 
 
 
vicky
Guest
Posts: n/a
 
      06-19-2004
Using three instances would be very expensive in terms of storage.
jakarta doubleorderedmap might be a good solution if the values are
used in an array.

vick.

Michael Borgwardt <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> vicky wrote:
>
> > I want to know if there is any efficient way to implement something
> > ;like a hashmap which has the entries in the form
> > (key,valu1,value2).The structure should also work for getting the key
> > using any one of the values.
> >
> > any thought on this?? is there any java libraries which implements
> > this??

>
> HashMap implements this. Use three instances, one with your key as key
> and an array with the two values as elements as value, and two with
> the respective values as key and the key as value.

 
Reply With Quote
 
Michael Borgwardt
Guest
Posts: n/a
 
      06-19-2004
vicky wrote:

> Using three instances would be very expensive in terms of storage.


Is the number of items really that large or your RAM so small?
Programming ain't magic. If you want to efficiently search for
three fields, you need three indexes, no way around it.

> jakarta doubleorderedmap might be a good solution if the values are
> used in an array.


You couldn't look for one value then, only a pair. Besides, the
API documentation for that class is misleading in regard to its space
saving. I found the claims (and the concept) intriguing and had a
look at the code. The only space it saves is the object overhead of
the node objects and the second copy of the key and value references,
and some of that is squandered on a pointless caching of hash values.
With an object overhead of 8 bytes that's less than 20% space saved
compared to two TreeSet instances with the same data.

 
Reply With Quote
 
Job Numbers
Guest
Posts: n/a
 
      06-20-2004

"vicky" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
>I want to know if there is any efficient way to implement something
> ;like a hashmap which has the entries in the form
> (key,valu1,value2).The structure should also work for getting the key
> using any one of the values.
>
> any thought on this?? is there any java libraries which implements
> this??
>
> -vick


This might sound like a stupid question, but why are you doing something a
database could do for you?


 
Reply With Quote
 
Kevin McMurtrie
Guest
Posts: n/a
 
      06-20-2004
In article <(E-Mail Removed) >,
http://www.velocityreviews.com/forums/(E-Mail Removed) (vicky) wrote:

> I want to know if there is any efficient way to implement something
> ;like a hashmap which has the entries in the form
> (key,valu1,value2).The structure should also work for getting the key
> using any one of the values.
>
> any thought on this?? is there any java libraries which implements
> this??
>
> -vick


Use three maps, each with a different object as the key, and all three
sharing the same result Object.

class FooElement
{
final Object key;
final Object value1;
final Object value2;
public FooElement (Object key, Object value1, Object value2)
{
this.key= key;
this.value1= value1;
this.value2= value2;
}
}

final HashMap m_keyMap= new HashMap ();
final HashMap m_value1Map= new HashMap ();
final HashMap m_value2Map= new HashMap ();

void put (Object key, Object value1, Object value2)
{
FooElement f= new FooElement(key, value1, value2);
m_keyMap(key, f);
m_value1Map(value1, f);
m_value2Map(value2, f);
}

Object getKeyByValue1 (Object value1)
{
FooElement f= (FooElement)m_value1Map.get (value1);
return (f == null) ? null : f.key;
}

// etc...

This is a somewhat tedious example so adjust it to suit your needs.
You'll also need to figure out what happens when the mappings aren't
1:1. If your mapping needs to be large, complex, rule-based, shared, or
have persistence, a database is exactly what you'll want. The code gets
to be a mess beyond simple cases like I've shown above.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      06-20-2004
On 19 Jun 2004 06:41:45 -0700, (E-Mail Removed) (vicky) wrote or
quoted :

>I want to know if there is any efficient way to implement something
>;like a hashmap which has the entries in the form
>(key,valu1,value2).The structure should also work for getting the key
>using any one of the values.


You create a Glue object which contains references (possibly to the
key) and the two data values. Then you do a lookup by key to the Glue
object.


A simple Glue object could be new Object[] pair = { value1 , value2 };

adjust as necessary if value1 and value2 are primitives.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Job Numbers
Guest
Posts: n/a
 
      06-20-2004

"Kevin McMurtrie" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> In article <(E-Mail Removed) >,
> (E-Mail Removed) (vicky) wrote:
>
>> I want to know if there is any efficient way to implement something
>> ;like a hashmap which has the entries in the form
>> (key,valu1,value2).The structure should also work for getting the key
>> using any one of the values.
>>
>> any thought on this?? is there any java libraries which implements
>> this??
>>
>> -vick

>
> Use three maps, each with a different object as the key, and all three
> sharing the same result Object.
>
> class FooElement
> {
> final Object key;
> final Object value1;
> final Object value2;
> public FooElement (Object key, Object value1, Object value2)
> {
> this.key= key;
> this.value1= value1;
> this.value2= value2;
> }
> }
>
> final HashMap m_keyMap= new HashMap ();
> final HashMap m_value1Map= new HashMap ();
> final HashMap m_value2Map= new HashMap ();
>
> void put (Object key, Object value1, Object value2)
> {
> FooElement f= new FooElement(key, value1, value2);
> m_keyMap(key, f);
> m_value1Map(value1, f);
> m_value2Map(value2, f);
> }
>
> Object getKeyByValue1 (Object value1)
> {
> FooElement f= (FooElement)m_value1Map.get (value1);
> return (f == null) ? null : f.key;
> }
>
> // etc...
>
> This is a somewhat tedious example so adjust it to suit your needs.
> You'll also need to figure out what happens when the mappings aren't
> 1:1. If your mapping needs to be large, complex, rule-based, shared, or
> have persistence, a database is exactly what you'll want. The code gets
> to be a mess beyond simple cases like I've shown above.


Exactly


 
Reply With Quote
 
NOBODY
Guest
Posts: n/a
 
      06-22-2004
"Job Numbers" <(E-Mail Removed)> wrote in
news:WE5Bc.11023$(E-Mail Removed):

> "vicky" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
>>I want to know if there is any efficient way to implement something
>> ;like a hashmap which has the entries in the form
>> (key,valu1,value2).The structure should also work for getting the key
>> using any one of the values.
>>
>> any thought on this?? is there any java libraries which implements
>> this??
>>
>> -vick

>
> This might sound like a stupid question, but why are you doing
> something a database could do for you?



You are truly misleading people. Using a DB for that case is really
overkill, and WILL kill your performance.
 
Reply With Quote
 
NOBODY
Guest
Posts: n/a
 
      06-22-2004
An array costs ~14 +/- 2 bytes , plus the 2 object ref you intent to put
in. that is 24 bytes. Writing an object with 2 fields will cost you 8 bytes
for the object and 2x4 bytes for the refs, 16 bytes total, and you get
compile time safety.

concl.: don't use arrays.




(E-Mail Removed) (vicky) wrote in news:f7f2f1e5.0406191055.4eec4ea5
@posting.google.com:

> Using three instances would be very expensive in terms of storage.
> jakarta doubleorderedmap might be a good solution if the values are
> used in an array.
>
> vick.
>
> Michael Borgwardt <(E-Mail Removed)> wrote in message news:

<(E-Mail Removed)>...
>> vicky wrote:
>>
>> > I want to know if there is any efficient way to implement something
>> > ;like a hashmap which has the entries in the form
>> > (key,valu1,value2).The structure should also work for getting the key
>> > using any one of the values.
>> >
>> > any thought on this?? is there any java libraries which implements
>> > this??

>>
>> HashMap implements this. Use three instances, one with your key as key
>> and an array with the two values as elements as value, and two with
>> the respective values as key and the key as value.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Řyvind Isaksen ASP .Net 1 05-18-2007 09:24 AM
myfaces-impl-1.1.3 instantiates the request scope bean elliot.khazon@gmail.com Java 0 06-28-2006 03:55 PM
CachedRowSetImpl in 1.5 incompatible to Tiger 1.01 ref impl Jochen Riekhof Java 2 09-18-2005 07:49 PM
STL hash_set problem, SGI impl Timo Qvist C++ 1 11-18-2004 03:04 PM
Which type of SOAP client? (sun impl) iksrazal Java 0 08-21-2003 06:28 PM



Advertisments