Jake Cobb
Jake Cobb

Reputation: 1861

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

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.

Upvotes: 3

Views: 1114

Answers (2)

Equator
Equator

Reputation: 1

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

Upvotes: -2

Sezin Karli
Sezin Karli

Reputation: 2525

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?

Upvotes: 4

Related Questions