user868935
user868935

Reputation:

Java class version of C#'s Dictionary<key, value>

I'm working on converting a C# program to a Java program. Is there a class in Java comparable in usage to C#'s

Dictionary<key, value> dictionary = new Dictionary<key, value>();

Upvotes: 1

Views: 280

Answers (2)

Dean J
Dean J

Reputation: 40328

HashMap is likely what you want.

Hashtable is synchronized; it works more reliably across multiple threads, but for most code, HashMap.

Kocko nails it, above.

Upvotes: 0

Konstantin Yovkov
Konstantin Yovkov

Reputation: 62864

There is a Dictionary abstract class in Java, which has a single direct subclass - Hashtable.

But in the javadoc is clearly said:

This class is obsolete. New implementations should implement the Map interface, rather than extending this class.

A sample Map:

Map<Key, Value> map = new HashMap<Key,Value>();

Note that Map is an interface and HashMap is an implementation of Map. There are many other Map imeplementations, like LinkedHashMap, IdentityHashMap, etc. Which implementation to use depends on what you need.

Upvotes: 3

Related Questions