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

I am new to java development, I am from C # .net, developing an Android application.

I am looking for a list of Key-Value for use in Java, the same as an IDictionary in C #.

thanks

+4
source share
5 answers

Use the Map<K, V> interface , and the HashMap<K, V> class for example. Or, see the β€œAll Known Subinterfaces” section of the Map<K, V> interface description for more implementations.

http://download.oracle.com/javase/6/docs/api/java/util/Map.html

+9
source

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

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

+3
source

I usually preferred java.util.TreeMap to HashMap. It's not that fast, but you don't have to worry about hashing algorithms or sizing. More importantly, it is convenient for memory because it uses small chunks of memory rather than allocating extensive arrays. If you have close control over the creation and deletion of Maps, know in advance how much data they will store, and know how many records will not change much as your program starts, use Hashmap. Otherwise, use the Treemap.

Another really cool map that I have yet to meet in .NET is java.util.concurrent.ConcurrentSkipListMap . This is great for multi-threaded situations. It is fully thread safe and non-blocking.

(I found this question looking at one closed one in order to be a duplicate. I thought that both questions could use an answer that mentions something other than HashMaps, good, like HashMaps for most applications and the like, like C # Dictionary.)

+1
source

You are looking for an alternative to a general IDictionary in C # that accepts pairs (key, value).

In Java, there is a Dictionary class, which is the parent class of any class that accepts pairs (key, value).

A HashTable (which extends the dictionary) will suit your needs. It performs operations such as copying, deleting, adding, checking for an item, etc. However, while IDictionary supports the foreach loop in C #, to iterate over the HashTable in Java, you will need an Iterator.

The added benefit of HashTables is synchronization, so you don't have to worry about concurrency.

IF, however, you are looking for an asynchronous implementation, you should use a HashMap . HashMap also requires the iterator to go through it.

Undoubtedly, you also need to see how 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

0
source

Source: https://habr.com/ru/post/1380348/


All Articles