Jakarta Commons Cookbook notes(一)

本文详细介绍了Java实用工具包commons-lang的功能,包括字符串操作、数组处理、日期格式化等常见任务的方法实现。此外还展示了如何利用该工具包进行数组转换、查找元素、日期处理等实用操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

jar name: commons-lang

 

一. Supplements to the java 2 platform

 

1. toString()

public void toString() {
  ReflectionToStringBuilder.toString(this);
}
 

 

2. hashCode()

public int hashCode() {
 return HashCodeBuilder.reflectionHashCode(this);
}
 

 

3. equal() 

public boolean equals(Object o){
 return EqualBuilder.reflectionEquals(this,o);
}
 

 

4. compareTo() 

public int compareTo(Object o) {
  return CompareToBuilder.reflectionCompare(this,o);
}
 

 

5. print a array

int[] intArray = new int[] {2,3,4,5,6}
ArrayUtils.toString(intArray));
 

 

6. cloning and reversing arrays

long array[] array = {1,3,2,3,5,6};
long[] reversed = ArrayUtils.clone(array);
ArraysUtils.reverse(reversed);

 

 

7. transforming between object arrays and primitive array

long primitiveArray = new long[] {12,100,2929,3223};
Long[] objectArray = ArrayUtils.toObject(primitiveArray);

// null values in the object array are stored as Double.NaN
ArrayUtils.toPrivimitive(doubleObject,Double.NaN);
 

 

8. find items in an array

String[] stringArray = {"red","orange","blue","brown","red"};
boolean containsBlue = ArraysUtils.contains(stringArray,"blue");
int indexOfRed = ArraysUtils.indexOf(stringArray,"red");
int lastIndexOfRed = ArraysUtils.lastIndexOf(stringArray,"red");
 

 

9. creating a map from multidimensional array

Object[] weightArray = new Object[][] {{"H", new Double(1.0003)},
							     {"He", new Double(4.0002)},
                               {"Li", new Double(6.032)});
Map weights = ArraysUtils.toMap(weightArray);
 

 

10. formatting dates

// thead-safe formatter for java
DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(new Date());
 

 

11. Rounding date objects

DateUtils.round(new Date(), Calendar.HOUR);
DateUtils.round(new Date(), Calendar.DATE_OF_MONTH);
DateUtils.round(new Date(), Calendar.YEAR);
 

 

12. truncating date object

DateUtils.truncat(new Date(), Calendar.MONTH);
DateUtils.truncat(new Date(), Calendar.HOUR);
 

 

13. Generating unique numberic identifiers

LonIdentifierFactory idfactory = IdentifierUtils.LongGenerator(false,0);
idFactory.nextLongIdentifier();
 

 

14. measure time

StopWatch clock = new StopWatch();
clock.start();
clock.stop();
clock.getTime();
clock.reset();
 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值