Bidirectional Multimap

1,641 views
Skip to first unread message

Francis Giraldeau

unread,
Nov 7, 2012, 2:37:34 PM11/7/12
to guava-...@googlegroups.com
I needed a Multimap that was also keeping reverse entry relationship, a kind of non-bijective bidirectional map where duplicated values are not allowed. AFAIK, there is not such collection in Guava, so I did a QND prototype:


Unfortunately, BiMap and Multimap can't be implemented at the same time, because get() return value type is conflicting.

Usage example: 

  ArrayListBiMultimap<String, String> m = test.new ArrayListBiMultimap<String, String>();
  m.put("foo", "bar");
  m.put("foo", "baz");

  forward map:
  {foo=[bar, baz]}

  reverse map:
  {baz=foo, bar=foo}

Could it be interesting for Guava? 

Cheers, 

Francis

Louis Wasserman

unread,
Nov 7, 2012, 2:40:34 PM11/7/12
to Francis Giraldeau, guava-...@googlegroups.com
We have a BiMultimap internally, but haven't open-sourced it due to lack of demand -- though we do have ImmutableMultimap.inverse(), which helps users who are satisfied with immutability.  

Could you describe your use case in more detail?

(Additionally, ImmutableMultimap.inverse() has


Francis

--
guava-...@googlegroups.com
Project site: http://guava-libraries.googlecode.com
This group: http://groups.google.com/group/guava-discuss
 
This list is for general discussion.
To report an issue: http://code.google.com/p/guava-libraries/issues/entry
To get help: http://stackoverflow.com/questions/ask (use the tag "guava")



--
Louis Wasserman

Louis Wasserman

unread,
Nov 7, 2012, 2:41:23 PM11/7/12
to Francis Giraldeau, guava-...@googlegroups.com
Also relevant: Guava issue 394.
--
Louis Wasserman

Francis Giraldeau

unread,
Nov 7, 2012, 3:32:27 PM11/7/12
to guava-...@googlegroups.com, Francis Giraldeau
Here is more info on the use case. I do have a graph composed of vertices. Vertices belong to some owner object. it must be quick to lookup the owner of a vertex, or to get the list of vertex for some owner. The relation between Owner and Vertex is one to many. Holding a reference to the owner inside the vertex is inelegant, because it creates a dependency between a Vertex, that is pure graph class and domain dependent owner class.

Bidirectional map for many-to-many relationship is probably the most general case, thought.

Cheers, 

Francis

Louis Wasserman

unread,
Nov 7, 2012, 3:41:43 PM11/7/12
to Francis Giraldeau, guava-...@googlegroups.com
Is the graph mutable, or immutable?

If immutable, this would probably be satisfied with ImmutableMap.asMultimap(), which will be in release 14:

ImmutableMap<Vertex, Owner> ownerMap;
ImmutableMultimap<Owner, Vertex> vertexMultimap = ownerMap.asMultimap().inverse();

Gregory Kick

unread,
Nov 7, 2012, 4:02:24 PM11/7/12
to Louis Wasserman, Francis Giraldeau, guava-discuss
Also, if you really want a representation of a graph, I've heard that http://jung.sourceforge.net/ is good.
Greg Kick
Java Core Libraries Team

Grzegorz Rożniecki

unread,
Nov 8, 2012, 9:35:31 AM11/8/12
to guava-...@googlegroups.com, Louis Wasserman, Francis Giraldeau
About Jung, there's Jung 3 beta version hosted on Google Code (http://code.google.com/p/jung/) which uses Guava instead of Apache Commons-Collections generics port (see migration guide), but seems to be inactive for some time now (but so does sourceforge version).

--
Cheers,
Grzegorz Rożniecki

Joshua O'Madadhain

unread,
Nov 8, 2012, 3:16:07 PM11/8/12
to Grzegorz Rożniecki, guava-discuss, Louis Wasserman, Francis Giraldeau
FWIW, JUNG is not precisely inactive, but the changes that we're working on haven't made it to the repositories yet.  

   Joshua O'Madadhain: Information Scientist, Musician, Philosopher-At-Tall
  "It's that moment of dawning comprehension that I live for" -- Bill Watterson

Grzegorz Rożniecki

unread,
Nov 10, 2012, 8:15:23 AM11/10/12
to guava-...@googlegroups.com, Grzegorz Rożniecki, Louis Wasserman, Francis Giraldeau
That's great to hear because we use Jung 2 in our project and it's the only part depending on Apache Collections so I'd like to see Guava port. 

Out of curiosity, when is Jung 3 expected to come out?

--
Cheers,
Grzegorz Rożniecki

Joshua O'Madadhain

unread,
Nov 12, 2012, 1:53:54 PM11/12/12
to Grzegorz Rożniecki, guava-discuss, Louis Wasserman, Francis Giraldeau
On Sat, Nov 10, 2012 at 5:15 AM, Grzegorz Rożniecki <xaer...@gmail.com> wrote:
That's great to hear because we use Jung 2 in our project and it's the only part depending on Apache Collections so I'd like to see Guava port. 

Out of curiosity, when is Jung 3 expected to come out?

That is under discussion at the moment, but not until early 2013.  There are a couple of options we're considering.
Reply all
Reply to author
Forward
0 new messages