guava常用类

常用类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
HashMultiset: 元素存放于 HashMap
LinkedHashMultiset: 元素存放于 LinkedHashMap,即元素的排列顺序由第一次放入的顺序决定
TreeMultiset:元素被排序存放于TreeMap
EnumMultiset: 元素必须是 enum 类型
ImmutableMultiset: 不可修改的 Mutiset

List<String> strings = Arrays.asList("-2.00","-2.00","-2.00","-2.00","-2.00", "22.40", "59.36", "296.00", "6.00", "30.00", "62.00");
Ordering<Object> objectOrdering = Ordering.natural().onResultOf(e -> {
return Floats.tryParse((String) e);
});
Collections.sort(strings, objectOrdering);
Multiset(计数)
Multiset<Object> objects = HashMultiset.create();

Multimap(键==》list)
Multimap<Object, Object> objectObjectHashMultimap = HashMultimap.create();

BiMap(值唯一,可翻转键值)
BiMap<String, Integer> userId = HashBiMap.create();

Table(表格数据)
Table<row, column, val> weightedGraph = HashBasedTable.create();

ClassToInstanceMap(每种类存一个对象)
ClassToInstanceMap<Number> numberDefaults = MutableClassToInstanceMap.create();

RangeSet(存放范围数据)
RangeSet<Integer> rangeSet = TreeRangeSet.create();
rangeSet.add(Range.closed(1, 10)); // {[1, 10]}
rangeSet.add(Range.closedOpen(11, 15)); // disconnected range: {[1, 10], [11, 15)}
rangeSet.add(Range.closedOpen(15, 20)); // connected range; {[1, 10], [11, 20)}
rangeSet.add(Range.openClosed(0, 0)); // empty range; {[1, 10], [11, 20)}
rangeSet.remove(Range.open(5, 10)); // splits [1, 10]; {[1, 5], [10, 10], [11, 20)}

RangeMap(存放范围数据,带键)
RangeMap<Integer, String> rangeMap = TreeRangeMap.create();
rangeMap.put(Range.closed(1, 10), "foo"); // {[1, 10] => "foo"}
rangeMap.put(Range.open(3, 6), "bar"); // {[1, 3] => "foo", (3, 6) => "bar", [6, 10] => "foo"}
rangeMap.put(Range.open(10, 20), "foo"); // {[1, 3] => "foo", (3, 6) => "bar", [6, 10] => "foo", (10, 20) => "foo"}
rangeMap.remove(Range.closed(5, 11)); // {[1, 3] => "foo", (3, 5) => "bar", (11, 20) => "foo"}

创建对象
List<TypeThatsTooLongForItsOwnGood> list = Lists.newArrayList();
Map<KeyType, LongishValueType> map = Maps.newLinkedHashMap();
Set<Type> copySet = Sets.newHashSet(elements);
List<String> theseElements = Lists.newArrayList("alpha", "beta", "gamma");
List<Type> exactly100 = Lists.newArrayListWithCapacity(100);
List<Type> approx100 = Lists.newArrayListWithExpectedSize(100);
Set<Type> approx100Set = Sets.newHashSetWithExpectedSize(100);
SetMultimap<String, String> map= HashMultimap.create();
ListMultimap<String, String> map2 = ArrayListMultimap.create();

工具对象:
Iterables,,FluentIterable,,
Lists,,相当于py的切片Lists.partition(countUp, 2);
Sets,,两两组合Sets.cartesianProduct(Set...);
Set<String> animals = ImmutableSet.of("gerbil", "hamster");
Maps,,两个map对比有什么不一样Maps.difference
Map<String, ?> of = ImmutableMap.of("a", "ff", "b", 2, "c", 3);
Multisets,,按次数排序Multisets.copyHighestCountFirst
Multimaps,,按取值过滤分组Multimaps.index,,翻转分组Multimaps.invertFrom(multimap, TreeMultimap.<String, Integer> create());
Map<String, Integer> map = ImmutableMap.of("a", 1, "b", 1, "c", 2);
SetMultimap<String, Integer> multimap = Multimaps.forMap(map);
// multimap maps ["a" => {1}, "b" => {1}, "c" => {2}]
Multimap<Integer, String> inverse = Multimaps.invertFrom(multimap, HashMultimap.<Integer, String> create());
// inverse maps [1 => {"a", "b"}, 2 => {"c"}]
Tables,,转换xy轴Tables.transpose
Comparators
Objects
Ranges

Joiner
Splitter
CharMatcher
Charsets
CaseFormat

Unsigned无法判断-值
* Sign-independent methods are present in: Bytes, Shorts, Ints, Longs, Floats, Doubles, Chars, Booleans. Not UnsignedInts, UnsignedLongs, SignedBytes, or UnsignedBytes.

** Sign-dependent methods are present in: SignedBytes, UnsignedBytes, Shorts, Ints, Longs, Floats, Doubles, Chars, Booleans, UnsignedInts, UnsignedLongs. Not Bytes.

list切割

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class test{
public static <T> List<Collection> splitList(Collection<T> collection, int maxSize, int splitSize) {
if(CollectionUtils.isEmpty(collection)) return Collections.emptyList();
return Stream.iterate(0, f -> f + 1)
.limit(maxSize)
.parallel()
.map(a -> collection.parallelStream().skip(a * splitSize).limit(splitSize).collect(Collectors.toList()))
.filter(b -> !b.isEmpty())
.collect(Collectors.toList());
}

//google工具:
ArrayList<String> strings1 = Lists.newArrayList("a", "a", "b", "c", "a", "d", "b");
List<List<String>> partition = Lists.partition(strings1, 2);
}