参考
一文搞懂guava
官方文档翻译
实现目标
- 了解Lists Sets Maps 等集合类的常用方法
- 了解guava Collections2 和 jdk Collections hutool CollectionUtil
- 了解guava ListenableFuture的 及 Java 原生Future的使用
问题
- 如何获得集合类的迭代器
- 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");