java - 树状图和并发跳过列表图之间的区别?是否可以将 NavigableMap 与重复键的映射结构一起使用?

标签 java dictionary

我是 Java 初学者。我有一个方法如下:

 public void time_filter(Long t1, Long t2){
    NavigableMap<Long, Operations_enter> time_map = new TreeMap<Long, Operations_enter>();
    for(int i=0; i< tm.size();i++){
        Operations_enter op = new Operations_enter();
        op.cpuid_enter = cpu.get(i);
        op.func_enter = stt.get(i);
        op.time_enter = tm.get(i);
        op.src_enter = tok.get(i);

        time_map.put(op.time_enter, op);

    }


    for(Map.Entry<Long, Operations_enter> entry: time_map.subMap(t1, true, t2, true).entrySet()){
        Integer time_func = entry.getValue().func_enter;
        System.out.println(" Filtered function numbers corresponding to given time range is" + time_func);

        }

就像上面看到的那样,我使用 TreeMap 实现了 NavigableMap ,并且能够使用 NavigableMap 中的 subMap 方法过滤给定范围。但我现在有一个问题,time_enter 包含一些重复的键,因此当我使用TreeMap 时,这些值会被最新添加的重复值覆盖。我知道这就是 TreeMap 的工作原理。我也尝试使用ConcurrentSkipListMap,但结果是一样的。

ConcurrentSkipListMapTreeMap 之间的实际区别是什么?是否有可能使用 TreeMapConcurrentSkipListMap 实现 NavigableMap,同时将重复值包含在 NavigableMap 中

编辑代码现在看起来像这样:

  NavigableMap<Long, Operations_enter> time_map = new TreeMap<Long, Operations_enter>(new Comparator<Long>() {
        public int compare(Long o1, Long o2) {
            return o1.equals(o2) ? 1 : o1.compareTo(o2);
        }
    });
    for(int i=0; i< tm.size();i++){
        Operations_enter op = new Operations_enter();
        op.cpuid_enter = cpu.get(i);
        op.func_enter = stt.get(i);
        op.time_enter = tm.get(i);
        op.src_enter = tok.get(i);

        time_map.put(op.time_enter, op);

    }

    for(Map.Entry<Long, Operations_enter> entry: time_map.subMap(t1, true, t2, true).entrySet()){

       Integer time_func = entry.getValue().func_enter;
        System.out.println(" Filtered function numbers corresponding to given time range is" + time_func);
     }

最佳答案

ConcurrentSkipListMapTreeMap 类都是 NavigableMap 实现。实现该接口(interface)意味着他们按排序顺序保存 key 。这是他们的共同点。在其他方面,它们有很大不同。

Possibility to use NavigableMap with a map structure for duplicate keys?

不,TreeMapConcurrentSkipListMap 都不能处理重复的键。当做put时,现有的键将其先前的值替换为到达的值。

Difference between treemap and concurrentskiplistmap?

3 个差异:空值、性能和并发性。

空值

  • TreeMap 允许空值,而键必须为非空。
  • ConcurrentSkipListMap 禁止键或值中出现 null

性能

ConcurrentSkipListMap 使用 skip list技术。此类可以显示非常快的搜索性能,同时仍然允许快速插入。

跳过列表是可扩展的,即使在进行插入时也能保持大量条目的性能。

并发

正如其他人指出的,ConcurrentSkipListMap 被设置为 concurrentthread-safeTreeMap 不是。

结论

  • 如果您有大量条目,请使用 ConcurrentSkipListMap 而不是 TreeMap
  • 如果您要跨线程访问 NavigableMap,请使用 ConcurrentSkipListMap,切勿使用 TreeMap
  • 对于在单个线程中访问较少数量的条目,请使用 TreeMap
<小时/>

下表总结了各种 Map 实现。单击/点击进行缩放。

A table describing aspects of the various Map implementations in Java 11.

关于java - 树状图和并发跳过列表图之间的区别?是否可以将 NavigableMap 与重复键的映射结构一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17189833/

相关文章:

python - 如何删除 python 字典中的键/值对?

java - 为什么在java中使用map时出现空指针异常?

Python 父子图字典

java - 引用外部 Java 和 Android 库

java - Apache HttpClient 空响应 header

python - 如何知道哪本词典在我的列表中最关键

dictionary - 结构体作为 Go map 中的键

java - 如何使用 Java 中的正则表达式匹配函数来匹配具有元字符的字符串

java - 未报告的异常 java.io.FileNotFoundException;必须被捕获或宣布被抛出

java - Spring JPA/Hibernate 未更新到数据库以在保存时更改 @Entity 的 boolean 值