Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Java have a data structure that stores key value pairs, equivalent to IDictionary in C#? [duplicate]

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?

like image 803
Conscious Avatar asked Nov 28 '22 02:11

Conscious


2 Answers

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

like image 105
Bobs Avatar answered Dec 10 '22 07:12

Bobs


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.

like image 24
StandByUkraine Avatar answered Dec 10 '22 07:12

StandByUkraine