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
LVL 7
gudii9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dpearsonCommented:
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mccarlIT Business Systems Analyst / Software DeveloperCommented:
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.
0
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

gurpsbassiCommented:
@gudii9
Please read the API documentaiton.
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.
0
gudii9Author 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
0
gurpsbassiCommented:
Thats what mccarl said wasn't it?
0
gudii9Author 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.

Open in new window



what is the difference between above three.

all looks similar to me.

what is meaning of < and ? and >

please advise
0
gurpsbassiCommented:
Please read tutorial on generics first.
Generics
0
gudii9Author Commented:
i wonder why they write sentences so looooooooooong which makes it hard to understand and digest in API. please advise
0
CEHJCommented:
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
0
gurpsbassiCommented:
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.
0
gudii9Author 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.
0
gudii9Author 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
0
gudii9Author 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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.