Possible Duplicate:
What will store a Key-Value list in Java or an alternate of C# IDictionary in Java?
In C# there is a data structure named IDictionary which stores a collection of key-value pairs. Is there simillar data structure in Java? If so, what is it called and can anybody give me an example of how to use it?
One of the best ways is HashMap:
Use this sample:
HashMap<String, Integer> dicCodeToIndex;
dicCodeToIndex = new HashMap<String, Integer>();
// valuating
dicCodeToIndex.put("123", 1);
dicCodeToIndex.put("456", 2);
// retrieving
int index = dicCodeToIndex.get("123");
// index is 1
Look at this link: http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html
You can use the various implementations of java.util.Map (see http://docs.oracle.com/javase/7/docs/api/java/util/Map.html). The most commonly used Map implementation for a dictionary-like map should be HashMap.
However, depending on the exact usage scenario, another type of map might be better.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With