• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

TreeMap and Sorting

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I compile this program I get this output
A=B
B=C
C=D
D=E
I am too confused about (=)
from where it has appeared in between
and the second thing I cant sort this collection in reverse order
here is the program
import java.util.*;
class MyComp implements Comparator{
public int compare(Object a,Object b){
String aStr, bStr;
aStr = (String)a;
bStr = (String)b;
return bStr.compareTo(aStr);
}
}
class CompDemo{
public static void main(String args[]){
TreeMap ts = new TreeMap();
ts.put("A","B");
ts.put("B","C");
ts.put("C","D");
ts.put("D","E");
Set set = ts.entrySet();
Iterator i = set.iterator();
while(i.hasNext()){
Object o = i.next();
System.out.println(o.toString());
}
}
}
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unfortunately your user name does not follow our site requirements, which you can read about again here. Please re-register with a valid name (i.e. one with a space in it). Thanks.
As for your question - ts.entrySet() returns a Set view of the mappings in ts. Each entry in this set is a Map.Entry, which is an interface. If you look at the source code of TreeMap, you will see that what it actually returns is a TreeMap.Entry, a static member class which is invisible outside the TreeMap class and implements Map.Entry. (Which is all we see of it publicly unless we look at the source code ) Anyway though, it is this class TreeMap.Entry which overrides toString() thus:
<code><pre>
public String toString() {
return key + "=" + value;
}
</pre></code>
So this is the method which gets called by your final println() statement, and puts the "=" sign in between the key and value in each Map.Entry.
As for your attempt to reverse the order of the TreeMap, you almost have it. All that is missing is that you have to tell the TreeMap to use the new Comparator class you have created:
<code><pre> TreeMap ts = new TreeMap(new MyComp());</pre></code>
Then the output is:
<code><pre>
D=E
C=D
B=C
A=B
</pre></code>

[This message has been edited by Jim Yingst (edited December 01, 2000).]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic