当前位置: 首页 / 技术干货 / 正文
Java教程之Java程序员常用的工具类库

2020-08-11

Java教程

  Java教程之Java程序员常用的工具类库,Java世界有很多实用的工具类框架,今天介绍3个使用频率最高的框架。有很多实用的工具类并没有全部列出来,只列出了最基础的一部分。

  Apache CommonsApache Commons有很多子项目,常用的项目如下:

图片1

  BeanUtils

  提供了一系列对java bean的操作,读取和设置属性值等。

  @Data

  public class User {

  private String username;

  private String password;

  }

  User user = new User();

  BeanUtils.setProperty(user, "username", "li");

  BeanUtils.getProperty(user, "username");

  mapbean的互相转换:

  // bean->map

  Map<String, String> map = BeanUtils.describe(user);

  // map->bean

  BeanUtils.populate(user, map);

  我们将对象放在缓存中通常用redis中的hash,如下:

  # 设置用户信息

  hset student name test

  hset student age 10

  这种场景下mapbean的互相转换的工具类就特别有用。

  Codec

  常见的编码,解码方法封装:

  // Base64

  Base64.encodeBase64String(byte[] binaryData)

  Base64.decodeBase64(String base64String)

  // MD5

  DigestUtils.md5Hex(String data)

  // URL

  URLCodec.decodeUrl(byte[] bytes);

  URLCodec.encodeUrl(BitSet urlsafe, byte[] bytes);

  Collections

  交并差等操作:

  // 判空

  CollectionUtils.isEmpty(collA);

  // 交集

  CollectionUtils.retainAll(collA, collB);

  // 并集

  CollectionUtils.union(collA, collB);

  // 差集

  CollectionUtils.subtract(collA, collB);

  // 判等

  CollectionUtils.isEqualCollection(collA, collB);

  I/O

  IOUtilsIO操作的封装

  // 拷贝流

  IOUtils.copy(InputStream input, OutputStream output);

  // 从流中读取内容,转为list

  List<String> line = IOUtils.readLines(InputStream input, Charset encoding);

  FileUtils

  对文件操作类的封装

  File file = new File("/show/data.text");

  // 按行读取文件

  List<String> lines = FileUtils.readLines(file, "UTF-8");

  // 将字符串写入文件

  FileUtils.writeStringToFile(file, "test", "UTF-8");

  // 文件复制

  FileUtils.copyFile(srcFile, destFile);

  Lang

  StringUtils 以下断言测试通过

  // isEmpty的实现 cs == null || cs.length() == 0; returntrue

  assertEquals(true, StringUtils.isEmpty(""));

  assertEquals(true, StringUtils.isBlank(null));

  assertEquals(true, StringUtils.isBlank(""));

  // 空格

  assertEquals(true, StringUtils.isBlank(" "));

  // 回车

  assertEquals(true, StringUtils.isBlank(" "));

  PairTriple 当想返回2个或3个值,但这几个值没有相关性,没有必要单独封装一个对象,就可以用到如下数据结构,返回PairTriple对象

  Pair<Integer, Integer> pair = new ImmutablePair<>(1, 2);

  // 1 2

  System.out.println(pair.getLeft() + " " + pair.getRight());

  Triple<Integer, Integer, Integer> triple = new ImmutableTriple<>(1,2,3);

  // 1 2 3

  System.out.println(triple.getLeft() + " " + triple.getMiddle() + " " + triple.getRight());

  Google Guava

  集合的创建

  // 普通集合的创建List list = Lists.newArrayList();Set set = Sets.newHashSet();// 不可变集合的创建ImmutableList list = ImmutableList.of("a", "b", "c");ImmutableSet set = ImmutableSet.of("a", "b");

  不可变集合是线程安全的,并且中途不可改变,因为add等方法是被声明为过期,并且会抛出异常。

  // 普通集合的创建

  List<String> list = Lists.newArrayList();

  Set<String> set = Sets.newHashSet();

  // 不可变集合的创建

  ImmutableList<String> list = ImmutableList.of("a", "b", "c");

  ImmutableSet<String> set = ImmutableSet.of("a", "b");

  各种黑科技集合

  // use java

  Map<String, List<Integer>> map = new HashMap<String, List<Integer>>();

  // use guava

  Multimap<String, Integer> map = ArrayListMultimap.create();

  map.put("key1", 1);

  map.put("key1", 2);

  // [1, 2]

  System.out.println(map.get("key1"));

  2个键映射一个值

  Table<String, String, Integer> table = HashBasedTable.create();

  table.put("a", "a", 1);

  table.put("a", "b", 2);

  // 2

  System.out.println(table.get("a", "b"));

  还有很多其他各种类型的集合,不再介绍。

  stop watch

  查看某段代码的运行时间

  Stopwatch stopwatch = Stopwatch.createStarted();

  // do something

  long second = stopwatch.elapsed(TimeUnit.SECONDS);

  TimeUnit 可以指定时间精度

  Joda Time

  jdk1.8之前,日期操作类常用的只有java.util.Datejava.util.Calendar,但是这2个类的易用性实在太差了,SimpleDateFormat不是线程安全的 。这就逼迫用户去选择第三方的日期操作类,Joda Time就是其中的佼佼者。

  2者的api很相似,如果公司的jdk版本在1.8以上推荐使用jdk1.8新推出的日期类,如果在1.8以下推荐使用Joda Time

好程序员公众号

  • · 剖析行业发展趋势
  • · 汇聚企业项目源码

好程序员开班动态

More+
  • HTML5大前端 <高端班>

    开班时间:2021-04-12(深圳)

    开班盛况

    开班时间:2021-05-17(北京)

    开班盛况
  • 大数据+人工智能 <高端班>

    开班时间:2021-03-22(杭州)

    开班盛况

    开班时间:2021-04-26(北京)

    开班盛况
  • JavaEE分布式开发 <高端班>

    开班时间:2021-05-10(北京)

    开班盛况

    开班时间:2021-02-22(北京)

    开班盛况
  • Python人工智能+数据分析 <高端班>

    开班时间:2021-07-12(北京)

    预约报名

    开班时间:2020-09-21(上海)

    开班盛况
  • 云计算开发 <高端班>

    开班时间:2021-07-12(北京)

    预约报名

    开班时间:2019-07-22(北京)

    开班盛况
IT培训IT培训
在线咨询
IT培训IT培训
试听
IT培训IT培训
入学教程
IT培训IT培训
立即报名
IT培训

Copyright 2011-2023 北京千锋互联科技有限公司 .All Right 京ICP备12003911号-5 京公网安备 11010802035720号