Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

tree map implementation

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)

I need bit more clarity on above two. please advise
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

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
Thats what mccarl said wasn't it?
Avatar of gudii9

ASKER

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.

Open in new window



what is the difference between above three.

all looks similar to me.

what is meaning of < and ? and >

please advise
Please read tutorial on generics first.
Generics
Avatar of gudii9

ASKER

i wonder why they write sentences so looooooooooong which makes it hard to understand and digest in API. please advise
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
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.
Avatar of gudii9

ASKER

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.
Avatar of gudii9

ASKER

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
Avatar of gudii9

ASKER

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.