`

Guava Collections - Multiset Interface

 
阅读更多

Multiset Interface

Multiset interface extends ‘Set’ to have duplicate elements, and provides various utility methods to deal with the occurrences of such elements in a set.

package org.fool.guava.collections;

import java.util.Set;

import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset;

public class MultisetTest {
	public static void main(String[] args) {
		HashMultiset<String> multiset = HashMultiset.create();
		multiset.add("a");
		multiset.add("b");
		multiset.add("c");
		multiset.add("d");
		multiset.add("a");
		multiset.add("b");
		multiset.add("c");
		multiset.add("b");
		multiset.add("b");
		multiset.add("b");

		// print the occurrence of an element
		System.out.println("Occurrence of 'b' : " + multiset.count("b"));

		// print the total size of the multiset
		System.out.println("Total Size : " + multiset.size());

		// get the distinct elements of the multiset as set
		System.out.println("Set [");
		Set<String> set = multiset.elementSet();
		set.forEach(s -> System.out.println(s));
		System.out.println("]");

		// display all the elements of the multiset
		System.out.println("MultiSet [");
		multiset.forEach(ms -> System.out.println(ms));
		System.out.println("]");

		// display the distinct elements of the multiset with their occurrence
		// count
		System.out.println("MultiSet [");
		for (Multiset.Entry<String> entry : multiset.entrySet()) {
			System.out.println("Element: " + entry.getElement() + ", Occurrence(s): " + entry.getCount());
		}
		System.out.println("]");

		// remove extra occurrences
		multiset.remove("b", 2);
		// print the occurrence of an element
		System.out.println("Occurence of 'b' : " + multiset.count("b"));
	}
}

 output

Occurrence of 'b' : 5
Total Size : 10
Set [
a
b
c
d
]
MultiSet [
a
a
b
b
b
b
b
c
c
d
]
MultiSet [
Element: a, Occurrence(s): 2
Element: b, Occurrence(s): 5
Element: c, Occurrence(s): 2
Element: d, Occurrence(s): 1
]
Occurence of 'b' : 3

 

分享到:
评论

相关推荐

    guava-collections-r03.jar

    guava类似Apache Commons工具集包含了若干被Google的 Java项目广泛依赖 的核心库

    guava-30.0-jre-API文档-中文版.zip

    赠送jar包:guava-30.0-jre.jar; 赠送原API文档:guava-30.0-jre-javadoc.jar; 赠送源代码:guava-30.0-jre-sources.jar; 赠送Maven依赖信息文件:guava-30.0-jre.pom; 包含翻译后的API文档:guava-30.0-jre-...

    guava-27.0.1-jre-API文档-中文版.zip

    赠送jar包:guava-27.0.1-jre.jar; 赠送原API文档:guava-27.0.1-jre-javadoc.jar; 赠送源代码:guava-27.0.1-jre-sources.jar; 赠送Maven依赖信息文件:guava-27.0.1-jre.pom; 包含翻译后的API文档:guava-...

    guava-25.0-jre-API文档-中文版.zip

    赠送jar包:guava-25.0-jre.jar; 赠送原API文档:guava-25.0-jre-javadoc.jar; 赠送源代码:guava-25.0-jre-sources.jar; 赠送Maven依赖信息文件:guava-25.0-jre.pom; 包含翻译后的API文档:guava-25.0-jre-...

    Google-Guava-Collections-使用介绍

    ### Google Guava Collections 使用介绍 #### 一、概述 Google Guava Collections 是 Java Collections Framework 的一个强大且实用的非官方扩展 API。它由 Google 工程师 Kevin Bourrillion 和 Jared Levy 在著名...

    guava-20.0-API文档-中文版.zip

    赠送jar包:guava-20.0.jar; 赠送原API文档:guava-20.0-javadoc.jar; 赠送源代码:guava-20.0-sources.jar; 赠送Maven依赖信息文件:guava-20.0.pom; 包含翻译后的API文档:guava-20.0-javadoc-API文档-中文...

    guava-27.0-jre.jar

    guava-27.0-jre.jar

    guava-11.0.2-API文档-中文版.zip

    赠送jar包:guava-11.0.2.jar; 赠送原API文档:guava-11.0.2-javadoc.jar; 赠送源代码:guava-11.0.2-sources.jar; 赠送Maven依赖信息文件:guava-11.0.2.pom; 包含翻译后的API文档:guava-11.0.2-javadoc-API...

    guava-29.0-jre-API文档-中英对照版.zip

    赠送jar包:guava-29.0-jre.jar; 赠送原API文档:guava-29.0-jre-javadoc.jar; 赠送源代码:guava-29.0-jre-sources.jar; 赠送Maven依赖信息文件:guava-29.0-jre.pom; 包含翻译后的API文档:guava-29.0-jre-...

    guava-18.0(guava-18.0.jar和guava-18.0-sources.jar)

    这个压缩包包含的是Guava库的18.0版本,分为两个部分:`guava-18.0.jar`和`guava-18.0-sources.jar`。 `guava-18.0.jar`是Guava库的编译后的二进制版本,包含了所有Guava类和方法的字节码,开发者可以直接在项目中...

    guava-28.2-jre-API文档-中文版.zip

    赠送jar包:guava-28.2-jre.jar; 赠送原API文档:guava-28.2-jre-javadoc.jar; 赠送源代码:guava-28.2-jre-sources.jar; 赠送Maven依赖信息文件:guava-28.2-jre.pom; 包含翻译后的API文档:guava-28.2-jre-...

    guava-27.1-jre.jar

    guava-27.1-jre

    guava-16.0.1-API文档-中文版.zip

    赠送jar包:guava-16.0.1.jar; 赠送原API文档:guava-16.0.1-javadoc.jar; 赠送源代码:guava-16.0.1-sources.jar; 赠送Maven依赖信息文件:guava-16.0.1.pom; 包含翻译后的API文档:guava-16.0.1-javadoc-API...

    guava-26.0-jre.zip

    1. **集合框架增强**:Guava提供了许多强大的集合类,如Multiset(多集)、Multimap(多映射)、ImmutableList、ImmutableSet和ImmutableMap等。这些集合在可变性、并发性和性能方面都进行了优化,能够更好地满足...

    最新版 guava-30.1-jre.jar

    最新版 guava-30.1-jre.jar

    guava-27.0.1-jre-API文档-中英对照版.zip

    赠送jar包:guava-27.0.1-jre.jar; 赠送原API文档:guava-27.0.1-jre-javadoc.jar; 赠送源代码:guava-27.0.1-jre-sources.jar; 赠送Maven依赖信息文件:guava-27.0.1-jre.pom; 包含翻译后的API文档:guava-...

    guava-20.0-API文档-中英对照版.zip

    赠送jar包:guava-20.0.jar; 赠送原API文档:guava-20.0-javadoc.jar; 赠送源代码:guava-20.0-sources.jar; 赠送Maven依赖信息文件:guava-20.0.pom; 包含翻译后的API文档:guava-20.0-javadoc-API文档-中文...

    google-collections-1.0-rc2.jar

    google-collections-1.0-rc2.jar 的jar包,放心使用。

    guava-28.2-android.jar

    guava-28.2-android.jar

    guava-18.0-API文档-中文版.zip

    赠送jar包:guava-18.0.jar; 赠送原API文档:guava-18.0-javadoc.jar; 赠送源代码:guava-18.0-sources.jar; 包含翻译后的API文档:guava-18.0-javadoc-API文档-中文(简体)版.zip 对应Maven信息:groupId:...

Global site tag (gtag.js) - Google Analytics