Web / Apache Commons Collections Interview questions
What is a SortedBidiMap in Apache Commons Collections?
A SortedBidiMap<K,V> combines the guarantees of a BidiMap (bidirectional key/value lookup with unique values) with those of a SortedMap (keys iterated in a defined sort order).
SortedBidiMap<String, Integer> ranks = new DualTreeBidiMap<>(); ranks.put("silver", 2); ranks.put("gold", 1); System.out.println(ranks.firstKey()); // "gold" System.out.println(ranks.getKey(2)); // "silver"
DualTreeBidiMap is the typical implementation, backed by two internal TreeMaps - one for the forward direction, one for the inverse - so both directions stay sorted and support efficient reverse lookup at the same time.
Use it whenever you need both properties together, such as a ranking table where you must look up a rank by name and a name by rank, in sorted order.
More Related questions...