Skip to content
Advertisement

Difference between treemap and concurrentskiplistmap? Possibility to use NavigableMap with a map structure for duplicate keys?

I’m a beginner in Java. I have a method as below:

JavaScript

So like seen above, I implement a NavigableMap with TreeMap and Im able to filter for a given range using the subMap method in NavigableMap. But I have a problem now, the time_enter contains some duplicate keys, so when I use TreeMap, the values get overwritten by the latest added duplicate value. I understand that it is how TreeMap works. I tried using ConcurrentSkipListMap too, but the result was the same.

What is the actual difference between ConcurrentSkipListMap and TreeMap?? And is there a possibility to implement NavigableMap with either TreeMap or ConcurrentSkipListMap and at the same time have the duplicate values included in the NavigableMap?

EDIT The code looks like this now:

JavaScript

Advertisement

Answer

ConcurrentSkipListMap is a NavigableMap implementation designed to be safely used from multiple threads concurrently. This is its main difference from TreeMap. Since your time_map is a local variable it can be accessed only from current thread and you dont need ConcurrentSkipListMap.

If you need a TreeMap that allows duplicate keys, you can try to make a custom Comparator that never returns 0:

JavaScript

output

JavaScript

note that it breaks Map contract which may cause unexpected behavior eg m.containsKey(1) returns false instead you need to use m.subMap(0, false, 2, false) != null

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement