Java工具库自带有很多集合,但是有时候并不能方便的满足一些需求,第三方集合很好的满足了我们的需求,说不多说,下面看看代码
public static void main(String[] args) {
Multimap<String, String> multimap = ArrayListMultimap.create();
multimap.put("水果类", "西瓜");
multimap.put("水果类", "桃子");
multimap.put("水果类", "柚子");
multimap.put("坚果类", "核桃");
multimap.put("坚果类", "松子");
System.out.println(multimap);
multimap.forEach((k, v) -> {
System.out.println("key:" + k);
System.out.println("value:" + v);
});
Set<String> keySet = multimap.keySet();
for(String key : keySet ) {
Collection<String> collection = multimap.get(key);
System.out.println("key:" + key);
System.out.println("value:" + collection);
}
Multiset<String> multiset = LinkedHashMultiset.create();
multiset.add("木星");
multiset.add("木星");
multiset.add("木星");
multiset.add("木星");
System.out.println("数量:"+multiset.count("木星"));
multiset.forEach(e->{
System.out.println(e);
});
}
Table<String, String, String> table = HashBasedTable.create();
table.put("动物", "鱼", "比目鱼");
table.put("动物", "鱼", "大白鲨");
table.put("动物", "鱼", "鮟鱇");
table.put("植物", "树", "白杨树");
table.put("植物", "裸子植物", "百岁兰");
Set<Cell<String, String, String>> cellSet = table.cellSet();
for (Table.Cell<String, String, String> cell : cellSet) {
System.out.println("ColumnKey:" + cell.getColumnKey());
System.out.println("RowKey:" + cell.getRowKey());
System.out.println("Value:" + cell.getValue());
}
MutableIntList list = IntLists.mutable.with(1, 2, 3, 4, 5);
LazyIntIterable asReverseLazy = list.asReversed();
System.out.println("asReverseLazy:" + asReverseLazy.toList());
MutableIntList shuffled = list.toList().shuffleThis();
System.out.println("shuffled:" + shuffled);
MutableIntList shuffledPredictable = list.toList().shuffleThis(new Random(3));
System.out.println("shuffledPredictable:" + shuffledPredictable);
MutableIntList listWith = list.with(6).with(7);
System.out.println("listWith:" + listWith);
MutableIntList listWithAll = list.withAll(IntLists.immutable.with(8, 9, 10));
System.out.println("listWithAll:" + listWithAll);
MutableIntList listWithout = list.without(9).without(10);
System.out.println("listWithout:" + listWithout);
MutableIntList listWithoutAll = list.withoutAll(IntLists.immutable.with(6, 7, 8));
System.out.println("listWithoutAll:" + listWithoutAll);
MutableIntList reverseList = list.reverseThis();
System.out.println("reverseList:" + reverseList);
MutableIntList sortList = list.sortThis();
System.out.println("sortList:" + sortList);
MutableIntList evens = list.select(each -> each % 2 == 0);
System.out.println("evens:" + evens);
MutableIntList odds = list.reject(each -> each % 2 == 0);
System.out.println("odds:" + odds);
MutableIntCollection timesTwo = list.collectInt(each -> each * 2, IntLists.mutable.empty());
System.out.println("timesTwo:" + timesTwo);
MutableList<String> collect = list.collect(String::valueOf);
System.out.println("collect:" + collect);
MutableIntList distinct = IntLists.mutable.with(1, 1, 2, 2, 3, 3, 4, 4, 5, 5).distinct();
System.out.println("distinct:" + distinct);
RichIterable<IntIterable> chunk = list.chunk(2);
System.out.println("chunk:" + chunk);
MutableList<IntIntPair> zipped = list.zipInt(IntInterval.zeroTo(4));
System.out.println("zipped:" + zipped);
ImmutableIntList immutableIntList = list.toImmutable();
System.out.println("immutableIntList:" + immutableIntList);
MutableIntSet set = list.toSet();
System.out.println("set:" + set);
MutableIntBag bag = list.toBag();
System.out.println("bag:" + bag);
MutableIntList sortedList = list.toSortedList();
System.out.println("sortedList:" + sortedList);
int[] ints = list.toArray();
System.out.println("ints:" + Arrays.toString(ints));
int[] sortedInts = list.toSortedArray();
System.out.println("sortedInts:" + Arrays.toString(sortedInts));
String string = list.toString();
System.out.println("string:" + string);
String makeString = list.makeString("/");
System.out.println("makeString:" + makeString);
long sum = list.sum();
System.out.println("sum:" + sum);
double average = list.averageIfEmpty(0.0);
System.out.println("average:" + average);
double median = list.medianIfEmpty(0.0);
System.out.println("median:" + median);
int min = list.minIfEmpty(0);
System.out.println("median:" + min);
int max = list.maxIfEmpty(0);
System.out.println("max:" + max);
IntSummaryStatistics stats = list.summaryStatistics();
System.out.println(stats.getSum());
System.out.println(stats.getMin());
System.out.println(stats.getMax());
MutableIntIntMap map = IntIntMaps.mutable.empty();
map.put(1, 1);
System.out.println("map get:" + map.get(1));
map.put(2, 2);
System.out.println("map get:" + map.get(2));
MutableIntIntMap map1 = map.withKeyValue(3, 3).withKeyValue(4, 4).withKeyValue(5, 5).withKeyValue(6, 7);
System.out.println("map1:" + map1);
MutableIntIntMap map2 = map.withoutKey(4).withoutKey(5);
System.out.println("map2:" + map2);
MutableIntIntMap flipped = map.flipUniqueValues();
System.out.println("flipped:" + flipped);
System.out.println("getIfAbsent:" + map.getIfAbsent(6, 20));
System.out.println("getIfAbsentPut:" + map.getIfAbsentPut(8, 99));
MutableIntIntMap withoutAllKeys = map.withoutAllKeys(IntLists.immutable.with(4, 5, 6, 7));
System.out.println("withoutAllKeys:" + withoutAllKeys);
long mapSum = map.sum();
System.out.println("mapSum:" + mapSum);