常有Util类总结

org.apache.commons.lang.StringUtils 使用:
// StringUtils.EMPTY 用法
eturnValue = ""; 
改写成:returnValue = StringUtils.EMPTY;
// 判断Strring是否为空
boolean StringUtils.isNotEmpty(String str)
boolean StringUtils.isEmpty(String str)
boolean StringUtils.isBlank(String str)
实例:
str != null && str.length() > 0 
改写成:StringUtils.isNotEmpty(Str)

// 比较字任串大小
boolean StringUtils.equals(String str,String str2)
Boolean StrringUtils. equalsIgnoreCase(String str ,String str2)

//StringUtils.trim使用
StringUtils.trim(str)// 去空格
StringUtils.strip(Str) // 去空白

//StringUtils.replace使用   
StrringUtils.replace(str,str)

org.apache.commons.collections.CollectionUtils使用:
// 判断Collection是否为空
CollectionUtils.isNotEmpty(Collection coll)
CollectionUtils.isEmpty(Collection coll)

实例:
boolean result = list != null && list.size() > 0 
改写成:result = CollectionUtils.isNotEmpty(list);

CollectionUtils.EMPTY_COLLECTION使用:
对于返回类型为Collection的方法,没有理由返回null(《effective Java》中的观点),
可以返回CollectionUtils.EMPTY_COLLECTION。

java.util.Collections 使用:
// 反转list的排列
Collections. reverse(List list) 
// 排序
Collections.sort(List list)
Collections.sort(List,Comparator)
MapUtils 的使用
boolean MapUtils. isNotEmpty(Map map)
boolean MapUtils. isEmpty(Map map)

List 的循环:
多用foreach.
For(Object obj:list)
Map的循环:
使用值循环
for (Entry<String,List<TempProductRpcVo>> entry : colorSubProductMap.entrySet())
不使用key循环
colorSubProductMap.keySet()

巧用equals方法
null != tp.getIsMainProduct() && tp.getIsMainProduct() == true
改写成
Boolean.TRUE.equals(tp.getIsMainProduct())
productCode != ""
改写成
!("".equalsIgnoreCase(productCode))

productCode.equals(“”)
改写成:
“”.equals(productCode) // equals 左边的表达式的值确保不会等于null

Map的使用:
HashMap 存储性能好,不足之处:数据的存储是无序,及数据输出顺序与输入顺序不一样;
LinkedHashMap:保存数据的存入顺序;
TreeMap 支持用key进行排序,构造函数:TreeMap(Comparator<? super K> comparator)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值