什么将在 Java 中存储键值列表或在 Java 中存储 C# IDictionary 的替代品?
我是java开发新手,我来自C#.net,开发android应用程序。
我正在寻找在 Java 中使用的键值列表,与 C# 中的 IDictionary 相同。
谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您正在寻找的接口是
Map
。对于类似于 C# 的
Dictionary
的实现,请尝试HashMap
The interface you are looking for is
Map<K, V>
.For an implementation similar to C#'s
Dictionary
tryHashMap<K, V>
我通常更喜欢 java.util.TreeMap 而不是 HashMap。它不是那么快,但您不必担心哈希算法或设置大小。更重要的是,它是内存友好的,因为它使用小块内存而不是分配大量数组。如果您可以密切控制地图的创建和处置,提前知道它们将保存多少数据,并且知道条目数量不会随着程序运行而发生显着变化,请使用哈希图。否则使用树形图。
另一个非常酷的 Map 是 java.util.concurrent.ConcurrentSkipListMap,我在 .NET 中还没有遇到过类似的 Map。这对于多线程情况非常有用。它是完全线程安全的并且是非阻塞的。
(我在查看一个因重复而关闭的问题时发现了这个问题。我认为这两个问题都可以使用提到除 HashMap 之外的其他内容的答案,因为 HashMap 对于大多数用途和来说都很好,因为它们与C# 词典。)
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.)
您正在寻找 C# 中通用 IDictionary 的替代方案,它采用(键,值)对。
在Java中,有一个Dictionary类,它是任何类的父类,它接受(键,值)对。
HashTable(它扩展了字典)将满足您的需要。它执行复制、删除、添加、检查项目是否存在等操作。然而,虽然 IDictionary 支持 C# 中的 foreach 循环,但要在 Java 中循环 HashTable,您将需要一个迭代器。
HashTables 的另一个优点是它是同步的,因此您不必担心并发性。
但是,如果您正在寻找异步实现,则应该使用HashMap。 HashMap 也需要迭代器来遍历它。
毫无疑问,您还需要了解迭代器在Java中的工作原理。
哈希表: http://download.oracle.com /javase/1.4.2/docs/api/java/util/Hashtable.html
HashMap:http://download.oracle.com/javase/1.4 .2/docs/api/java/util/HashMap.html
迭代器:http://download.oracle.com/javase/1.4.2/docs/api/java/util/Iterator.html
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
使用
Map接口
,以及
HashMap
类。或者查看Map
接口描述中的“所有已知子接口”部分以了解更多实现。http://download.oracle.com/javase/6 /docs/api/java/util/Map.html
Use the
Map<K, V>
interface, and theHashMap<K, V>
class for example. Or see the "All Known Subinterfaces" section in theMap<K, V>
interface description for more implementations.http://download.oracle.com/javase/6/docs/api/java/util/Map.html
Java 有 哈希表 和 HashMap
请参阅HashMap 和 Hashtable 之间的区别?
Java has Hashtable and HashMap
See Differences between HashMap and Hashtable?