教程列表
- Guava Optional类详解-处理null值
- Guava Preconditions类-检查参数
- Guava Ordering类处理排序功能
- Guava Objects类详解
- Guava Range类-范围处理
- Guava Throwables类-异常处理
- Guava 集合处理-Multiset接口
- Guava集合处理- Bimap接口
- Guava集合工具类-Table接口映射处理
- Guava字符串连接-Joiner类
- Guava字符串分割处理-Spiltter类
- Guava CharMatcher类处理JAVA char类型值
- Guava ASCII字符转换 CaseFormat类
- Guava处理java byte类型工具类-Bytes类
- Guava 处理java short实用工具类-Shorts类
- Guava处理java int实用工具类-Ints类
- Guava Longs类-long的实用工具类
- Guava Floats类-float基本类型的实用工具类
- Guava Doubles类-double基本类型的实用工具类
- Guava Chars类-char类型的实用工具类
- Guava Booleans类-布尔型基本的实用工具类
- Guava IntMath类-提供整型的Math实用方法
- Guava LongMath类-long基础类型的Math实用方法
- Guava Multimap类-映射到多个值
- Guava cache缓存工具类
开源项目
知识点
相关文章
更多最近更新
更多Guava 集合处理-Multiset接口
2019-04-23 23:06|来源: 网路
Multiset接口扩展设置有重复的元素,并提供了各种实用的方法来处理这样的元素在集合中出现。
接口声明
以下是com.google.common.collect.Multiset<E>接口的声明:
@GwtCompatible public interface Multiset<E> extends Collection<E>
接口方法
S.N. | 方法及说明 |
---|---|
1 | boolean add(E element) 添加一个出现的指定元素这个multiset。 |
2 | int add(E element, int occurrences) 增加大量的元素到这个multiset。 |
3 | boolean contains(Object element) 确定此多集是否包含指定的元素。 |
4 | boolean containsAll(Collection<?> elements) 返回true,如果这个多集至少包含一个出现的指定集合中的所有元素。 |
5 | int count(Object element) 返回出现的元素的在该multiset的数目(元素的数量)。 |
6 | Set<E> elementSet() 返回集包含在此多集不同的元素。 |
7 | Set<Multiset.Entry<E>> entrySet() 返回此多集的内容的视图,分组在Multiset.Entry实例中,每一个都提供了多集的一个元素和元素的计数。 |
8 | boolean equals(Object object) 比较指定对象与此multiset是否相等。 |
9 | int hashCode() 返回此multiset的哈希码。 |
10 | Iterator<E> iterator() 返回一个迭代在这个集合中的元素。 |
11 | boolean remove(Object element) 移除此多集multiset的单个出现的指定元素,如果存在。 |
12 | int remove(Object element, int occurrences) 删除了一些出现,从该多集multiset的指定元素。 |
13 | boolean removeAll(Collection<?> c) 删除所有这一切都包含在指定集合(可选操作)在此集合的元素。 |
14 | boolean retainAll(Collection<?> c) 保持那些包含在指定collection(可选操作)在此只集合中的元素。 |
15 | int setCount(E element, int count) 添加或删除,使得该元素达到所期望的计数的元件的必要出现。 |
16 | boolean setCount(E element, int oldCount, int newCount) 有条件设置元素的计数为一个新值,如在setCount(对象,INT)中所述,条件是该元素预期的当前计数。 |
17 | String toString() 返回该对象的字符串表示。 |
方法继承
此接口继承从以下接口方法:
java.util.Collection
Multiset 示例
使用所选择的编辑器创建下面的java程序,比如说 C:/> Guava
GuavaTester.java
import java.util.Iterator; import java.util.Set; import com.google.common.collect.HashMultiset; import com.google.common.collect.Multiset; public class GuavaTester { public static void main(String args[]){ //create a multiset collection Multiset<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 Set<String> set = multiset.elementSet(); //display the elements of the set System.out.println("Set ["); for (String s : set) { System.out.println(s); } System.out.println("]"); //display all the elements of the multiset using iterator Iterator<String> iterator = multiset.iterator(); System.out.println("MultiSet ["); while(iterator.hasNext()){ System.out.println(iterator.next()); } 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")); } }
验证结果
使用javac编译器编译如下类
C:\Guava>javac GuavaTester.java
现在运行GuavaTester看到的结果
C:\Guava>java GuavaTester
看到结果。
Occurence of 'b' : 5 Total Size : 10 Set [ d b c a ] MultiSet [ d b b b b b c c a a ] MultiSet [ Element: d, Occurence(s): 1 Element: b, Occurence(s): 5 Element: c, Occurence(s): 2 Element: a, Occurence(s): 2 ] Occurence of 'b' : 3
相关问答
更多-
Google Guava供应商示例(Google Guava Supplier Example)[2022-09-15]
Supplier接口只是一个无参数函数的抽象,它返回一个值...它是获取对象的一些实例或实例的一种手段 。 既然它是如此普遍,它可以用于许多事情。 Jared解释了Multimaps工厂如何利用它作为创建某种类型值Collection的新实例的工厂。 鉴于接口的简单性,它还允许通过将Supplier的行为包装在另一Supplier ,以某种方式改变其行为,从而对Supplier的行为进行非常有力的装饰。 记忆就是一个例子。 我已经使用Suppliers.memoizeWithExpiration方法作为一种 ... -
循环对我来说似乎很好。 那说,你可以这样做: // implementation is simple enough public static Function
, String> joinFunction(Joiner joiner) { ... } String string = Joiner.on('\n').join( Iterables.transform(matrix, joinFunction(Joiner.on(", ")))); 你在那里失去了一些效率,因 ... -
主要是这样的:Multiset精确地遵循Collection集合,并且通常在其他JDK集合实用程序中扮演更好的角色。 此外,Multiset具有泛型支持,支持entrySet()方法和setCount方法,它们都不是由Bag提供的。 正如您可以清楚地看到的那样,它们的用途是相同的,但我希望Multiset在很多方面都能胜出。 Primarily this: Multiset follows the Collection contract precisely, and generally plays bett ...
-
在Guava中,如何使用单个元素和n次出现创建Multiset(In Guava, how to create a Multiset with a single element and n occurrences)[2023-12-12]
番石榴贡献者在这里。 坚持建设者。 它已经很简单地解决了这个问题,而且只需一行; 要求自己的特殊方法可能不是一个普遍的例子。 Guava contributor here. Stick with the builder. It already addresses the problem quite simply, and in a single line; it's probably not a common enough case to require its own special method. -
Ordering
- o = new Ordering
- () { @Override public int compare(Item left, Item right) { return Ints.compare(left.price, right.price); } }; return o.max(list); 它的效率一样高:它遍历列表的项目,并返回具有最大价格的第一个项目:O(n)。 Ordering
- o = new Orderi ...
- o = new Ordering
-
你能更具体地了解你的用例吗? 我猜你有一系列范围,你正在尝试使用copyOf方法创建ImmutableRangeSet ,它会在重叠范围的情况下抛出IAE。 让我们看看这个测试用例: @Test public void shouldHandleOverlappingRanges() { //given ImmutableList
> ranges = ImmutableList.of( Range.closed(0, 2), ... -
是否有针对Guava MultiSet和Table概念的scala替代品?(Is there a scala replacement for Guava MultiSet and Table concepts?)[2023-05-29]
您可以使用Map[(R, C), V]而不是Table和Map[T, Int]而不是Multiset 。 您还可以向Map[T, Int]添加辅助方法Map[T, Int]如下所示: implicit class Multiset[T](val m: Map[T, Int]) extends AnyVal { def setAdd(e: T, i: Int = 1) = { val cnt = m.getOrElse(e, 0) + i if (cnt <= 0) ... -
根据与Louis Wasserman的讨论,我决定接受他的建议并使用迭代器。 以下解决方案似乎对我有用。 class MySupplier
implements Supplier { private T s; public MySupplier( T s ) { this.s = s; } public T get() { System.out.println( String.format( "Lazy loading '%s'", ... -
番石榴有空吗?(is there isEmpty in Guava? [duplicate])[2022-06-19]
要检查Collection是否为空和空,您必须自己进行空检查。 Guava开发者主张不要将null用于Collection s - 请参阅Guava Wiki上的IdeaGraveyard : 更喜欢返回空集合而不是null。 然后一个简单的isEmpty检查就足够了。 编辑: 请参阅Guava的主要开发人员的回答 。 To check if Collection is both null and empty, you must do null check by yourself. Guava devs a ... -
Iterable
> entriesSortedByCount = Multisets.copyHighestCountFirst(multiset).entrySet(); Iterable > entriesSortedByValue = ImmutableSortedMultiset.copyOf(multiset).entrySet(); 基本上,您只需要entrySet() ,而不是迭代Mu ...