妙用Commons良药 <六>

妙用Commons良药 <六>
[color=blue][size=14]1、定义toString(),hashCode(),equals()等内容[/size][/color]
toString()方法举例:
[code]import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

public String toString( ) {
return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
.append("lastName", lastName)
.append("firstName", firstName)
.toString( );
}[/code]
提示一点,ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE),第二个参数当然也可以为ToStringStyle.DEFAULT_STYLE,ToStringStyle.NO_FIELD_NAMES_STYLE和ToStringStyle.SIMPLE_STYLE


hashCode()与equals()方法举例:
[code]import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;

public class PoliticalCandidate {
public int hashCode( ) {
return new HashCodeBuilder(17, 37) //两个不为0且非偶的质数
.append(firstName)
.append(lastName).toHashCode( );
}

public boolean equals(Object o) {
boolean equals = false;
if ( o != null &&
PoliticalCandidate.class.isAssignableFrom(o) ) {
PoliticalCandidate pc = (PoliticalCandidate) o;
equals = (new EqualsBuilder( )
.append(firstName, ps.firstName)
.append(lastName, ps.lastName)).isEquals( );
}
return equals;
}
}[/code]


[color=blue][size=14]2、操作数组内容[/size][/color]
ArrayUtils.toString()方法可以输入数组内容,此方法接受任意一个数组作为参数并输出其内容,输出时所有元素位于大括号,中间逗号隔开.举例:
[code]int[] intArray = new int[] { 2, 3, 4, 5, 6 };
int[] multiDimension = new int[][] { { 1, 2, 3 }, { 2, 3 }, {5, 6, 7} };
System.out.println( "intArray: " + ArrayUtils.toString( intArray ) );
System.out.println( "multiDimension: " + ArrayUtils.toString( multiDimension ) );[/code]

使用ArrayUtils.reverse()方法可以反转一个数组.
使用ArrayUtils.toObject()和ArrayUtils.toPrimitive()两个方法,即可互相转换基本类型数组和对象数组。


当然,Commons也提供了方便的方法,可以在数组中搜索特定项.
使用ArrayUtils.contains()方法可以判定数组是否存在指完元素.当然有两个比较实用的方法ArrayUtils.lastIndexOf()和ArrayUtils.indexOf(),举例如下:
[code]import org.apache.commons.lang.ArrayUtils;

String[] stringArray = { "Red", "Orange", "Blue", "Brown", "Red" };
boolean containsBlue = ArrayUtils.contains( stringArray, "Blue" );
int indexOfRed = ArrayUtils.indexOf( stringArray, "Red");
int lastIndexOfRed = ArrayUtils.lastIndexOf( string, "Red" );

System.out.println( "Array contains 'Blue'? " + containsBlue );
System.out.println( "Index of 'Red'? " + indexOfRed );
System.out.println( "Last Index of 'Red'? " + lastIndexOfRed );[/code]
结果如下:
[quote]Array contains 'Blue'? true
Index of 'Red'? 0
Last Index of 'Red'? 4[/quote]

另外一个功能,你可以很容易将一个二维对象数组成一个Map对象,只需要ArrayUtils.toMap()这一个方法即可.举例如下:
[code]import org.apache.commons.lang.ArrayUtils;

Object[] weightArray =
new Object[][] { {"H" , new Double( 1.007)},
{"He", new Double( 4.002)},
{"Li", new Double( 6.941)},
{"Be", new Double( 9.012)},
{"B", new Double(10.811)},
{"Ne", new Double(20.180)} };
Map weights = ArrayUtils.toMap( weightArray );
Double hydrogenWeight = map.get( "H" );[/code]


[color=blue][size=14]3、怎样格式化日期[/size][/color]
如果你想格式化一个的时候,需要注意一下JDK中自带的SimpleDatFormat类不是线程安全的。你可以用FastDateFormat或DateFormatUtils来实现你想要的功能。如果在你的系统中有多个线程共享一个SimpleDatFormat实例,建议你马上改为SimpleDatFormat类.
举例:
[code]Date now = new Date( );
String isoDT = DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format( now );
System.out.println( "It is currently: " + isoDT );[/code]
结果是:
[quote]It is currently: 2004-03-26T16:20:00-07:00[/quote]

又举一个例子,如下:
[code]FastDateFormat formatter =
new FastDateFormat( "yyyy-mm", TimeZone.getDefault( ), Locale.getDefault( ));
String output = formatter.format( new Date( ) );
// output equals "2003-10"[/code]

DateFormatUtils的时间格式如下:
[code]ISO_DATE_FORMAT
yyyy-MM-dd"2004-01-02"

ISO_DATE_TIME_ZONE_FORMAT
yyyy-MM-ddZZ"2004-01-02-07:00"

ISO_DATETIME_FORMAT
yyyy-MM-dd'T'HH:mm:ss"2004-01-02T23:22:12"

ISO_DATETIME_TIME_ZONE_FORMAT
yyyy-MM-dd'T'HH:mm:ssZZ"2004-01-02T21:13:45-07:00"

ISO_TIME_FORMAT
'T'HH:mm:ss"T04:23:22"

ISO_TIME_NO_T_FORMAT
HH:mm:ss"05:12:34"

ISO_TIME_NO_T_TIME_ZONE_FORMAT
HH:mm:ssZZ"12:32:22-07:00"

ISO_TIME_TIME_ZONE_FORMAT
'T'HH:mm:ssZZ"T18:23:22-07:00"

SMTP_DATETIME_FORMAT
EEE, dd MMM yyyy HH:mm:ss Z"Wed, 01 Feb 2004 20:03:01 CST"[/code]

注:本文章代码均来自<<Jarkata Commonss Cookbook>>一书第一章的内容,下一讲主要说一下Functors方面的内容
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值