This Map can be looked up by both the key and the value WITH the same performance. The catch is that the values should also be unique. Which is expected as the values become the keys in reverse lookup.
A simple example:
import org.apache.commons.collections.BidiMap;
import org.apache.commons.collections.bidimap.TreeBidiMap;
public class BidiMapDemo {
public static void main(String[] args)
{
BidiMap map = new TreeBidiMap();
map.put("
map.put("IN", "
map.put("
System.out.println(
String.format(
"Lookup by Key. key: %s Name: %s",
"US",
map.get("US") // returns "
)
);
System.out.println(
String.format(
"Lookup by value. Name: %s Key: %s",
"
map.getKey("
)
);
}
}
Output:
Lookup by Key. key: US Name:
Lookup by value. Name:
No comments:
Post a Comment