Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Statistics for java HashMap [closed]

During the lifecycle of a Java HashMap is there a way to determine how many collisions/chaining occured. How many times the HashMap was resized and how long each resize event took?

I checked source code for HashMap(1.6), and it looks like it can be extended to add the above functionality to it.

A little background, i have inherited some legacy application and since we are using CMS-GC, which happens to be non-compacting. These hashtables can have more than a million entries. We are facing some performance issues in production that occur only during activity spikes. And we have been unable to reproduce them in our production replica.

In my previous project we do print the our cache usage statistics periodically to determine their effectiveness.

Also would appreciate your opinion/criticism as to if this a right line of thinking?

like image 908
yichuan Avatar asked Nov 26 '14 16:11

yichuan


People also ask

How efficient is Java HashMap?

A HashMap shouldn't be more than 70% – 75% full. If it gets close, it gets resized and entries rehashed. Rehashing requires n operations which is costly wherein our constant time insert becomes of order O(n) It's the hashing algorithm which determines the order of inserting the objects in the HashMap.

Why HashMap is not recommended in a multi threaded environment?

It is a problem if multiple threads are adding to the same HashMap instance without it being synchronized . Even if just 1 thread is modifying a HashMap and other threads are reading from that same map without synchronization, you will run into problems.

How fast is HashMap get?

Since HashMap stores its values in hash buckets, you can generally get between O(1) and O(N) for a lookup depending on the amount of hash collisions the map hash.

What is the problem with HashMap in Java?

HashMap is similar to HashTable, but it is unsynchronized. It allows to store the null keys as well, but there should be only one null key object and there can be any number of null values. This class makes no guarantees as to the order of the map. To use this class and its methods, you need to import java.

What is HashMap values method in Java?

HashMap values() Method in Java. The java.util.HashMap.values() method of HashMap class in Java is used to create a collection out of the values of the map. It basically returns a Collection view of the values in the HashMap.

What types of data can be stored in a hashmap?

It can store different types: String keys and Integer values, or the same type, like: String keys and String values: Create a HashMap object called capitalCities that will store String keys and String values: The HashMap class has many useful methods.

What is the average of all even numbers in a hashmap?

It the average is 6.333, the rounded average value is 6.33 . ] The return type of the output is a floating point value which is the average of all values whose key elements are even numbers. Input consists of the number of elements in the HashMap and the HashMap<int, float>.

How do you find the number of items in a hashmap?

To find out how many items there are, use the size () method: Loop through the items of a HashMap with a for-each loop. Note: Use the keySet () method if you only want the keys, and use the values () method if you only want the values: Keys and values in a HashMap are actually objects.


1 Answers

You are going on right track. You can extend Hashmap and override the methods by using counts. Based upon conditions you can increase the counts.

Update: You can try to using Java's ManagementFactory API's that will give you more insights into hashmap.

like image 144
Abhijeet Dhumal Avatar answered Oct 17 '22 10:10

Abhijeet Dhumal