Google Guava 快速入门 —— 【集合工具】Multisets 类

Google Guava 快速入门.jpg

Guava 集合工具类 Multisets

提供作用于或生成 Multiset 的静态方法。

一、接口声明

以下是 com.google.common.collect.Multisets 接口的声明:

@GwtCompatiblepublic final class Multisets
    extends Object

二、接口方法

官方文档:https://google.github.io/guava/releases/27.0.1-jre/api/docs/com/google/common/collect/Multisets.html

修饰符和类型方法描述
static booleancontainsOccurrences(Multiset<?> superMultiset, Multiset<?> subMultiset) 
返回 true 如果 subMultiset.count(o) <= superMultiset.count(o),o为元素.
static <E>ImmutableMultiset<E>copyHighestCountFirst(Multiset<E> multiset) 
返回multiset的副本作为Immutable Multiset.
static <E>Multiset<E>difference(Multiset<E> multiset1, Multiset<?> multiset2) 
返回不可修改的差集视图.
static <E>Multiset<E>filter(Multiset<E> unfiltered, Predicate<? super E> predicate) 
返回满足predicate的未过滤元素的视图.
static <E>Multiset.Entry<E>immutableEntry(E e, int n) 
返回具有指定元素和计数的不可变multiset.Entry
static <E>Multiset<E>intersection(Multiset<E> multiset1, Multiset<?> multiset2) 
返回两个多重集的交集的不可修改的视图.
static booleanremoveOccurrences(Multiset<?> multisetToModify, Iterable<?> occurrencesToRemove)
For each occurrence of an element e in occurrencesToRemove, removes one occurrence of e in multisetToModify.
static booleanremoveOccurrences(Multiset<?> multisetToModify, Multiset<?> occurrencesToRemove)
For each occurrence of an element e in occurrencesToRemove, removes one occurrence of e in multisetToModify.
static booleanretainOccurrences(Multiset<?> multisetToModify, Multiset<?> multisetToRetain) 
修改multisetToModify,使其元素e的计数最多为multisetToRetain.count(e).
static <E>Multiset<E>sum(Multiset<? extends E> multiset1, Multiset<? extends E> multiset2) 
返回两个集合之和的不可修改视图.
static <T,E,Mextends Multiset<E>> Collector<T,?,M>toMultiset(Function<? super T,E> elementFunction, ToIntFunction<? super T>countFunction,Supplier<M> multisetSupplier) 
Returns a Collector that accumulates elements into a multiset created via the specified Supplier, whose elements are the result of applying elementFunction to the inputs, with counts equal to the result of applying countFunction to the inputs.
static <E>Multiset<E>union(Multiset<? extends E> multiset1, Multiset<? extends E> multiset2) 
返回两个multisets的并集的不可修改的视图。
static <E>Multiset<E>unmodifiableMultiset(ImmutableMultiset<E> multiset) 
已过时
static <E>Multiset<E>unmodifiableMultiset(Multiset<? extends E> multiset) 
返回指定的 multiset 不可修改视图
static <E>SortedMultiset<E>unmodifiableSortedMultiset(SortedMultiset<E> sortedMultiset) 
返回指定的已排序的 multiset 不可修改视图.

三、测试类

package com.example.guava.collect;

import com.google.common.collect.*;
import org.junit.Test;

public class MultimapsTest {

    @Test
    public void asMap() {
        ListMultimap<String, String> listMultimap = LinkedListMultimap.create();
        listMultimap.put("foo", "A");
        listMultimap.put("foo", "B");
        listMultimap.put("foo", "C");

        // 遍历
        listMultimap.forEach((k, v) -> System.out.println("k:" + k + ", v:" + v));

        System.out.println(listMultimap);
    }

    @Test
    public void containsOccurrences(){
        Multiset<String> set1 = HashMultiset.create();
        set1.add("a", 2);
        set1.add("b");
        set1.add("b");

        Multiset<String> set2 = HashMultiset.create();
        set2.add("a", 5);

        System.out.println(set1.containsAll(set2)); // true

        System.out.println(Multisets.containsOccurrences(set1, set2)); // returns false
        System.out.println(set2.remove("a",2));    // 5
        System.out.println(set2.size());            // 3
        System.out.println(set2.removeAll(set1));   // true
        System.out.println(set2.size());            // 0
        System.out.println(set2.isEmpty());         // true
    }

    @Test
    public void copyHighestCountFirst(){
        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");

        System.out.println(multiset.count("a"));        // 6
        System.out.println(multiset.size());                    // 12

        ImmutableMultiset<String> highestCountFirst = Multisets.copyHighestCountFirst(multiset);

        System.out.println(highestCountFirst.size());           // 12
        System.out.println(highestCountFirst.entrySet());       // [a x 6, b x 5, c]
        System.out.println(highestCountFirst.elementSet());     // [a, b, c]
    }

    @Test
    public void retainOccurrences(){
        Multiset<String> set1 = HashMultiset.create();
        set1.add("a", 2);
        set1.add("b");
        set1.add("b");

        Multiset<String> set2 = HashMultiset.create();
        set2.add("a", 5);

        System.out.println(set1);       // [a x 2, b x 2]
        System.out.println(set2);       // [a x 5]

        Multisets.retainOccurrences(set1, set2);

        System.out.println("\n"+set1);  // [a x 2]
        System.out.println(set2);       // [a x 5]
    }

    @Test
    public void difference_sum_union(){
        Multiset<String> set1 = HashMultiset.create();
        set1.add("a", 3);
        set1.add("b", 2);
        set1.add("c", 1);

        Multiset<String> set2 = HashMultiset.create();
        set2.add("a");
        set2.add("c");
        set2.add("e");

        // 差集
        Multiset<String> differenceSet = Multisets.difference(set1, set2);
        System.out.println(differenceSet);  // [a x 2, b x 2]

        // 合集
        Multiset<String> sumSet = Multisets.sum(set1, set2);
        System.out.println(sumSet);         // [a x 4, b x 2, c x 2, e]

        // 并集
        Multiset<String> unionSet = Multisets.union(set1, set2);
        System.out.println(unionSet);       // [a x 3, b x 2, c, e]
    }

}

四、相关文章



未经允许请勿转载:程序喵 » Google Guava 快速入门 —— 【集合工具】Multisets 类

点  赞 (1) 打  赏
分享到: