ToStringBuilder、HashCodeBuilder、EqualsBuilder、ToStringStyle、ReflectionToStringBuilder、CompareToBuilder等这些类都是位于commons-lang.jar下面的,所以要使用这些类一定要导入commons-lang.jar。
为什么要使用ToStringBuilder?
系统中一般都要打印日志的,因为所有实体的toString()方法 都用的是简单的"+",因为每"+" 一个就会 new 一个 String 对象,这样如果系统内存小的话会暴内存(前提系统实体比较多)。使用ToStringBuilder就可以避免暴内存这种问题的。
ToStringBuilder的append方法
ToStringBuilder类主要用于类的格式化输出。ToStringBuilder中append方法可以向该类添加基本类型、数组、和对象,只有添加的方法才会被toString输出。如:
class TaxReturn {
private String ssn;
private int year;
private String lastName;
private BigDecimal taxableIncome;
// get/set方法省略
public TaxReturn() {
}
public TaxReturn(String pSsn, int pYear, String pLastName, BigDecimal pTaxableIncome) {
setSsn(pSsn);
setYear(pYear);
setLastName(pLastName);
setTaxableIncome(pTaxableIncome);
}
public String toString() {
return new ToStringBuilder(this).append("ssn", ssn).append("year", year).append("lastName",
lastName).toString();
}
public int hashCode() {
return new HashCodeBuilder(3, 7).append(ssn).append(year).toHashCode();
}
public boolean equals(Object pObject) {
boolean equals = false;
if (pObject instanceof TaxReturn) {
TaxReturn bean = (TaxReturn) pObject;
equals = (new EqualsBuilder().append(ssn, bean.ssn).append(year, bean.year)).isEquals();
}
return equals;
}
public int compareTo(Object pObject) {
return CompareToBuilder.reflectionCompare(this, pObject);
}
}
public class MainClass {
public static void main(String[] pArgs) throws Exception {
TaxReturn return1 = new TaxReturn("012-68-3242", 1998, "O'Brien", new BigDecimal(43000.00));
TaxReturn return2 = new TaxReturn("012-68-3242", 1999, "O'Brien", new BigDecimal(45000.00));
TaxReturn return3 = new TaxReturn("012-68-3242", 1999, "O'Brien", new BigDecimal(53222.00));
System.out.println("ToStringBuilder: " + return1.toString());
}
}
运行结果如下:
ToStringBuilder: TaxReturn@1503a3[ssn=012-68-3242,year=1998,lastName=O'Brien]
2、ToStringBuilder的reflectionToString方法
该方法主要是把类对应的基本属性和值输出来。如:
public class MainClass {
public static void main(String[] args) {
MyClass one = new MyClass("Becker", 35);
MyClass two = new MyClass("Becker", 35);
MyClass three = new MyClass("Agassi", 33);
System.out.println("One>>>" + one);
System.out.println("Two>>>" + two);
System.out.println("Three>>>" + three);
System.out.println("one equals two? " + one.equals(two));
System.out.println("one equals three? " + one.equals(three));
System.out.println("One HashCode>>> " + one.hashCode());
System.out.println("Two HashCode>>> " + two.hashCode());
System.out.println("Three HashCode>>> " + three.hashCode());
}
}
class MyClass {
private String name = null;
private int age = 0;
public MyClass(String name, int age) {
this.name = name;
this.age = age;
}
public boolean equals(Object obj) {
return EqualsBuilder.reflectionEquals(this, obj);
}
public String toString() {
return ToStringBuilder.reflectionToString(this,
ToStringStyle.MULTI_LINE_STYLE);
}
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}
}
运行结果如下:
One>>>MyClass@743399[
name=Becker
age=35
]
Two>>>MyClass@1d8957f[
name=Becker
age=35
]
Three>>>MyClass@3ee284[
name=Agassi
age=33
]
one equals two? true
one equals three? false
One HashCode>>> 462213092
Two HashCode>>> 462213092
Three HashCode>>> -530629296
ToStringStyle参数说明:
1. DEFAULT_STYLE
com.entity.Person@182f0db[name=John Doe,age=33,smoker=false]
2. MULTI_LINE_STYLE
com.entity.Person@182f0db[
name=John Doe
age=33
smoker=false
]
3. NO_FIELD_NAMES_STYLE
com.entity.Person@182f0db[John Doe,33,false]
4. SHORT_PREFIX_STYLE (即截去了包名)
Person[name=John Doe,age=33,smoker=false]
5. SIMPLE_STYLE
John Doe,33,false
为什么要使用ToStringBuilder?
系统中一般都要打印日志的,因为所有实体的toString()方法 都用的是简单的"+",因为每"+" 一个就会 new 一个 String 对象,这样如果系统内存小的话会暴内存(前提系统实体比较多)。使用ToStringBuilder就可以避免暴内存这种问题的。
ToStringBuilder的append方法
ToStringBuilder类主要用于类的格式化输出。ToStringBuilder中append方法可以向该类添加基本类型、数组、和对象,只有添加的方法才会被toString输出。如:
class TaxReturn {
private String ssn;
private int year;
private String lastName;
private BigDecimal taxableIncome;
// get/set方法省略
public TaxReturn() {
}
public TaxReturn(String pSsn, int pYear, String pLastName, BigDecimal pTaxableIncome) {
setSsn(pSsn);
setYear(pYear);
setLastName(pLastName);
setTaxableIncome(pTaxableIncome);
}
public String toString() {
return new ToStringBuilder(this).append("ssn", ssn).append("year", year).append("lastName",
lastName).toString();
}
public int hashCode() {
return new HashCodeBuilder(3, 7).append(ssn).append(year).toHashCode();
}
public boolean equals(Object pObject) {
boolean equals = false;
if (pObject instanceof TaxReturn) {
TaxReturn bean = (TaxReturn) pObject;
equals = (new EqualsBuilder().append(ssn, bean.ssn).append(year, bean.year)).isEquals();
}
return equals;
}
public int compareTo(Object pObject) {
return CompareToBuilder.reflectionCompare(this, pObject);
}
}
public class MainClass {
public static void main(String[] pArgs) throws Exception {
TaxReturn return1 = new TaxReturn("012-68-3242", 1998, "O'Brien", new BigDecimal(43000.00));
TaxReturn return2 = new TaxReturn("012-68-3242", 1999, "O'Brien", new BigDecimal(45000.00));
TaxReturn return3 = new TaxReturn("012-68-3242", 1999, "O'Brien", new BigDecimal(53222.00));
System.out.println("ToStringBuilder: " + return1.toString());
}
}
运行结果如下:
ToStringBuilder: TaxReturn@1503a3[ssn=012-68-3242,year=1998,lastName=O'Brien]
2、ToStringBuilder的reflectionToString方法
该方法主要是把类对应的基本属性和值输出来。如:
public class MainClass {
public static void main(String[] args) {
MyClass one = new MyClass("Becker", 35);
MyClass two = new MyClass("Becker", 35);
MyClass three = new MyClass("Agassi", 33);
System.out.println("One>>>" + one);
System.out.println("Two>>>" + two);
System.out.println("Three>>>" + three);
System.out.println("one equals two? " + one.equals(two));
System.out.println("one equals three? " + one.equals(three));
System.out.println("One HashCode>>> " + one.hashCode());
System.out.println("Two HashCode>>> " + two.hashCode());
System.out.println("Three HashCode>>> " + three.hashCode());
}
}
class MyClass {
private String name = null;
private int age = 0;
public MyClass(String name, int age) {
this.name = name;
this.age = age;
}
public boolean equals(Object obj) {
return EqualsBuilder.reflectionEquals(this, obj);
}
public String toString() {
return ToStringBuilder.reflectionToString(this,
ToStringStyle.MULTI_LINE_STYLE);
}
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}
}
运行结果如下:
One>>>MyClass@743399[
name=Becker
age=35
]
Two>>>MyClass@1d8957f[
name=Becker
age=35
]
Three>>>MyClass@3ee284[
name=Agassi
age=33
]
one equals two? true
one equals three? false
One HashCode>>> 462213092
Two HashCode>>> 462213092
Three HashCode>>> -530629296
ToStringStyle参数说明:
1. DEFAULT_STYLE
com.entity.Person@182f0db[name=John Doe,age=33,smoker=false]
2. MULTI_LINE_STYLE
com.entity.Person@182f0db[
name=John Doe
age=33
smoker=false
]
3. NO_FIELD_NAMES_STYLE
com.entity.Person@182f0db[John Doe,33,false]
4. SHORT_PREFIX_STYLE (即截去了包名)
Person[name=John Doe,age=33,smoker=false]
5. SIMPLE_STYLE
John Doe,33,false