aspose file tools*
The moose likes Beginning Java and the fly likes Using TreeMap with comparator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using TreeMap with comparator" Watch "Using TreeMap with comparator" New topic
Author

Using TreeMap with comparator

Preet Rams
Greenhorn

Joined: May 27, 2013
Posts: 25
I have the following code in Java:





As seen, I use treemap with comparator to get values from a NavigableMap. But I have a problem when I use submap...I dont get the values corresponding to t1 or the first entry in the submap, when I use the comparator. I use the comparator, so that I can include the duplicate elements also in the treemap. Can anyone tell me where Im, going wrong??
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

I don't know if this is the reason for what you're seeing, but that isn't a valid compare method. It breaks the contract of the method, which states:

The implementor must ensure that sgn(compare(x, y)) == -sgn(compare(y, x)) for all x and y.


It wouldn't surprise me if breaking this causes unexpected behaviour, because the TreeMap will have been written under the assumption that this is always true. The bottom line is, you shouldn't try and put duplicate keys in a map. Whatever the intention is, there must be a better solution. For instance, using a Map<Long, List<Operations_enter>> would allow you to have multiple values against a time.
Preet Rams
Greenhorn

Joined: May 27, 2013
Posts: 25
Ok I understand that this is not the best way to do it. But I want to use the NavigableMap interface which has the method submap feature. Unfortunately it is only possible to implement NavigableMap with TreeMap and ConcuurentSkipListMap, both of which does not support the duplicate key feature. So what do you think is the best solution in this case?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

Well, the approach I suggested would allow you to use a navigable map. If you want to record multiple values against a single time that's an obvious solution. Another would be to forget the idea of using a map (because if you aren't looking up by the key you don't need it) and just use a navigable set like TreeSet<Operations_enter> instead with a suitable Comparator. The Comparator would have to consider each value unique, so you could sort by time and then by whichever other fields you need to make it unique.
Preet Rams
Greenhorn

Joined: May 27, 2013
Posts: 25
I think you got me wrong, I have multiple values for time(key) and there is a unique value for all the keys(times) and I want to retrieve the values for all the fields(cpuid, stream, func) corresponding to the entered time range. But the time range has duplicate values, and Im not able to get all the values when I use Map structure. Hope this makes my question clear.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

Not really. You say that there's a unique value for times, but you have multiple values for times. That's a contradictory statement.

However, I think I understand what you're trying to do, and if so the approaches I just described will allow it.

- Create a TreeMap<Long, List<Operations_enter>> using a natural ordering
- For each Operations_enter object:
- If the time doesn't exist as a key in the map, add it with the value a List
- Add the Operations_enter object to the List
- Use .subMap() to get all the values in a time range.
- Iterate over the map to get the Lists
- Iterate over each List

or

- Create a suitable Comparator<Operations_enter>
- Create a TreeSet<Operations_enter> using that Comparator
- Add all the Operations_enter objects
- Use .subSet() to get all the values in a time range. There's a tricky point that you'll have to create dummy Operations_enter objects for the end points, but it's still doable.
- Iterate over the subset
Preet Rams
Greenhorn

Joined: May 27, 2013
Posts: 25
Thanks for the reply Matthew! I mean that there are duplicate values in time and the other fields like cpuid, func, streaam corresponding to the time have a unique value.

For eg. time = 1, cpuid = 4, func = 20, stream = 2
time = 1, cpuid = 3, func = 31, stream = 3
time = 2, cpuid = 5, func = 5, stream = 6
time = 3, cpuid = 1, func = 21, stream = 3
time = 4, cpuid = 3, func = 6, stream = 5
time = 4, cpuid = 4, func = 72, stream = 4

So this is the situation. And I want to get the time values from say time = 1 to time= 3... and the corresponding values for cpuid, stream, func. And I want the duplicate values too...in this case, the time = 1 occurs twice and I want to retrieve the value for both. Will the suggestions given you by in your previous post help with this? TreeMap does not allow duplicate elements. That is my problem now
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

That's what I thought. Either of the approaches I mentioned ought to work. A TreeMap<Long, List<Operations_enter>> lets you have multiple values recorded against the same key without having to duplicate that key.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using TreeMap with comparator
 
Similar Threads
Where are Inner Classes used (except for the SCJP test)?
Please help me to understand why i am getting an empty map and how can i fix it
Using Comparator with TreeNap
Sorting TreeMap with values
PriorityQueue FIFOEntry and Generics