guava 教程收集一些案例慢慢写 google工具类

阅读 58

2022-03-12

参考

一文搞懂guava
官方文档翻译

实现目标

  1. 了解Lists Sets Maps 等集合类的常用方法
  2. 了解guava Collections2 和 jdk Collections hutool CollectionUtil
  3. 了解guava ListenableFuture的 及 Java 原生Future的使用

问题

  1. 如何获得集合类的迭代器
  2. Collections 为什么搞那么多的内部类(为了返回类对象)

集合类

// map 拷贝
@Test
public void test4() throws IllegalAccessException {
    HashMap<String, String> hashMap = Maps.newHashMap();
    hashMap.put("1","A");
    hashMap.put("2","B");
    Map<String, String> res ;
    res = ImmutableMap.copyOf(hashMap);
    System.out.println(JSON.toJSONString(res));
}

集合类的初始化

// 普通Collection的创建
List<String> list = Lists.newArrayList();
Set<String> set = Sets.newHashSet();
Map<String, String> map = Maps.newHashMap();
// 不变Collection的创建
ImmutableList<String> iList = ImmutableList.of("a", "b", "c");
ImmutableSet<String> iSet = ImmutableSet.of("e1", "e2");
ImmutableMap<String, String> iMap = ImmutableMap.of("k1", "v1", "k2", "v2");

精彩评论(0)

0 0 举报