本文整理了Java中com.google.common.collect.Multiset.count()
方法的一些代码示例,展示了Multiset.count()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Multiset.count()
方法的具体详情如下:
包路径:com.google.common.collect.Multiset
类名称:Multiset
方法名:count
[英]Returns the number of occurrences of an element in this multiset (the count of the element). Note that for an Object#equals-based multiset, this gives the same result as Collections#frequency(which would presumably perform more poorly).
Note: the utility method Iterables#frequency generalizes this operation; it correctly delegates to this method when dealing with a multiset, but it can also accept any other iterable type.
[中]返回此多重集中某个元素的出现次数(元素的计数)。请注意,对于基于对象#equals的多集,这会给出与集合#frequency相同的结果(这可能会执行得更差)。
注:实用方法Iterables#frequency概括了该操作;在处理multiset时,它正确地委托给这个方法,但它也可以接受任何其他iterable类型。
代码示例来源:origin: google/guava
@Override
public int count(Object element) {
int count1 = multiset1.count(element);
return (count1 == 0) ? 0 : Math.min(count1, multiset2.count(element));
}
代码示例来源:origin: google/guava
@Override
@GuardedBy("ServiceManagerState.this.monitor")
public boolean isSatisfied() {
return states.count(TERMINATED) + states.count(FAILED) == numberOfServices;
}
}
代码示例来源:origin: google/guava
@Override
public int count(@Nullable Object element) {
int count1 = multiset1.count(element);
return (count1 == 0) ? 0 : Math.max(0, count1 - multiset2.count(element));
}
代码示例来源:origin: google/guava
@Override
public int count(@Nullable Object element) {
int count = unfiltered.count(element);
if (count > 0) {
@SuppressWarnings("unchecked") // element is equal to an E
E e = (E) element;
return predicate.apply(e) ? count : 0;
}
return 0;
}
代码示例来源:origin: google/guava
/** An implementation of {@link Multiset#setCount(Object, int, int)}. */
static <E> boolean setCountImpl(Multiset<E> self, E element, int oldCount, int newCount) {
checkNonnegative(oldCount, "oldCount");
checkNonnegative(newCount, "newCount");
if (self.count(element) == oldCount) {
self.setCount(element, newCount);
return true;
} else {
return false;
}
}
代码示例来源:origin: google/guava
public void testCollectionCreate() {
Multiset<Color> ms = EnumMultiset.create(asList(Color.RED, Color.YELLOW, Color.RED));
assertEquals(0, ms.count(Color.BLUE));
assertEquals(1, ms.count(Color.YELLOW));
assertEquals(2, ms.count(Color.RED));
}
代码示例来源:origin: google/guava
@CollectionFeature.Require(SUPPORTS_ADD)
public void testAddOccurrences() {
int originalCount = getMultiset().count(e0());
assertEquals("old count", originalCount, getMultiset().add(e0(), 2));
assertEquals("old count", originalCount + 2, getMultiset().count(e0()));
}
代码示例来源:origin: google/guava
public void testCreateWithSize() {
Multiset<String> multiset = HashMultiset.create(50);
multiset.add("foo", 2);
multiset.add("bar");
assertEquals(3, multiset.size());
assertEquals(2, multiset.count("foo"));
}
代码示例来源:origin: google/guava
@CollectionFeature.Require(SUPPORTS_ADD)
public void testAddSeveralTimes() {
int originalCount = getMultiset().count(e0());
assertEquals(originalCount, getMultiset().add(e0(), 2));
assertTrue(getMultiset().add(e0()));
assertEquals(originalCount + 3, getMultiset().add(e0(), 1));
assertEquals(originalCount + 4, getMultiset().count(e0()));
}
代码示例来源:origin: google/guava
@CollectionFeature.Require(SUPPORTS_ADD)
public void testAddOccurrencesZero() {
int originalCount = getMultiset().count(e0());
assertEquals("old count", originalCount, getMultiset().add(e0(), 0));
expectUnchanged();
}
代码示例来源:origin: google/guava
@CollectionSize.Require(SEVERAL)
public void testCount_3() {
initThreeCopies();
assertEquals("multiset.count(thriceContained) didn't return 3", 3, getMultiset().count(e0()));
}
代码示例来源:origin: google/guava
@CollectionSize.Require(SEVERAL)
@CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE)
@MultisetFeature.Require(ENTRIES_ARE_VIEWS)
public void testEntryReflectsEntrySetIteratorRemove() {
initThreeCopies();
assertEquals(3, getMultiset().count(e0()));
Iterator<Multiset.Entry<E>> entryItr = getMultiset().entrySet().iterator();
Multiset.Entry<E> entry = entryItr.next();
entryItr.remove();
assertEquals(0, entry.getCount());
}
代码示例来源:origin: google/guava
@CollectionFeature.Require(SUPPORTS_ADD)
public void testAddTooMany() {
getMultiset().add(e3(), Integer.MAX_VALUE);
try {
getMultiset().add(e3());
fail();
} catch (IllegalArgumentException expected) {
}
assertEquals(Integer.MAX_VALUE, getMultiset().count(e3()));
assertEquals(Integer.MAX_VALUE, getMultiset().size());
}
代码示例来源:origin: google/guava
public void testCreateWithComparator() {
Multiset<String> multiset = TreeMultiset.create(Collections.reverseOrder());
multiset.add("foo", 2);
multiset.add("bar");
assertEquals(3, multiset.size());
assertEquals(2, multiset.count("foo"));
assertEquals("[foo x 2, bar]", multiset.toString());
}
代码示例来源:origin: google/guava
public void testCreateWithSize() {
Multiset<String> multiset = LinkedHashMultiset.create(50);
multiset.add("foo", 2);
multiset.add("bar");
assertEquals(3, multiset.size());
assertEquals(2, multiset.count("foo"));
assertEquals("[foo x 2, bar]", multiset.toString());
}
代码示例来源:origin: google/guava
@CollectionSize.Require(SEVERAL)
@CollectionFeature.Require(SUPPORTS_REMOVE)
@MultisetFeature.Require(ENTRIES_ARE_VIEWS)
public void testEntryReflectsEntrySetClear() {
initThreeCopies();
assertEquals(3, getMultiset().count(e0()));
Multiset.Entry<E> entry = Iterables.getOnlyElement(getMultiset().entrySet());
assertEquals(3, entry.getCount());
getMultiset().entrySet().clear();
assertEquals(0, entry.getCount());
}
代码示例来源:origin: google/guava
@CollectionSize.Require(SEVERAL)
@CollectionFeature.Require(SUPPORTS_REMOVE)
@MultisetFeature.Require(ENTRIES_ARE_VIEWS)
public void testEntryReflectsElementSetClear() {
initThreeCopies();
assertEquals(3, getMultiset().count(e0()));
Multiset.Entry<E> entry = Iterables.getOnlyElement(getMultiset().entrySet());
assertEquals(3, entry.getCount());
getMultiset().elementSet().clear();
assertEquals(0, entry.getCount());
}
代码示例来源:origin: google/guava
@CollectionFeature.Require(SUPPORTS_REMOVE)
public void testRemoveZeroNoOp() {
int originalCount = getMultiset().count(e0());
assertEquals("old count", originalCount, getMultiset().remove(e0(), 0));
expectUnchanged();
}
代码示例来源:origin: google/guava
public void testCreateFromIterable() {
Multiset<String> multiset = TreeMultiset.create(Arrays.asList("foo", "bar", "foo"));
assertEquals(3, multiset.size());
assertEquals(2, multiset.count("foo"));
assertEquals("[bar, foo x 2]", multiset.toString());
}
代码示例来源:origin: google/guava
@CollectionSize.Require(absent = ZERO)
@CollectionFeature.Require(SUPPORTS_REMOVE)
public void testRemove_occurrences_present() {
assertEquals(
"multiset.remove(present, 2) didn't return the old count",
1,
getMultiset().remove(e0(), 2));
assertFalse(
"multiset contains present after multiset.remove(present, 2)",
getMultiset().contains(e0()));
assertEquals(0, getMultiset().count(e0()));
}
内容来源于网络,如有侵权,请联系作者删除!