Is there a BiMap implementation with predictable iteration ordering (insertion order)?

1k Views Asked by At

The standard library LinkedHashSet is a HashSet that provides insertion-order iteration. Is there a version of Guava's BiMap that maintains insertion-order iteration on keys and values? I do need the BiMap to be mutable, so ImmutableBiMap is not sufficient for my purpose.

2

There are 2 best solutions below

2
On BEST ANSWER

The only available implementation is ImmutableBiMap<K,V>. You can check the implementations here. You can use a LinkedHashMap and convert it to with copyOf(Map<? extends K,? extends V> map) to make it an ImmutableBiMap<K,V>. Will it work for you?

1
On

You could get the keyset, convert it to an array and then loop over the array like this:

Object[] keys = someBiMap.keySet().toArray();
for(Object key : keys) {
    Object entry = someBiMap.get(key);
}

This is not a very nice solution but it's the only solution that I have found for this problem