平常写代码时,用到的常用工具类

来源:互联网 发布:java项目实战视频 编辑:程序博客网 时间:2024/06/04 18:53

我们在编写代码时,经常会用到一些通用的工具类,有时我们懒得找现有的,上来就自己封装一个.
其实大多数我们在开源项目的工具包中就有,而使用现有的一个好处就是通用化,省了测试用例,大部分程序员都能接受。

废话不多说了,下面来看代码

1. org.springframework.web.util.HtmlUtils

字符串转int

NumberUtils.toInt(fileItem.getString());

// 我们会得到一个int的变量

字符串转long

NumberUtils.toLong(fileItem.getString());


// 我们会得到一个long的变量


org.apache.commons.collections.MapUtils


Map工具
MapUtils.isEmpty(map) ;
MapUtils.getLong(map, key);
// return Long value; 这个很实用,之前都是自己封装的Map

ListUtils, SetUtils 用的不多
org.apache.commons.collections.CollectionUtils

集合的空判断
CollectionUtils.isNotEmpty(list|map|set) ;

//可以判读集合是否为空

org.springframework.cache.utils.RedisUtils

map -> javabean
GoodsConsult goodsConsult=(GoodsConsult) RedisUtils.convertMap(GoodsConsult.class, ((Map) (params.get("bParam"))));

//将一个map对象,转化为实体bean
javaben -> map

RedisUtils.convertBean(bean) ;
//将一个实体bean,转化为map对象
org.apache.commons.beanutils.BeanMap

apache的工具类提供的转化
Map beanMap = new BeanMap(personBean);
//将一个实体bean,转化为map对象,
copy bean 属性
RedisUtils.copyProperties(dest, orig) ;
org.apache.commons.beanutils.BeanUtilsBean
copy bean 属性
BeanUtilsBean.getInstance().copyProperties(dest, orig) ;

2.com.google.common.base.Strings
校验字符产是否为空
Strings.isNullOrEmpty(str);
3.org.apache.commons.lang3.time.DateFormatUtils
日期格式化

DateFormatUtils.format(Calendar.getInstance(), "yyyyMMddHHmmss");

Date date = DateUtils.parseDate("2015-03-01", "yyyy-MM-dd"); //返回指定日期

//返回三十天后的凌晨


System.out.println(DateUtils.addDays(DateUtils.truncate(new Date(), Calendar.DATE), 30));

System.out.println(DateUtils.parseDate(DateFormatUtils.ISO_DATE_FORMAT.format(DateUtils.addDays(new Date(), 30)), DateFormatUtils.ISO_DATE_FORMAT.getPattern()));

4.java.util.Arrays

将字符串 --> List

Arrays.asList(line.split(","));

数组转List集合

String[] array= {"Java", "C++",

"Python", "Linux", "JVM",

"编程思想", "设计模式", "分布式编程"};

List<String> books= Arrays.asList(array);

5.ArrayUtils.indexOf(array,5.67,3);

6.java.text.MessageFormat

字符串拼接

MessageFormat.format("vko_app_vc:{0}", sid);

简单的测试输出也可以用
System.out.format("Method name %s , 第 %s 次调用", "这一个递归方法", "5");


6.1.org.apache.commons.lang3.StringUtils
字符串分隔成->字符数组
StringUtils.split("ds lk fd", " ")

6.2.org.apache.commons.lang3.StringUtils

字符串拼接===以特殊字符拼接成新的字符串

StringUtils.join(array ,",")

8.org.apache.commons.lang3.StringUtils

字符串空判断
StringUtils.isBlank(type) ; //将在字符串含有空白字符的时候,返回true

属于com.apache.commons包

String someWhiteSpace = " \t \n";

StringUtils.isEmpty(someWhiteSpace); // false

StringUtils.isBlank(someWhiteSpace); // true

9.org.apache.commons.lang3.StringUtils

字符串内容比较

StringUtils.equals(str1,str2);
10.com.google.common.collect.ImmutableMap<K, V>

初始化Map对象
Map<String, String> map = ImmutableMap.of("ON","TRUE","OFF","FALSE"); //return ImmutableMap<Object, Object> 与Map不同,少用
11.com.google.common.collect.Lists

初始化一个有数据的List

List<?> list = Lists.newArrayList("one","two","three"); return ArrayList<E> 建议使用;

12.com.google.common.collect.Sets

初始化一个有数据的Set

Sets.newHashSet("one","two","three"); return HashSet<E> 建议使用

13. redis.clients.util.MurmurHash

获取不重复的hashcode

MurmurHash 支持 32或128

HashFunction hashFn = Hashing.murmur3_123();

hashFn.putLong(carId).putString(carName, Charsets.UTF-8).hash();

14.org.apache.commons.io.FilenameUtils

上传文件是获取文件后缀

String uploadFile = file.getOriginalFilename();

String fileSuffix = FilenameUtils.getExtension(uploadFile);

作者:头条号 / 程序员的成长之路
链接:http://toutiao.com/a6319561581000737025/

0 0