Reputation: 10672
I am new to java development, I am from C# .net, developing android application.
I am looking for Key-Value list to use in Java same as IDictionary in C#.
Thanks
Upvotes: 5
Views: 27072
Reputation: 35136
Java has Hashtable and HashMap
See Differences between HashMap and Hashtable?
Upvotes: 7
Reputation: 3158
I've usually preferred java.util.TreeMap to a HashMap. It's not quite as fast, but you don't have to worry about hashing algorithms or setting the size. More important, it's memory-friendly because it uses small chunks of memory rather than allocating vast arrays. If you have close control over your Maps' creation and disposal, know in advance how much data they will hold, and know the number of entries will not vary dramatically as your program runs, use a Hashmap. Otherwise use a Treemap.
The other really cool Map, which I have yet to encounter the likes of in .NET, is java.util.concurrent.ConcurrentSkipListMap. This is excellent for multithreaded situations. It is completely thread safe and it is non-blocking.
(I found this question while looking at one closed for being a duplicate. I thought both questions could use an answer that mentioned something other than HashMaps, good as HashMaps are for most uses and similar as they are to the C# Dictionary.)
Upvotes: 1
Reputation: 1754
You are looking for an alternative to the generic IDictionary in C# which takes (key,value) pairs.
In Java, there is a Dictionary class that is the parent class of any class, which accepts (key,value) pairs.
A HashTable (which extends Dictionary) will suit your need. It performs operations like copy, remove, add, check if an item exists etcetera. However, while IDictionary supports foreach loop in C#, to loop over a HashTable in Java, you will need an Iterator.
An added advantage with HashTables is that it is synchronized, so you would not have to worry about concurrency.
IF however, you are looking for an asynchronous implementation, you should use the HashMap. A HashMap too requires an Iterator to traverse through it.
Undoubtedly, you also need to look at how an Iterator works in Java.
HashTables: http://download.oracle.com/javase/1.4.2/docs/api/java/util/Hashtable.html
HashMaps: http://download.oracle.com/javase/1.4.2/docs/api/java/util/HashMap.html
Iterator: http://download.oracle.com/javase/1.4.2/docs/api/java/util/Iterator.html
Upvotes: 0
Reputation: 12523
Use the Map<K, V>
interface, and the HashMap<K, V>
class for example. Or see the "All Known Subinterfaces" section in the Map<K, V>
interface description for more implementations.
http://download.oracle.com/javase/6/docs/api/java/util/Map.html
Upvotes: 9
Reputation: 838416
The interface you are looking for is Map<K, V>
.
For an implementation similar to C#'s Dictionary
try HashMap<K, V>
Upvotes: 3