We help IT Professionals succeed at work.

# tree map implementation

on
Hi,

My eclipse produced below two TreeMap Impementation codes when i tried

TreeMap<Integer, String> ts=new TreeMap<Integer, String>(theComparator);

TreeMap<Integer, String> tm=new TreeMap<Integer, String>();

What is the difference between above two. theComparator is used for the value right?(which happened to be String here)

Comment
Watch Question

## View Solutions Only

Top Expert 2016
Commented:
Commented:
You use this one:

TreeMap<Integer, String> ts=new TreeMap<Integer, String>(theComparator);

when you want to use a custom ordering, e.g. sorting the strings in order but ignoring their case.

To do the custom ordering, you write a "Comparator" and pass it into the constructor.

The one without the comparator parameter just uses "default" ordering - alphabetical for strings, increasing value for numbers etc.

Make sense?

Doug
IT Business Systems Analyst / Software Developer
Top Expert 2015
Commented:
Just want to make one thing clear (in addition to what Doug has posted)...

...theComparator is used for the value right?(which happened to be String here)
NO, the comparator is used to order the entries in the map by their KEYS so in this case the type on the left, ie. the Integer is the key in this map. So in this case, if you don't specify a comparator, the entries will be order in numerical ascending order of their Integer keys. But if you do pass a comparator you can change that order, ie. as just one example, you can make the TreeMap order the entries in descending numerical order.
Top Expert 2015
Commented:
@gudii9
This question is extremely similar to the question I answered for you earlier here

The only difference it this is a TreeMap whilst the other was a TreeSet.

Commented:
NO, the comparator is used to order the entries in the map by their KEYS so in this case the type on the left, ie. the Integer is the key in this map. So in this case, if you don't specify a comparator, the entries will be order in numerical ascending order of their Integer keys.

i see by KEYS not VALUES
Top Expert 2015

Commented:
Thats what mccarl said wasn't it?

Commented:
``````TreeMap
public TreeMap(Comparator<? super K> comparator)
Constructs a new, empty tree map, ordered according to the given comparator. All keys inserted into the map must be mutually comparable by the given comparator: comparator.compare(k1, k2) must not throw a ClassCastException for any keys k1 and k2 in the map. If the user attempts to put a key into the map that violates this constraint, the put(Object key, Object value) call will throw a ClassCastException.
Parameters:
comparator - the comparator that will be used to order this map. If null, the natural ordering of the keys will be used.
TreeMap
public TreeMap(Map<? extends K,? extends V> m)
Constructs a new tree map containing the same mappings as the given map, ordered according to the natural ordering of its keys. All keys inserted into the new map must implement the Comparable interface. Furthermore, all such keys must be mutually comparable: k1.compareTo(k2) must not throw a ClassCastException for any keys k1 and k2 in the map. This method runs in n*log(n) time.
Parameters:
m - the map whose mappings are to be placed in this map
Throws:
ClassCastException - if the keys in m are not Comparable, or are not mutually comparable
NullPointerException - if the specified map is null
TreeMap
public TreeMap(SortedMap<K,? extends V> m)
Constructs a new tree map containing the same mappings and using the same ordering as the specified sorted map. This method runs in linear time.
``````

what is the difference between above three.

all looks similar to me.

what is meaning of < and ? and >

Top Expert 2015

Commented:
Generics

Commented:
i wonder why they write sentences so looooooooooong which makes it hard to understand and digest in API. please advise
Top Expert 2016

Commented:
They don't. They make them as short as possible and as precise as possible.

You problem is probably the opposite of what you think it is: i.e. you'd benefit from repetition, expansion and variation to provide clarification
Top Expert 2015

Commented:
You need to take time to read the subject.
Posting in forums and bouncing questions of people is not the way to go about doing it. You wont benefit from it unless you put the hard work to do some background reading.

Commented:
They make them as short as possible and as precise as possible.

i get they are short and precise. But readability of the new comers is challenge right. I read 3 times still trying to understand.

Commented:
Map<? extends K,? extends V> m

comma means two parameters it has right  one is key and other is value  But Parameters says only one as below

Parameters:
m - the map whose mappings are to be placed in this map

Commented:
i looked below link on generics which is more undersandable to me.

http://www.journaldev.com/1663/java-generics-tutorial-example-class-interface-methods-wildcards-and-much-more

So the API they are using upperbound syntax.