简体   繁体   English

Java相当于C ++ std :: map?

[英]Java equivalent of C++ std::map?

I'm looking for a Java class with the characteristics of C++ std::map's usual implementation (as I understand it, a self-balancing binary search tree): 我正在寻找一个具有C ++ std :: map常用实现特性的Java类(据我所知,它是一个自平衡二进制搜索树):

  1. O(log n) performance for insertion/removal/search 插入/删除/搜索的O(log n)性能
  2. Each element is composed of a unique key and a mapped value 每个元素由唯一键和映射值组成
  3. Keys follow a strict weak ordering 键遵循严格的弱顺序

I'm looking for implementations with open source or design documents; 我正在寻找开源或设计文档的实现; I'll probably end up rolling my own support for primitive keys/values. 我可能最终会支持原始键/值。

This question's style is similar to: Java equivalent of std::deque , whose answer was "ArrayDeque from Primitive Collections for Java". 这个问题的风格类似于: Java等价于std :: deque ,其答案是“来自Java的原始集合的ArrayDeque”。

ConcurrentSkipListMap is a sorted map backed by a skip list (a self-balancing tree-like structure with O(log n) performance). ConcurrentSkipListMap是一个由跳过列表支持的有序映射(具有O(log n)性能的自平衡树状结构)。 Generally the bounds on CSLM are tighter than TreeMap (which is a self-balancing red-black tree impl) so it will probably perform better, with the side benefit of being thread-safe and concurrent, which TreeMap is not. 通常,CSLM上的边界比TreeMap(这是一个自平衡的红黑树impl)更紧密,因此它可能会表现得更好,具有线程安全和并发的附带好处,而TreeMap则不然。 CSLM was added in JDK 1.6. CSLM在JDK 1.6中添加。

Trove has a set of collections for primitive types and some other interesting variants of the common Java collection types. Trove有一组原始类型的集合以及常见Java集合类型的一些其他有趣变体。

Other collection libraries of interest include the Google Collection library and Apache Commons Collections . 其他感兴趣的收藏库包括Google Collection库Apache Commons Collections

The closest class to a binary tree in the standard Java libraries is java.util.TreeMap but it doesn't support primitive types, except by boxing (ie int is wrapped as an Integer, double as a Double, etc). 标准Java库中与二叉树最接近的类是java.util.TreeMap,但它不支持基本类型,除了通过装箱(即int包装为Integer,double包装为Double等)。

java.util.HashMap is likely to give better performance for large maps. java.util.HashMap可能会为大型地图提供更好的性能。 Theoretically it is O(1) but its precise performance characteristics depend on the hash code generation algorithm(s) for the key class(es). 从理论上讲,它是O(1),但其精确的性能特征取决于密钥类的哈希码生成算法。

According to Introduction to Collections : "Arrays ... are the only collection that supports storing primitive data types." 根据Collections简介 :“Arrays ...是唯一支持存储原始数据类型的集合。”

You can take a look at commons-collections FastTreeMap as well. 你也可以看一下commons-collections FastTreeMap

I doubt you will find many collections that support primitive types without boxing, so just live with it. 我怀疑你会发现许多支持原始类型而没有装箱的集合,所以只需要忍受它。 And that is not necessarily needed, because of autoboxing . 由于自动装箱 ,这不一定是必需的。

If you really want to use primitive (after making benchmarks that show insufficient performance!), you can see the source of the FastTreeMap and add methods for handling primitives. 如果你真的想使用原语(在显示性能不足的基准之后!),你可以看到FastTreeMap的源代码并添加处理原语的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM