妙用Commons良药 <六>
1、定义toString(),hashCode(),equals()等内容
toString()方法举例:
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( );
}
提示一点,ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE),第二个参数当然也可以为ToStringStyle.DEFAULT_STYLE,ToStringStyle.NO_FIELD_NAMES_STYLE和ToStringStyle.SIMPLE_STYLE
hashCode()与equals()方法举例:
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;
}
}
2、操作数组内容
ArrayUtils.toString()方法可以输入数组内容,此方法接受任意一个数组作为参数并输出其内容,输出时所有元素位于大括号,中间逗号隔开.举例:
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 ) );
使用ArrayUtils.reverse()方法可以反转一个数组.
使用ArrayUtils.toObject()和ArrayUtils.toPrimitive()两个方法,即可互相转换基本类型数组和对象数组。
当然,Commons也提供了方便的方法,可以在数组中搜索特定项.
使用ArrayUtils.contains()方法可以判定数组是否存在指完元素.当然有两个比较实用的方法ArrayUtils.lastIndexOf()和ArrayUtils.indexOf(),举例如下:
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 );
结果如下:
引用
Array contains 'Blue'? true
Index of 'Red'? 0
Last Index of 'Red'? 4
另外一个功能,你可以很容易将一个二维对象数组成一个Map对象,只需要ArrayUtils.toMap()这一个方法即可.举例如下:
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" );
3、怎样格式化日期
如果你想格式化一个的时候,需要注意一下JDK中自带的SimpleDatFormat类不是线程安全的。你可以用FastDateFormat或DateFormatUtils来实现你想要的功能。如果在你的系统中有多个线程共享一个SimpleDatFormat实例,建议你马上改为SimpleDatFormat类.
举例:
Date now = new Date( );
String isoDT = DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format( now );
System.out.println( "It is currently: " + isoDT );
结果是:
引用
It is currently: 2004-03-26T16:20:00-07:00
又举一个例子,如下:
FastDateFormat formatter =
new FastDateFormat( "yyyy-mm", TimeZone.getDefault( ), Locale.getDefault( ));
String output = formatter.format( new Date( ) );
// output equals "2003-10"
DateFormatUtils的时间格式如下:
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"
注:本文章代码均来自<<Jarkata Commonss Cookbook>>一书第一章的内容,下一讲主要说一下Functors方面的内容
分享到:
相关推荐
<artifactId>commons-fileupload</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.5</version> </...
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
添加依赖: <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext7-core... <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>1.3.2</version> </dependency>
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
commons-attributes-2.2-src.zip<br>commons-attributes-2.2.zip<br>commons-beanutils-1.8.0-BETA-src.zip<br>commons-beanutils-1.8.0-BETA.zip<br>commons-betwixt-0.8-src.zip<br>commons-betwixt-0.8.zip<br>...
<artifactId>commons-httpclient</artifactId> <version>3.0</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </...
<version>${commons-lang3.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.spring...
<artifactId>commons-collections4</artifactId> <version>4.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </...
struts-1.3.8 包(<br><br>antlr-2.7.2.jar<br>bsf-2.3.0.jar<br>commons-beanutils-1.7.0.jar<br>commons-chain-1.1.jar<br>commons-digester-1.8.jar<br>commons-fileupload-1.1.1.jar<br>commons-io-1.1.jar<br>...
<artifactId>standard</artifactId> <version>1.1.2</version> <artifactId>jstl</artifactId> ... <artifactId>commons-io</artifactId> <version>2.11.0</version> <artifactId>javax.servlet-api</artifactId>
<artifactId>commons-io</artifactId> <version>2.11.0</version> <artifactId>mybatis</artifactId> <version>3.5.5</version> <artifactId>mysql-connector-java</artifactId> <version>8.0.28</version> ...
其中包含:shale-core.jar<br>commons-beanutils.jar<br>commons-chain.jar<br>commons-codec.jar<br>commons-collections.jar<br>commons-digester.jar<br>commons-el.jar<br>commons-fileupload.jar<br>commons-...
<module>cloud-api-commons</module> <module>cloud-eureka-server7001</module> <module>cloud-eureka-server7002</module> <module>cloud-comsumer-order80</module> <module>cloud-provider-payment8001</...
collections-2.1.1.jar<br>commons-logging-1.0.4.jar<br>dom4j-1.6.1.jar<br>ehcache-1.1.jar<br>hibernate3.jar<br>jaas.jar<br>jaxen-1.1-beta-7.jar<br>jdbc2_0-stdext.jar<br>jta.jar<br>log4j-1.2.11.jar<br>...
api.jar<br>jxl.jar<br>itext-1.3.jar<br>poi-2.5.1.jar<br>cos.jar<br>velocity-1.5.jar<br>velocity-tools-view-1.3.jar<br>commons-codec.jar<br>antlr.jar<br>commons-beanutils.jar<br>commons-digester.jar<br...
ssm在maven项目管理下的pom文件,jar包依赖 <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> ... <artifactId>commons-codec</artifactId> <version>1.10</ver