I am new to java development, I am from C# .net, developing android application.
我是java开发的新手,我来自C#.net,正在开发android应用程序。
I am looking for Key-Value list to use in Java same as IDictionary in C#.
我正在寻找在Java中使用的Key-Value列表,与C#中的IDictionary相同。
Thanks
谢谢
5 个解决方案
#1
5
Java has Hashtable and HashMap
Java有Hashtable和HashMap
See Differences between HashMap and Hashtable?
请参阅HashMap和Hashtable之间的差异?
#2
9
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.
例如,使用Map
http://download.oracle.com/javase/6/docs/api/java/util/Map.html
http://download.oracle.com/javase/6/docs/api/java/util/Map.html
#3
3
The interface you are looking for is Map<K, V>
.
您正在寻找的界面是Map
For an implementation similar to C#'s Dictionary
try HashMap<K, V>
对于类似于C#的Dictionary的实现,尝试HashMap
#4
1
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.
我通常更喜欢java.util.TreeMap到HashMap。它不是那么快,但您不必担心散列算法或设置大小。更重要的是,它是内存友好的,因为它使用小块内存而不是分配大量数组。如果您可以严密控制地图的创建和处理,请事先知道它们将保留多少数据,并且知道在程序运行时条目数量不会发生显着变化,请使用Hashmap。否则使用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.
另一个非常酷的Map,我还没有遇到过像.NET这样的版本,它是java.util.concurrent.ConcurrentSkipListMap。这对于多线程情况非常有用。它完全是线程安全的,并且是非阻塞的。
(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.)
(我发现这个问题的同时看一个被关闭的副本。我认为这两个问题都可以使用一个提到HashMaps以外的东西的答案,好像HashMaps用于大多数用途,类似于C#字典。)
#5
0
You are looking for an alternative to the generic IDictionary in C# which takes (key,value) pairs.
您正在寻找C#中通用IDictionary的替代方法,它采用(键,值)对。
In Java, there is a Dictionary class that is the parent class of any class, which accepts (key,value) pairs.
在Java中,有一个Dictionary类,它是任何类的父类,它接受(键,值)对。
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.
HashTable(扩展词典)将满足您的需求。它执行复制,删除,添加等操作,检查项目是否存在等。但是,虽然IDictionary在C#中支持foreach循环,但要在Java中循环HashTable,您将需要一个Iterator。
An added advantage with HashTables is that it is synchronized, so you would not have to worry about concurrency.
HashTables的另一个优点是它是同步的,因此您不必担心并发性。
IF however, you are looking for an asynchronous implementation, you should use the HashMap. A HashMap too requires an Iterator to traverse through it.
但是,如果您正在寻找异步实现,则应使用HashMap。 HashMap也需要Iterator遍历它。
Undoubtedly, you also need to look at how an Iterator works in Java.
毫无疑问,您还需要了解Iterator如何在Java中工作。
HashTables: http://download.oracle.com/javase/1.4.2/docs/api/java/util/Hashtable.html
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
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
迭代器:http://download.oracle.com/javase/1.4.2/docs/api/java/util/Iterator.html
#1
5
Java has Hashtable and HashMap
Java有Hashtable和HashMap
See Differences between HashMap and Hashtable?
请参阅HashMap和Hashtable之间的差异?
#2
9
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.
例如,使用Map
http://download.oracle.com/javase/6/docs/api/java/util/Map.html
http://download.oracle.com/javase/6/docs/api/java/util/Map.html
#3
3
The interface you are looking for is Map<K, V>
.
您正在寻找的界面是Map
For an implementation similar to C#'s Dictionary
try HashMap<K, V>
对于类似于C#的Dictionary的实现,尝试HashMap
#4
1
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.
我通常更喜欢java.util.TreeMap到HashMap。它不是那么快,但您不必担心散列算法或设置大小。更重要的是,它是内存友好的,因为它使用小块内存而不是分配大量数组。如果您可以严密控制地图的创建和处理,请事先知道它们将保留多少数据,并且知道在程序运行时条目数量不会发生显着变化,请使用Hashmap。否则使用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.
另一个非常酷的Map,我还没有遇到过像.NET这样的版本,它是java.util.concurrent.ConcurrentSkipListMap。这对于多线程情况非常有用。它完全是线程安全的,并且是非阻塞的。
(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.)
(我发现这个问题的同时看一个被关闭的副本。我认为这两个问题都可以使用一个提到HashMaps以外的东西的答案,好像HashMaps用于大多数用途,类似于C#字典。)
#5
0
You are looking for an alternative to the generic IDictionary in C# which takes (key,value) pairs.
您正在寻找C#中通用IDictionary的替代方法,它采用(键,值)对。
In Java, there is a Dictionary class that is the parent class of any class, which accepts (key,value) pairs.
在Java中,有一个Dictionary类,它是任何类的父类,它接受(键,值)对。
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.
HashTable(扩展词典)将满足您的需求。它执行复制,删除,添加等操作,检查项目是否存在等。但是,虽然IDictionary在C#中支持foreach循环,但要在Java中循环HashTable,您将需要一个Iterator。
An added advantage with HashTables is that it is synchronized, so you would not have to worry about concurrency.
HashTables的另一个优点是它是同步的,因此您不必担心并发性。
IF however, you are looking for an asynchronous implementation, you should use the HashMap. A HashMap too requires an Iterator to traverse through it.
但是,如果您正在寻找异步实现,则应使用HashMap。 HashMap也需要Iterator遍历它。
Undoubtedly, you also need to look at how an Iterator works in Java.
毫无疑问,您还需要了解Iterator如何在Java中工作。
HashTables: http://download.oracle.com/javase/1.4.2/docs/api/java/util/Hashtable.html
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
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
迭代器:http://download.oracle.com/javase/1.4.2/docs/api/java/util/Iterator.html