This article mainly introduces the java tostring method rewriting code example, which has certain reference value. Friends who need it can learn about it.
When an object needs to be output to the display, its toString() method is usually called to convert the object's content into a string. All classes in Java have a toString() method by default
By default, System.out.println (object name) or System.out.println (object name.toString()) outputs the class name of this object and the first address of the memory corresponding to this object. If you want Custom output information must override the toString() method
Notes
1. Must be declared as public
2. The return type is String
3. The name of the method must be toString and has no parameters
4. Do not use the output method System in the method body. out.println()
import java.util.*;
public class TreeSetTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method 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 compare(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=aDescription;
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]
ItemOverloaded toString ()After method :
import java.util.*;
public class TreeSetTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method 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 compare(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=aDescription;
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]]
Summary
The above is the detailed content of Code example for overriding tostring method in Java. For more information, please follow other related articles on the PHP Chinese website!
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PMThe article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.
How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PMThe article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.
How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PMThe article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra
How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PMThe article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]
How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PMJava's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software






