简体   繁体   English

ConcurrentHashMap.get() 迭代器

[英]ConcurrentHashMap.get() Iterator

I am trying to retrieve a subset of keys in an already-populated ConcurrentHashMap in java.我正在尝试在 java 中已填充的 ConcurrentHashMap 中检索键的子集。 The example code attached works for iterating through the get method for the specified elements, but my question is about whether iterating on the get is actually the most efficient use of ConcurrentHashMap?附加的示例代码用于迭代指定元素的 get 方法,但我的问题是关于 get 迭代是否实际上是 ConcurrentHashMap 的最有效使用?

This implementation seems no different than a normal HashMap - is the concurrency automatically built in?这个实现似乎与普通的 HashMap 没有什么不同——并发是自动内置的吗? Is there a faster way to return all the elements for a subset of keys?有没有更快的方法来返回键子集的所有元素?

Looked into entrySet() method, but I am not entering the data into the map, it is pre-existing.查看 entrySet() 方法,但我没有将数据输入到地图中,它是预先存在的。 Also looked into the forEach(), but it didn't seem to accept a subset of the complete map.还查看了 forEach(),但它似乎不接受完整地图的子集。

import java.util.*; 
import java.util.concurrent.ConcurrentHashMap;

public class ConcurrentHashMapgetExample1 { 
    public static void main(String[] args) 
    { 
        ConcurrentHashMap<Integer, String> maphash = new 
ConcurrentHashMap<Integer, String>(); 
        maphash.put(1, "AA"); 
        maphash.put(2, "BB"); 
        maphash.put(3, "CC"); 
        maphash.put(4, "DD"); 
        maphash.put(5, "EE"); 

        int[] keySubSet = {1, 2, 3};

        System.out.println("mappings are: " + maphash); 
       for (int i = 1; i < keySubSet.length + 1; i++){
        System.out.println(" Value : " + maphash.get(i)); 
           }

       } 
 } 

Code outputs correctly.代码输出正确。 But it doesn't seem "concurrent".但它似乎并不“并发”。 Wondering if this is automatically an efficient implementation, or what would be.想知道这是否自动是一个有效的实现,或者会是什么。

From the javadoc :javadoc

A hash table supporting full concurrency of retrievals and high expected concurrency for updates.一个哈希表,支持检索的完全并发和更新的高预期并发。

Thus: misconception on your end.因此:对你的误解。 A ConcurrentHashMap does nothing by itself in parallel or multi-threaded ways.即ConcurrentHashMap做并行或多线程的方式本身没什么

It is simply a Map implementation that can be used by multiple threads in parallel.它只是一个 Map 实现,可以被多个线程并行使用。 The ordinary HashMap ... can not (well: of course you can read/write to a HashMap in parallel, but then you are subject to race conditions and all the potential issues that arise when multiple threads operate on a container that isn't implemented to take the necessary precautions).普通的 HashMap ... 不能(好吧:当然,您可以并行读取/写入 HashMap,但是您会受到竞争条件的影响以及当多个线程在不支持的容器上运行时出现的所有潜在问题)采取必要的预防措施)。

Long story short: a ConcurrentHashMap does not do what you assume it does.长话短说:即ConcurrentHashMap不会做你认为它做什么。

Edit, given the comments by the OP:编辑,鉴于 OP 的评论:

Yes, you are correct, there is no API, neither in the Map interface, nor the HashMap or ConcurrentHashMap provide specific means to somehow retrieve a subset of keys/values, or key+value pairs.是的,您是对的,Map 接口中没有 API,HashMap 或 ConcurrentHashMap 也没有提供特定方法来以某种方式检索键/值或键+值对的子集 You can only retrieve keySet() , values() or entrySet() as a whole.您只能整体检索keySet()values()entrySet() You then have to iterate those to collect the things you want, or to throw aways what you don't want.然后你必须迭代这些来收集你想要的东西,或者扔掉你不想要的东西。

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

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