When you need to output an object to the display, you typically call his ToString () method to convert the object's contents to a string. All classes in Java have a ToString () method by default
By default, the System.out.println (object name) or System.out.println (object name. ToString ()) Outputs the class name of this object and the first address of this object's corresponding memory. If you want to customize the output information, you must override ToString () Method
Attention matters
1. Must be declared public
2. return type is string
3. The name of the method must be ToString and has no parameters
4. Do not use the output method in the body of the method System.out.println ()
Import java.util.*; public class Treesettest {/** * @param args */public static void main (string[] args) {//TODO auto-generated m
Ethod stub sortedset<item> parts=new treeset<item> ();
Parts.add (New Item ("Toaster", 1234));
Parts.add (New Item ("Widget", 4562));
Parts.add (New Item ("Modem", 9912));
SYSTEM.OUT.PRINTLN (parts); Sortedset<item> sortbydescription=new treeset<item> (New comparator<item> () {public int Co
Mpare (Item A, item b) {String descra=a.getdescription ();
String descrb=b.getdescription ();
Return Descra.compareto (DESCRB);
}
});
Sortbydescription.addall (parts);
System.out.println (sortbydescription); } class Item implements comparable<item> {public item (String adescription, int apartnumber) {Description=ad
Escription;
Partnumber=apartnumber;
Public String GetDescription () {return description; public boolean equals (Object otherobject) {if (tHis==otherobject) return true;
if (Otherobject==null) {return false;
} if (GetClass ()!=otherobject.getclass ()) {return false;
Item other= (item) Otherobject;
Return Description.equals (other.description) && partnumber==other.partnumber;
public int hashcode () {return 13*description.hashcode () +17*partnumber;
public int CompareTo (Item other) {return partnumber-other.partnumber;
Private String description;
private int partnumber; }The output is:
[Item@8c9e3a56, item@d780c206, ITEM@39C021BA]
[Item@39c021ba, Item@8c9e3a56, item@d780c206]
After the item overloads the ToString () method:
Import java.util.*; public class Treesettest {/** * @param args */public static void main (string[] args) {//TODO auto-generated m
Ethod stub sortedset<item> parts=new treeset<item> ();
Parts.add (New Item ("Toaster", 1234));
Parts.add (New Item ("Widget", 4562));
Parts.add (New Item ("Modem", 9912));
SYSTEM.OUT.PRINTLN (parts); Sortedset<item> sortbydescription=new treeset<item> (New comparator<item> () {public int Co
Mpare (Item A, item b) {String descra=a.getdescription ();
String descrb=b.getdescription ();
Return Descra.compareto (DESCRB);
}
});
Sortbydescription.addall (parts);
System.out.println (sortbydescription); } class Item implements comparable<item> {public item (String adescription, int apartnumber) {Description=ad
Escription;
Partnumber=apartnumber;
Public String GetDescription () {return description; Public String toString () {return "[description="+description +", partnumber= "+partnumber+"];
public boolean equals (Object otherobject) {if (this==otherobject) return true;
if (Otherobject==null) {return false;
} if (GetClass ()!=otherobject.getclass ()) {return false;
Item other= (item) Otherobject;
Return Description.equals (other.description) && partnumber==other.partnumber;
public int hashcode () {return 13*description.hashcode () +17*partnumber;
public int CompareTo (Item other) {return partnumber-other.partnumber;
Private String description;
private int partnumber; }
The output is:
[[description=toaster,partnumber=1234], [description=widget,partnumber=4562], [description=modem,partnumber=9912 ]]
[[description=modem,partnumber=9912], [description=toaster,partnumber=1234], [description=widget,partnumber=4562 ]]