single-value map?

P

Paul

I need to store info where all elements are of the form
ID (an int) --> (ObjType1, ObjType2)
The question is, is there a way of storing O1 and O2 in some kind of an
entrySet instead of using a full-blown map?
That is, instead of doing
Map<Integer, Map<ObjType1, ObjType2>> myMap = new HashMap<Integer,
Map<ObjType1, ObjType2>>();
can I somehow instantiate a new Entry object and store that instead of
an entire map (i.e. Map<Integer, Map.Entry<ObjType1, ObjType2>>)?
The only alternative I see to storing a separate map with each
int-->object pair entry, is making the overall structure to be
Map<Integer, Object[]> but then I'd need to cast the objects out of the
Object[] each time I use them.

If those ARE the only two options I have, which one is faster, casting
every time, or storing each elements pair as a separate Map?

Thanks!
 
S

Steve

I need to store info where all elements are of the form
ID (an int) --> (ObjType1, ObjType2)
The question is, is there a way of storing O1 and O2 in some kind of an
entrySet instead of using a full-blown map?
That is, instead of doing
Map<Integer, Map<ObjType1, ObjType2>> myMap = new HashMap<Integer,
Map<ObjType1, ObjType2>>();
can I somehow instantiate a new Entry object and store that instead of
an entire map (i.e. Map<Integer, Map.Entry<ObjType1, ObjType2>>)?
The only alternative I see to storing a separate map with each
int-->object pair entry, is making the overall structure to be
Map<Integer, Object[]> but then I'd need to cast the objects out of the
Object[] each time I use them.

If those ARE the only two options I have, which one is faster, casting
every time, or storing each elements pair as a separate Map?

Thanks!

Why not another class which contains your two objects?

class TwoThings
{
public ObjType1 object1;
public ObjType2 object2;
}

Map<Integer,TwoThings> ...



--
Regards,
Steve

"...which means he created the heaven and the earth... in the DARK! How good
is that?"
 
L

Lasse Reichstein Nielsen

Paul said:
I need to store info where all elements are of the form
ID (an int) --> (ObjType1, ObjType2)
The question is, is there a way of storing O1 and O2 in some kind of an
entrySet instead of using a full-blown map?
That is, instead of doing
Map<Integer, Map<ObjType1, ObjType2>> myMap = new HashMap<Integer,
Map<ObjType1, ObjType2>>();


Ok, a full Map object is *not* the simplest way to store two objects.
Instead you can try something like

public class Pair<F,S> {
public final F first;
public final S second;
public Pair(F first, S second) {
this.first = first;
this.second = second;
}
}

Then your map would be
Map<Integer,Pair<ObjType1,ObjType2>> myMap =
new HashMap<Integer,Pair<ObjType1,ObjType2>>();

Map.put(42, new Pair(value1,value2));
If those ARE the only two options I have, which one is faster, casting
every time, or storing each elements pair as a separate Map?

Using a Map to store *two* elements is overkill, by a lot.
Using a Map.Entry outside of a map is just bad reuse, using a class
for something it was never ment for. Make the class that suites your
needs, and make it do what you want.

/L
 
P

Patricia Shanahan

Paul said:
I need to store info where all elements are of the form
ID (an int) --> (ObjType1, ObjType2)
The question is, is there a way of storing O1 and O2 in some kind of an
entrySet instead of using a full-blown map?
That is, instead of doing
Map<Integer, Map<ObjType1, ObjType2>> myMap = new HashMap<Integer,
Map<ObjType1, ObjType2>>();
can I somehow instantiate a new Entry object and store that instead of
an entire map (i.e. Map<Integer, Map.Entry<ObjType1, ObjType2>>)?
The only alternative I see to storing a separate map with each
int-->object pair entry, is making the overall structure to be
Map<Integer, Object[]> but then I'd need to cast the objects out of the
Object[] each time I use them.

If those ARE the only two options I have, which one is faster, casting
every time, or storing each elements pair as a separate Map?

Thanks!

I would not try to use Map.Entry, because that is intended to represent
a key-value pair in a map. The names are all wrong for what you want. In
any case it is only an interface, so you would have to define your own
class implementing it.

Instead, I would declare my own class, probably a private nested class
inside a class that is responsible for the whole data structure. That
way, you can pick meaningful names instead of getKey etc.

private class MyPair{
ObjType1 obj1;
ObjType2 obj2;
....
}

and declare the Map as

Map<Integer, MyPair> myMap = new HashMap<Integer, MyPair>();

only with better identifiers.

Patricia
 
J

Jeffrey Schwab

Steve said:
I need to store info where all elements are of the form
ID (an int) --> (ObjType1, ObjType2)
The question is, is there a way of storing O1 and O2 in some kind of an
entrySet instead of using a full-blown map?
That is, instead of doing
Map<Integer, Map<ObjType1, ObjType2>> myMap = new HashMap<Integer,
Map<ObjType1, ObjType2>>();
can I somehow instantiate a new Entry object and store that instead of
an entire map (i.e. Map<Integer, Map.Entry<ObjType1, ObjType2>>)?
The only alternative I see to storing a separate map with each
int-->object pair entry, is making the overall structure to be
Map<Integer, Object[]> but then I'd need to cast the objects out of the
Object[] each time I use them.

If those ARE the only two options I have, which one is faster, casting
every time, or storing each elements pair as a separate Map?

Thanks!

Why not another class which contains your two objects?

class TwoThings
{
public ObjType1 object1;
public ObjType2 object2;
}

Map<Integer,TwoThings> ...

+1

It may be worth noting that Map is just an interface. You can implement
whatever data structure you want, e.g. class TwoThings, and it will
still be a valid Map as long as the right methods are implemented.
 
P

Paul

Thank you guys,
This is obviously the right solution. I wasn't thinking clearly.
Thanks everybody!
Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,157
Latest member
MercedesE4
Top