原文地址
基本
- toString
- ToStringBuilder, ToStringStyle
- hashCode
- HashCodeBuilder:参数奇数,素数
- equals
- EqualsBuilder:最好使用能唯一标示的字段
- compareTo
- CompareToBuilder:参数比较顺序类似栈,后加的先比较
数组
- 打印数组
- ArrayUtils.toString(array);ArrayUtils.toString(list.toArray())
- 复制数组与翻转数组
- ArrayUtils.clone(array) ArrayUtils.reverse(array);jdk的数组实现的clone方法也可以复制数组只是大家都不会去注意,int a[]={1,2};int b[]=(int[])a.clone();jdk的Collections.reverse()可以实现list的翻转
- 原生类型与包装类型数组的转换
- ArrayUtils.toObject(array), ArrayUtils.toPrimitive(array);结合Arrays.asList()与List.toArray()使用
- 数组元素的查找
- ArrayUtils.contains(array, value), ArrayUtils.indexOf(array, value), ArrayUtils.lastIndexOf(array, value)
- 二维数组向Map转换
- ArrayUtils.toMap(array)
格式化日期
- DateFormatUtils(使用FastDateFormat格式化日期,线程安全)
- round日期
- DateUtils.round(date, Calendar.**):最接近date的日期
- truncate日期
- DateUtils.truncate(date, Calendar.**):将日期截断到指定的
生成一个Unique ID
- IdentifierUtils, IdentifierFactory;首先通过IdenfifierUtils获取一个IdentifierFactory,然后通过IdentifierFactory产生唯一标识。
文本处理(StringUtils 与WordUtils)
- 检查字符串是否为空
- StringUtils.isBlank(str),StringUtils.isNotBlank(str)
- 简略字符串
- StringUtils.abbreviate(str)
- 切分字符串
- StringUtils.split(str)
- 获取字符串之间的内容
- StringUtils.substringBetween(str, start, end)
- trim与strip
- StringUtils.trim(str), StringUtils.trimToNull(str)
- chomp
- StringUtils.chomp(str)
- 字符串强调,格式
- StringUtils.repeat(), StringUtils.center(), StringUtils.join()
- 翻转字符串
- StringUtils.reverse(), StringUtils.reverseDelimited()
- 折叠字符串
- WordUtils.wrapTest(), WordUtils.wrap()
- 测试字符串的内容
- StringUtils.isNumeric(), StringUtils.isAlpha(), StringUtils.isAlphanumeric(), StringUtils.isAlphaspace(),StringUtils.isAlphanumbericSpace()
- 检查字符串的次数
- StringUtils.countMatches()
- 解析格式化的字符串
- StringUtils.substringBetween(), StringUtils.substringAfter(), StringUtils.substringBefore(),StringUtils.substringBeforeLast(), StringUtils.substringAfterLast()
- 计算字符串的编辑距离
- StringUtils.getLevenshteinDistance(), StringUtils.difference(), StringUtils.indexOfDifference()
字符编码
- Base64, Hex, SoundEx
JavaBean操作
- 获取Bean的简单属性
- PropertyUtils.getSimpleProperty()
- 获取Bean的属性的属性
- PropertyUtils.getNestedProperty()
- 获取带索引的属性
- PropertyUtils.getIndexedProperty()
- 获取Map属性
- PropertyUtils.getMappedProperty()
- 综合访问所有属性
- PropertyUtils.getProperty( country, “regions[0].cities(richmond).population” );
- 获取属性的类型
- PropertyUtils.getPropertyType(), PropertyUtils.getPropertyDescriptor(), PropertyDescriptor.getProtertyType(), PropertyDescroptor.getWriteMethod(), PropertyDescriptor.getReadMethod()
- Bean之间的比较
- BeanComparator(String property)
- 拷贝Bean的属性
- PropertyUtils.copyProperties()
- 克隆Bean
- BeanUtils.cloneBean()
- 为Bean的属性赋值
- PropertyUtils.setProperty()
- 判断属性是否可读或可写
- PropertyUtils.isReadable(), PropertyUtils.isWritable()
- 使用Predicate判断Bean是否满足条件
- BeanPredicate(),
- 将Bean的属性转换为Map
- PropertyUtils.describe()
- 将Bean的属性信息转换到Map中
- BeanMap(bean), BeanMap.get(), BeanMap.getType()
- 将Bean的属性转换为字符串或通过字符串为其设置值
- BeanUtils.getProperty(), BeanUtils.setProperty()
算子
- 逆序比较器
- ReverseComparator()
- 比较器链
- ComparatorChain(), ComparatorChain.addComparator()
- 空比较器
- NullComparator()
- 固定顺序的比较器
- FixedOrderComparator()
- 简单谓词
- EqualPredicate(), InstanceofPredicate(), IdentityPredicate(), NotPredicate(),NullPredicateNullIsTruePredicate(), NotNullPredicateNullIsFalsePredicate(), TruePredicate(), FalsePredicate(), UniquePredicate()
- 自定义谓词
- 实现Predicate接口
- 创建复合谓词
- AndPredicate(),OrPredicate(), AllPredicate(), OnePredicate(), AnyPredicate(), NonePredicate()
- 转换对象
- 实现Transformer接口
- 转换链
- ChainedTransformer()
- 依赖于谓词的转换
- SwitchTransformer(predicateArray, transformerArray, NOPTransformer())
- 执行器
- 实现Closure接口
- 执行器链
- ChainedClosure()
- 带有条件的执行器
- IfClosure(predicate, yesClosure, noClosure)
- 循环执行器
- WhileClosure(predicate, closure), ForClosure(counter, closure)
集合
- 无限循环迭代器
- LoopingIterator()
- ArrayList迭代器
- ArrayListIterator()
- 使用谓词过滤集合数据
- FilterIterator(iterator, predicate),CollectionUtils.filter(list, predicate)会修改list留下过滤后的内容,CollectionUtil.select()以及CollectionUtil.selectRejected()不会修改原来数据会返回一个新的集合
- 遍历集合去重
- UniqueFilterIterator(iterator) = FilterIterator(interator, UniquePredicate)
- 记录对象出现次数
- Bag(object, counter), HashBag, TreeBag->维护对象添加的次序
- 缓冲
- Buffer[add, remove], UnbounededFifoBuffer, BoundedFifoBuffer
- 优先队列
- 使用PriorityBuffer(),可以添加comparator来实现比较
- 实现阻塞的缓冲区
- BlockingBuffer.decorate(BoundedFifoBuffer),
- 一个键对应多个值
- MultiMap()
- 通过key获取value,通过value获取key
- BidiMap, DualHashBidiMap->两个HashMap, DualTreeBidiMap->两个TreeMap, TreeBidiMap->红黑树
- key是字符串,忽略大小写
- CaseInsensitiveMap()
- 限制List与Map的类型
- TypedCollection.decorate(list, String.class), TypedMap.decorate(map, String.class, Number.class);
- 使用谓词限制Map的key和value
- PredicatedMap.decorate(new HashMap(), keyPredicate, valuePredicate)
- 使用谓词限制List的内容
- PredicatedList.decorate(list, predicate)
- **使用Predicate限制容器内容的装饰器
- PredicatedBag, PredicatedBuffer, PredicatedCollection, PredicatedList, PredicatedMap,PredicatedSet, PredicatedSortedBag, PredicatedSortedBag, PredicatedSortedMap, PredicatedSortedSet
- 使用Transformer变换容器中的内容
- CollectionUtils.transform(collection, transformer), CollectionUtils.transform(colleciton, transformerChain)
- ** 创建一个LeastRecentlyUsed缓存**
- LRUMap()
- 当key被请求时才将value放入Map
- LazyMap.decorate(map, transformer):key被请求时会调用transformer,transformer的返回值会作为value放入map
- 计算一个集合中元素出现的次数
- CollectionUtils.countMatches(collection, predicate), CollectionUtils.cardinality(object, collection), CollectionUtils.getCardinalityMap(collection):返回一个集合中元素出现次数的Map
- 对集合操作
- CollecitonUtils.union(), CollectionUtils.intersection(), CollectionUtils.disjunction(), CollectionUtils.subtract()
- 从Map中获取值不经过转换
- MapUtils.getBoolean(), MapUtils.getBooleanValue(), MapUtils.getDouble(), MapUtils.getDoubleValue(), MapUtils.getNumber()
XML
- 操作xml
- Digester
应用程序结构
- 解析命令行参数
- CommandLineParser, Option, CommandLine, OptionGroup, OptionBuilder, HelpFormatter
- 解析配置文件
- Configuration, PropertitiesConfiguration, DOMConfiguration
- 复合配置文件
- ConfigurationFactory
Math包
- 分数
- Fraction
- 获取数组最大最小值
- NumberUtils.max(array), Max.evaluate(), Min.evaluate()
- 数据范围
- DoubleRange, FloatRange, IntRange, LongRange, NumberRange
- 随机数
- RandomUtils.nextInt(), RandomUtils.nextLong(), RandomUtils.nextBoolean()
- 统计数组的最大,最小,平均,内积,和等
- StatUtils.max(),StatUtils.min(), StatUtils.mean(), StatUtils.product(), StatUtils.sum();除了使用StatUtils之外,还可以直接使用类Max, Min, Mean, Product, Sum, Percentile, GeometricMean, Skewness, Kurtosis
- 解线性方程
- RealMatrix
- 复数
- Complex, ComplexFormat
- 求解变量之间的关系
- SimpleRegression
模版
- commons-jexl包
IO与网络
- 复制流数据
- CopyUtils.copy(inputStream, outputStream), IOUtils.toString(inputStream)
- 关闭流
- IOUtils.closeQuietly(stream)
- 更容易阅读的文件大小
- FileUtils.byteCountToDisplaySize(bytes)
- 复制文件,字符串,URL内容到文件
- FileUtils.copyFile(src, dest), FileUtils.copyFileToDirectory(src, dir), FileUtils.writeStringToFile(dest, string, ?), FileUtils.copyURLToFile(src, dest)
- 删除目录
- FileUtils.deleteDirectory(dir), FileUtils.cleanDirectory(dir)
- 获得文件夹大小
- FileUtils.sizeOfDirectory()
- touch一个文件
- FilteUtils.touch(file)
- 过滤文件
- FilenameFilter, SuffixFileFilter, IOFileFilter, OrFileFilter, NotFileFilter, AndFileFilter
- 统计流读入与写出的比特数
- CountingOuputStream(stream), CountingInputStream(stream)
- 将一个流的数据写入两个不同的输出
- TeeOuputStream(ouputStream1, outputStream2)
- 使用oro利用globs与正则过滤文件
- GlobFilenameFilter(“.xml”),Perl5FilenameFilter(“^dev..tld$”)
- 使用net包
- FTPClient, SMTPClient, POP3Client
Http与WebDAV
- 执行HTTP的GET方法
HttpClient client = new HttpClient( );
String url = "http://www.discursive.com/jccook/";
HttpMethod method = new GetMethod( url );
try {
client.executeMethod( method );
if( method.getStatusCode( ) == HttpStatus.SC_OK ) {
String response = method.getResponseBodyAsString( );
System.out.println( response );
}
} catch( HttpException he ) {
System.out.println( "HTTP Problem: " + he.getMessage( ) );
} catch( IOException ioe ) {
System.out.println( "IO Exeception: " + ioe.getMessage( ) );
} finally {
method.releaseConnection( );
method.recycle( );
}- 添加参数
method.setQueryString(URIUtil.encodeQuery(“”));参数较多可以使用NameValuePair
获取内容
method.retResponseHeader()
调试HTTP连接
String logging = "org.apache.commons.logging";
// Configure Logging
System.setProperty(logging + ".Log", logging + ".impl.SimpleLog");
System.setProperty(logging + ".logging.simplelog.showdatetime", "true");
System.setProperty(logging + ".simplelog.log.httpclient.wire", "debug");
System.setProperty(logging + ".simplelog.log.org.apache.commons.httpclient", "debug");- 发送POST请求
-
- PostMethod, PostMethod.addParameter(), method.getResponseBodyAsString()
搜索与过滤
- commons-jxpath,lucene包
- 将文件内容通过POST请求发送出去
- postMethod.setRequestBody(new FileInputStream(file)), postMethod.setRequestContentLength((int)file.length());
- 发送文件
- MultipartPostMethod.addParameter(), MultipartPostMethod.addPart()
- 基本认证
- HttpState
- NTLM认证
- Credentials, NTCredentials
- Cookie
- Cookie()
- 重定向
- method.setFollowRedirects( true );
- SSL
- GetMethod自动处理
- 处理签名证书
- ProtocolSocketFactory, EasySSLProtocolSocketFactory, Protocol
- WebDAV资源
- UsernamePasswordCredentials, WebdavResource
- 修改WebDAV资源
- resource.lockMethod,resource.unlockMethod