//初始化
ImmutableList of = ImmutableList.of(1, 2, 3);
LinkedList
//笛卡儿积
List> list5 = Lists.cartesianProduct(Lists.newArrayList("A","B"), Lists.newArrayList("1","2"));
System.out.println(list5);//[[A, 1], [A, 2], [B, 1], [B, 2]]
//元素转换
List transform = Lists.transform(Lists.newArrayList("A","B"), new Function() {
@Nullable
@Override
public String apply(@Nullable String input) {
return input.toLowerCase();
}
});
System.out.println(transform);//[a, b]
//字符串转化为字符,返回ImmutableList,Immutable是不可变对象
ImmutableList list6 = Lists.charactersOf("mnbvcxz");
System.out.println(list6);//[m, n, b, v, c, x, z]
//reversed
List reverse = Lists.reverse(list6);
System.out.println(reverse);//[z, x, c, v, b, n, m]
//按个数分组
List> partition = Lists.partition(reverse, 3);
System.out.println(partition);//[[z, x, c], [v, b, n], [m]]
上述方法,其实实际并未将元素改变,是在正在需要拿元素的时候,才将相关元素按照指定变化。这是一个很重要思想,避免了一开始就遍历元素一个个将元素变化
/**
* jdk 排序
*
* 但是存在一个问题,不能为null,否则将抛出异常
*/
List list = Arrays.asList(1, 3, 2, 4,null);
System.out.println(list);
Collections.sort(list);
System.out.println(list);
上述代码会抛错,因为存在null无法排序,这时需要传递一个Comparator进去、
guava为我们提供了Ordering类
List list = Arrays.asList(1, 3, 2, 4,null);
System.out.println(list);
//将null放到第一个
Collections.sort(list, Ordering.natural().nullsFirst());
System.out.println(list);
//判断list是否是有序
System.out.println(Ordering.natural().nullsFirst().isOrdered(list));
//倒序
//Ordering.natural().reverse().nullsFirst()
HashSet set = Sets.newHashSet(1, 2, 3);
HashSet set1 = Sets.newHashSet(Lists.newArrayList(1, 1, 2, 3, 4));
//子集,size表示每一个子集的个数
Set> set2 = Sets.combinations(Sets.newHashSet(1, 2, 3), 2);
set2.forEach(System.out::print);//[1, 2][1, 3][2, 3]
//差集,交集,并集
//map构建
HashMap
/**
* LinkedListMultimap
*
* 非线程安全
* 可以存储相同key
* get方法返回的是list
*
*
* 线程安全使用:
* Multimap multimap= Multimaps.synchronizedMultimap(LinkedListMultimap.create());
*
*
*
*/
LinkedListMultimap multimap = LinkedListMultimap.create();
multimap.put("1", "2");
multimap.put("1", "2");
multimap.put("1", "3");
multimap.put("1", "4");
System.out.println(multimap);//{1=[2, 2, 3, 4]}
/**
* HashMultimap
*
* 非线程安全
* 可以存储相同key
* 相同key value覆盖
* get方法返回的是set
*
*
* 线程安全使用:
* Multimap hashMultimap= Multimaps.synchronizedMultimap(HashMultimap.create());
*
*/
HashMultimap hashMultimap = HashMultimap.create();
hashMultimap.put("1", "2");
hashMultimap.put("1", "2");
hashMultimap.put("1", "3");
hashMultimap.put("1", "4");
System.out.println(hashMultimap);//{1=[4, 2, 3]}
/**
* 非线程安全
* 不能存在相同key,也不能存在相同value
*
* 线程安全使用: Maps.synchronizedBiMap(biMap)
*/
//相同key覆盖,相同value抛错
HashBiMap biMap = HashBiMap.create();
biMap.put("11","2");
biMap.put("1","3");
System.out.println(biMap);//{11=2, 1=3}
try {
biMap.put("2","3");
} catch (Exception e) {
e.printStackTrace();//java.lang.IllegalArgumentException: value already present: 3
}
//强行加元素,若value和之前的相同,则覆盖之前的entry
biMap.forcePut("3","3");
System.out.println(biMap);//{11=2, 3=3}
//[]
Range closed = Range.closed(1, 5);
//()
Range open = Range.open(1, 5);
//(]
Range openClosed = Range.openClosed(1, 5);
//[)
Range closedOpen = Range.closedOpen(1, 5);
TreeMap treeMap = Maps.newTreeMap();
treeMap.put(1,"111");
treeMap.put(3,"333");
treeMap.put(2,"222");
treeMap.put(4,"444");
System.out.println(treeMap);//{1=111, 2=222, 3=333, 4=444}
NavigableMap subMap = Maps.subMap(treeMap, Range.closed(1, 3));
System.out.println(subMap);//{1=111, 2=222, 3=333}
TreeRangeMap rangeMap = TreeRangeMap.create();
rangeMap.put(Range.closed(90,100),"A");
rangeMap.put(Range.closed(60,89),"B");
System.out.println(rangeMap);//[[60..89]=B, [90..100]=A]
System.out.println(rangeMap.get(99));//A
//Map>
HashBasedTable table = HashBasedTable.create();
table.put(1,2,3);
System.out.println(table);
table.put(1,2,4);
System.out.println(table);
Object o = table.get(1, 2);
Map row = table.row(1);
Map column = table.column(2);
guava的collection实在是太多了,这里介绍一部分用法。后续学习数据结构把实现原理及用到的算法详细介绍一遍,以及整个的设计的设计模式和UML图,待续。。。。。