is there a Map in Guava that maps a Key to a List/Set of Pairs ?!

296 views
Skip to first unread message

zixzigma

unread,
Jan 6, 2011, 11:36:05 PM1/6/11
to guava-discuss
Hello Everyone,

I am looking for a DataStructure that can satisfy the following
requirement.

going through Guava JavaDoc, I couldn't find what I was looking for.

I am looking for a "Map", that maps a "Key", to a "Set of Values",
each of the values is a "Pair".

I came across MultiSet, but don't know how to present a Pair of two
object ?

ultimately I want to put the map structures in a List : )

really appreciate if you could please provide some tips on how I can
use Guava to its best capacity to model the data structure I
described.



Map<Key, Set<Pair>>

Pair {
ObjectA objectA;
ObjectB objectB;
}

example:

Map:

key set of pairs
1 ---> { (a,b), (x,y), (s,t) ...}
2---> { (f,g) , (L,K), (W,V) ..}
...
n --> { (z,y),(o,p).(x,y) ...}


eventually, I want to put these Maps that are populated separately,
into a List. something like this:
List<Map<Key,Set<Pair>>>


my main concern is that I don't want to create a Pair class
just to wrap two objects. I believe there must be a better way to
implement this data structure, perhaps using one of Guava's data
structures, but after going through the documentation couldn't find
one.


two other questions:

1- is there a Pair structure in Guava ?
2- is there a Tuple structure in Guava ?

Thank You !

Maarten Billemont

unread,
Jan 7, 2011, 2:17:05 PM1/7/11
to zixzigma, guava-discuss
> Hello Everyone,
>
> I am looking for a DataStructure that can satisfy the following
> requirement.
>
> going through Guava JavaDoc, I couldn't find what I was looking for.
>
> I am looking for a "Map", that maps a "Key", to a "Set of Values",
> each of the values is a "Pair".
>
> I came across MultiSet, but don't know how to present a Pair of two
> object ?

MultiSet satisfies your first requirement.

> two other questions:
>
> 1- is there a Pair structure in Guava ?
> 2- is there a Tuple structure in Guava ?

The Guava developers have opted against introducing Pair-style structures. AFAIK, the reason being that such a structure does not convey the proper context of why object 1 and object 2 are paired up.

Instead, you should look at why you're trying to combine your two objects in each set element and create a class that expresses the contract between the two objects properly.
Eg. If it's a username and password, you might want to make a Credentials class which provides accessors for a username and password, and then use a MultiSet<Credentials>. At least then your code explains the contract rather than tying the objects in some generic way that may make little sense to you later on. It also provides you with a perfect place for documenting this contract with JavaDoc and allows you to sensibly extend upon your structures without needing to refactor all the code that uses your MultiSet.

Chris Povirk

unread,
Jan 7, 2011, 2:41:49 PM1/7/11
to Maarten Billemont, zixzigma, guava-discuss
On Fri, Jan 7, 2011 at 2:17 PM, Maarten Billemont <lhu...@gmail.com> wrote:
>> Hello Everyone,
>>
>> I am looking for a DataStructure that can satisfy the following
>> requirement.
>>
>> going through Guava JavaDoc, I couldn't find what I was looking for.
>>
>> I am looking for a "Map", that maps a "Key", to a  "Set of Values",
>> each of the values is a "Pair".
>>
>> I came across MultiSet, but don't know how to present a Pair of two
>> object ?
>
> MultiSet satisfies your first requirement.

I think you're thinking of SetMultimap, rather than Multiset.
(SetMultimap is roughly a Map<K, Set<V>>; Multiset is roughly a Map<K,
Integer>.)

>> two other questions:
>>
>> 1- is there a Pair structure in Guava ?
>> 2- is there a Tuple structure in Guava ?
>
> The Guava developers have opted against introducing Pair-style structures.  AFAIK, the reason being that such a structure does not convey the proper context of why object 1 and object 2 are paired up.
>
> Instead, you should look at why you're trying to combine your two objects in each set element and create a class that expresses the contract between the two objects properly.
> Eg. If it's a username and password, you might want to make a Credentials class which provides accessors for a username and password, and then use a MultiSet<Credentials>.  At least then your code explains the contract rather than tying the objects in some generic way that may make little sense to you later on.  It also provides you with a perfect place for documenting this contract with JavaDoc and allows you to sensibly extend upon your structures without needing to refactor all the code that uses your MultiSet.

Exactly.

Johan Van den Neste

unread,
Jan 8, 2011, 10:17:03 AM1/8/11
to Maarten Billemont, zixzigma, guava-discuss
> The Guava developers have opted against introducing Pair-style structures.  AFAIK, the reason being that such a structure does not convey the proper context of why object 1 and object 2 are paired up.

As if a collection such as a SetMultimap conveys any context as to why
its items are 'collected'.

I understand other reasons for not including a Pair or Tuple type in
Guava, but in the religious debate on whether or not to protect
inexperienced developers from shooting themselves and others in the
foot versus increasing developer productivity, I am pro-productivity.
That said, for me, the productivity argument only holds for tuples as
long as all developers understand where using tuples really hampers
readability.

Maybe Guava's stance on this should go in a faq somewhere, along with
the whole functional programming debate. As much as I'd love to argue
about this endlessly, maybe it would cut down on some noise? :)

--
Johan

Reply all
Reply to author
Forward
0 new messages