使用guava集合,增加集合的功能
来源:互联网 发布:淘宝限制流量怎么办 编辑:程序博客网 时间:2024/06/10 12:20
Guava引入了很多JDK没有的、但我们发现明显有用的新集合类型。Multiset统计一个词在文档中出现了多少次,传统的做法是这样的:(这里举个简单的传统方法,下面不在说明)
- @Test
- public void test20(){
- List<String> words = new ArrayList<String>();
- Map<String, Integer> counts = new HashMap<String, Integer>();
- for (String word : words) {
- Integer count = counts.get(word);
- if (count == null) {
- counts.put(word, 1);
- } else {
- counts.put(word, count + 1);
- }
- }
- }
这种写法很笨拙,也容易出错,并且不支持同时收集多种统计信息,如总词数。我们可以做的更好。Guava提供了一个新集合类型 Multiset,它可以多次添加相等的元素。很好使用例如:- @Test
- public void test10() {
- Multiset<String> multiset1 = HashMultiset.create();
- multiset1.add("a", 2);
- multiset1.add("b");
- multiset1.add("b");
- int count = multiset1.count("b");
- System.out.println(count);
- Multiset<String> multiset2 = HashMultiset.create();
- multiset2.add("a", 5);
-
- multiset1.containsAll(multiset2);
-
- Multisets.containsOccurrences(multiset1, multiset2);
-
- multiset2.removeAll(multiset1);
- multiset2.isEmpty();
- }
下面我用测试用例说明一下guava集合类型的新功能-
- @Test
- public void test8() {
- Map<String, Integer> left = ImmutableMap.of("a", 1, "b", 2, "c", 3, "d", 4);
- Map<String, Integer> right = ImmutableMap.of("a", 2, "b", 2, "c", 3);
- MapDifference<String, Integer> diff = Maps.difference(left, right);
- System.out.println(diff.entriesDiffering());
- System.out.println(diff.entriesInCommon());
- System.out.println(diff.entriesOnlyOnLeft());
- System.out.println(diff.entriesOnlyOnRight());
- }
-
-
- @Test
- public void test7() {
- List<String> list = Lists.newArrayList();
-
- Set<String> wordsWithPrimeLength = ImmutableSet.of("one", "two", "three", "six", "seven", "eight");
- Set<String> primes = ImmutableSet.of("two", "three", "five", "seven");
-
- SetView<String> intersection = Sets.intersection(primes, wordsWithPrimeLength);
-
- ImmutableSet<String> immutableCopy = intersection.immutableCopy();
-
- SetView<String> difference = Sets.difference(primes, wordsWithPrimeLength);
- SetView<String> union = Sets.union(primes, wordsWithPrimeLength);
- System.out.println(union);
- System.out.println(difference);
- System.out.println(immutableCopy);
- Set<String> animals = ImmutableSet.of("gerbil", "hamster");
- Set<String> fruits = ImmutableSet.of("apple", "orange", "banana");
-
- Set<List<String>> product = Sets.cartesianProduct(animals, fruits);
-
-
-
- Set<Set<String>> animalSets = Sets.powerSet(animals);
-
- }
-
- @Test
- public void test6() {
- Ordering<String> order = new Ordering<String>() {
- @Override
- public int compare(String left, String right) {
- return Ints.compare(left.length(), right.length());
- }
- };
- order.compare("a", "b");
- String min = order.min("a", "b1", "c11", "d111");
- System.out.println(min);
- }
- @Test
- public void test11() {
- Multiset<String> multiset = HashMultiset.create();
- multiset.add("a", 3);
- multiset.add("b", 5);
- multiset.add("c", 1);
- multiset.add("a");
- multiset.add("a");
- multiset.add("a");
- multiset.add("a");
- ImmutableMultiset<String> highestCountFirst = Multisets.copyHighestCountFirst(multiset);
- System.out.println(highestCountFirst.elementSet());
-
- }
-
-
- @Test
- public void test12() {
- ImmutableSet<String> digits = ImmutableSet.of("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine");
- Function<String, Integer> lengthFunction = new Function<String, Integer>() {
- public Integer apply(String string) {
- return string.length();
- }
- };
- ImmutableListMultimap<Integer, String> digitsByLength = Multimaps.index(digits, lengthFunction);
- System.out.println(digitsByLength);
-
-
-
-
- }
-
- @Test
- public void test13() {
- ArrayListMultimap<String, Integer> multimap = ArrayListMultimap.create();
- multimap.putAll("b", Ints.asList(2, 4, 6));
- multimap.putAll("a", Ints.asList(4, 2, 1));
- multimap.putAll("c", Ints.asList(2, 5, 3));
- TreeMultimap<Integer, String> create = TreeMultimap.create();
- TreeMultimap<Integer, String> inverse = Multimaps.invertFrom(multimap, create);
- System.out.println(inverse);
-
-
-
-
-
- }
-
- @Test
- public void test14() {
- Map<String, Integer> map = ImmutableMap.of("a", 1, "b", 1, "c", 2);
- SetMultimap<String, Integer> multimap = Multimaps.forMap(map);
-
- System.out.println(multimap);
- HashMultimap<Integer, String> dest = HashMultimap.create();
- Multimap<Integer, String> inverse = Multimaps.invertFrom(multimap, dest);
- System.out.println(inverse);
-
-
-
-
-
-
-
-
-
- }
-
-
- @Test
- public void test15() {
- ArrayListMultimap<String, Integer> multimap = ArrayListMultimap.create();
- multimap.put("a", 1);
- multimap.put("a", 2);
- System.out.println(multimap);
- Map<String, Integer> map = new HashMap<String, Integer>();
- map.put("a", 1);
- map.put("a", 2);
- System.out.println(map);
- Map<String, Integer> tree = new TreeMap<String, Integer>();
- tree.put("a", 1);
- tree.put("a", 2);
- System.out.println(tree);
- Map<String, Integer> table = new Hashtable<String, Integer>();
- table.put("a", 1);
- table.put("a", 2);
- System.out.println(table);
-
-
-
- }