I’m wondering if the size() method invoked on ConcurrentHashMap is of the same complexity as the size() method for usual HashMap.
I’m wondering if the size() method invoked on ConcurrentHashMap is of the same complexity
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It isn’t. In my version of the JDK,
HashMap.size()hasO(1)complexity, whereasConcurrentHashMap.size()in the best case has to iterate over the segments. In the worst case it will lock all segments, which can be a pretty expensive operation in a multithreaded scenario.Of course, which is faster is a different question altogether. The answer largely depends on how many threads are accessing the map, and what it is exactly that they are doing.