Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use Double as keys in a TreeMap?

As described in the answer to Double in HashMap, Doubles shouldn't be used in HashMaps because they are difficult to compare for equality. I believe my case is different, but I thought I'd ask to make sure since I didn't see anything about this.

I'm going to have a series of double values associated with objects, and I want them to be sorted by the double values. Is TreeMap an appropriate solution? Would there be a better one? The double values are generated a bunch of math, so the likelihood of a duplicate value is extremely low.

EDIT: I should clarify: all I need is to have this list of objects sorted by the doubles they're associated with. The values of the doubles will be discarded and I'll never call map.get(key)

like image 962
MalcolmOcean Avatar asked Jul 26 '12 19:07

MalcolmOcean


People also ask

Are duplicates allowed in TreeMap?

A TreeMap cannot contain duplicate keys. TreeMap cannot contain the null key. However, It can have null values.

Is TreeMap sorted by key or value?

In Java Language, a TreeMap always stores key-value pairs which are in sorted order on the basis of the key. TreeMap implements the NavigableMap interface and extends AbstractMap class. TreeMap contains unique keys. The elements in TreeMap are sorted on the basis of keys.

How many null keys are allowed in TreeMap?

HashMap allows storing at most one null key and many null values. However, TreeMap doesn't allow a null key but may contain many null values.

How does a TreeMap store its key-value pairs?

TreeMap stores key-value pairs. The main difference is that TreeMap sorts the key in ascending order. TreeMap is sorted as the ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.


2 Answers

Doubles shouldn't be used in HashMaps because they are difficult to compare for equality.

  • Will you ever try to get the values based on certain keys?

    • If yes, then the reasoning about "difficult to compare" applies and you should probably avoid such data structure (or always rely on tailMap / headMap / submap and fetch ranges of the map).

    • If no (i.e. you'll typically just do for (Double key : map.keySet()) ... or iterate over the entrySet) then I would say you're fine using Double as keys.

The double values are generated a bunch of math, so the likelihood of a duplicate value is extremely low.

  • Is it a bug if you actually do get a duplicate?

    • If yes then it's not the right data structure to use. You could for instance use a Multimap from Guava instead.

    • If no, (i.e. it doesn't matter which of the two values it maps to, because they can only differ by a small epsilon anyway) then you should be fine.

like image 128
aioobe Avatar answered Nov 13 '22 16:11

aioobe


The problem with doubles in tree maps is exactly the same as it is with doubles in hash map - comparing for equality. If you avoid calls of treeMap.get(myDouble) and stay with range queries instead (e.g. by using submap) you should be fine.

TreeMap<Double,String> tm = new TreeMap<Double,String>();
tm.put(1.203, "quick");
tm.put(1.231, "brown");
tm.put(1.233, "fox");
tm.put(1.213, "jumps");
tm.put(1.243, "over");
tm.put(1.2301, "the");
tm.put(1.2203, "lazy");
tm.put(1.2003, "dog");
for (Map.Entry<Double,String> e : tm.subMap(1.230, 1.232).entrySet()) {
    System.out.println(e);
}

This prints

1.2301=the
1.231=brown

See this snippet on ideone.

like image 22
Sergey Kalinichenko Avatar answered Nov 13 '22 14:11

Sergey Kalinichenko