What will store a Key-Value list in Java or an alternate of C# IDictionary in Java?

26,877

Solution 1

Java has Hashtable and HashMap

See Differences between HashMap and Hashtable?

Solution 2

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

Solution 3

The interface you are looking for is Map<K, V>.

For an implementation similar to C#'s Dictionary try HashMap<K, V>

Solution 4

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.)

Share:
26,877
BreakHead
Author by

BreakHead

Updated on August 24, 2020

Comments

  • BreakHead
    BreakHead over 3 years

    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