Is there an ordered version of BiMap

707 views
Skip to first unread message

Peter

unread,
Jun 7, 2012, 4:42:41 AM6/7/12
to guava-discuss
I'm wanting to be able to iterate through a bimap in a repeatable
fashion.

I had a look at implementing my own LinkedHashBiMap by stealing the
HashBiMap impl and swapping in some LinkedHashMaps under the hood.
However there are private classses and methods that make this approach
problematic.

Is there an easy way to get an ordered BiMap or do I have to code the
whole thing up from scratch?

Cheers,
Peter

Kevin Bourrillion

unread,
Jun 7, 2012, 8:39:03 AM6/7/12
to Peter, guava-discuss
https://code.google.com/p/guava-libraries/issues/detail?id=130

There's also the fact that we should at least consider adding a LinkedHashBiMap class.





--
Kevin Bourrillion @ Google
Java Core Libraries Team
http://guava-libraries.googlecode.com

Louis Wasserman

unread,
Jun 7, 2012, 8:39:06 AM6/7/12
to Peter, guava-discuss
ImmutableBiMap maintains insertion order.

Part of the difficulty in a mutable, insertion-ordered BiMap is that keeping the order consistent in both directions is quite tricky and requires a lot of thought.

Kevin Bourrillion

unread,
Jun 7, 2012, 8:43:12 AM6/7/12
to Louis Wasserman, Peter, guava-discuss
Ah, I forgot about that complication. It can be weird to have entries iterate in a completely different order on the inverse from the, erm, obverse(?).

And right: Immutable is very often the answer.

Peter

unread,
Jun 7, 2012, 9:28:35 AM6/7/12
to guava-discuss
Thanks for your input everyone.

In my instance I want to an immutable won't help because I need to be
able to add more entries.

The iteration order of normal vs inverse doesn't actually bother me. I
just need to know that the ordering is consistent between iterations.

In my case I need to be able to look up values by key, keys by value
and also to be able to provide a list of key/values pairs from
position x to y.

I might just use store all my data in both a List and a BiMap.

Cheers,
Peter

On Jun 7, 10:43 pm, Kevin Bourrillion <kev...@google.com> wrote:
> Ah, I forgot about that complication. It can be weird to have entries
> iterate in a completely different order on the inverse from the, erm,
> obverse(?).
>
> And right: Immutable is very often the answer.
>
> On Thu, Jun 7, 2012 at 5:39 AM, Louis Wasserman
> <wasserman.lo...@gmail.com>wrote:
>
>
>
>
>
>
>
>
>
> > ImmutableBiMap maintains insertion order.
>
> > Part of the difficulty in a mutable, insertion-ordered BiMap is that
> > keeping the order consistent in both directions is quite tricky and
> > requires a lot of thought.
>
> > Louis Wasserman
> > wasserman.lo...@gmail.com
> >http://profiles.google.com/wasserman.louis
>
> > On Thu, Jun 7, 2012 at 4:42 AM, Peter <proggerp...@gmail.com> wrote:
>
> >> I'm wanting to be able to iterate through a bimap in a repeatable
> >> fashion.
>
> >> I had a look at implementing my own LinkedHashBiMap by stealing the
> >> HashBiMap impl and swapping in some LinkedHashMaps under the hood.
> >> However there are private classses and methods that make this approach
> >> problematic.
>
> >> Is there an easy way to get an ordered BiMap or do I have to code the
> >> whole thing up from scratch?
>
> >> Cheers,
> >> Peter
>
> >> --
> >> 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")
>
> >  --
> > 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")

Torbjorn Gannholm

unread,
Jun 7, 2012, 3:10:24 PM6/7/12
to Peter, guava-discuss
On Thu, Jun 7, 2012 at 3:28 PM, Peter <progg...@gmail.com> wrote:
Thanks for your input everyone.

In my instance I want to an immutable won't help because I need to be
able to add more entries.

The iteration order of normal vs inverse doesn't actually bother me. I
just need to know that the ordering is consistent between iterations.

Iteration order is consistent, except that additions made between iterations can end up anywhere.
 

In my case I need to be able to look up values by key, keys by value
and also to be able to provide a list of key/values pairs from
position x to y.

What is the meaning of "position"? Are you iterating e.g. first N, then N+1 to 2N and so on?
 

Peter

unread,
Jun 7, 2012, 8:24:58 PM6/7/12
to guava-discuss
You're interpretation of position is correct. My spec is that I need
to be able to return either mapping or a bulk list of mappings between
2 given indexes.

If BiMap iteration is consistent I can probably work with it, I'd need
to notify the client that their current list was no good when I add
values to the map. However I'm thinking having a list as well might be
better for me anyway because subList fits my requirement to return
elements by position so well.

I'm not sure how efficient something like new
ArrayList(map.entrySet()).sublist(x, y) would be anyway.

Cheers,
Pete

On Jun 8, 5:10 am, Torbjorn Gannholm <t...@google.com> wrote:
> > > >> To get help:http://stackoverflow.com/questions/ask(usethe tag
> > "guava")
>
> > > >  --
> > > > 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(usethe tag
> > "guava")
>
> > > --
> > > Kevin Bourrillion @ Google
> > > Java Core Libraries Teamhttp://guava-libraries.googlecode.com
>
> > --
> > 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
Reply all
Reply to author
Forward
0 new messages