复制代码

白话- 那些可爱的Java 工具类

0 commons beanutils

  <dependency>
      <groupId>commons-beanutils</groupId>
      <artifactId>commons-beanutils</artifactId>
      <version>1.9.3</version>
  </dependency>

 User user = new User();
 BeanUtils.setProperty(user,"username","name");
 BeanUtils.setProperty(user,"password","password");
 System.out.println(JSONObject.toJSONString(user));
// map-> bean 
Map<String, String> describe = BeanUtils.describe(user);
User user1 = new User();
BeanUtils.populate(user1,describe);
System.out.println(JSONObject.toJSONString(user1));

1 Collections

List<String> l1 = Lists.list("1", "2", "3");
List<String> l2 = Lists.list("2", "3", "4");
System.out.println(CollectionUtils.isEmpty(l1));
System.out.println(CollectionUtils.retainAll(l1, l2));
System.out.println(CollectionUtils.union(l1, l2));
System.out.println(CollectionUtils.subtract(l1, l2));
System.out.println(CollectionUtils.isEqualCollection(l1, l2));

2 stop watch

  <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
      <version>21.0</version>
  </dependency>

Stopwatch stopwatch = Stopwatch.createStarted();
// 以秒打印从计时开始至现在的所用时间,向下取整
System.out.println(stopwatch.elapsed(TimeUnit.SECONDS)); // 1
posted @ 2020-06-15 14:29  pg633  阅读(137)  评论(0编辑  收藏  举报