com.google.common.collect.Multiset.elementSet()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(6.8k)|赞(0)|评价(0)|浏览(141)

本文整理了Java中com.google.common.collect.Multiset.elementSet()方法的一些代码示例,展示了Multiset.elementSet()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Multiset.elementSet()方法的具体详情如下:
包路径:com.google.common.collect.Multiset
类名称:Multiset
方法名:elementSet

Multiset.elementSet介绍

[英]Returns the set of distinct elements contained in this multiset. The element set is backed by the same data as the multiset, so any change to either is immediately reflected in the other. The order of the elements in the element set is unspecified.

If the element set supports any removal operations, these necessarily cause all occurrences of the removed element(s) to be removed from the multiset. Implementations are not expected to support the add operations, although this is possible.

A common use for the element set is to find the number of distinct elements in the multiset: elementSet().size().
[中]返回此多重集中包含的不同元素集。元素集由与多元素集相同的数据支持,因此对其中一个元素集的任何更改都会立即反映在另一个元素集中。未指定元素集中元素的顺序。
如果图元集支持任何删除操作,则这些操作必然会导致删除的图元的所有引用都从multiset中删除。虽然这是可能的,但实现不支持添加操作。
元素集的一个常见用途是在multiset:elementSet()中查找不同元素的数量。大小()。

代码示例

代码示例来源:origin: google/guava

/** An implementation of {@link Multiset#removeAll}. */
static boolean removeAllImpl(Multiset<?> self, Collection<?> elementsToRemove) {
 Collection<?> collection =
   (elementsToRemove instanceof Multiset)
     ? ((Multiset<?>) elementsToRemove).elementSet()
     : elementsToRemove;
 return self.elementSet().removeAll(collection);
}

代码示例来源:origin: google/guava

/** An implementation of {@link Multiset#retainAll}. */
static boolean retainAllImpl(Multiset<?> self, Collection<?> elementsToRetain) {
 checkNotNull(elementsToRetain);
 Collection<?> collection =
   (elementsToRetain instanceof Multiset)
     ? ((Multiset<?>) elementsToRetain).elementSet()
     : elementsToRetain;
 return self.elementSet().retainAll(collection);
}

代码示例来源:origin: google/guava

/**
 * Returns the expected number of distinct elements given the specified elements. The number of
 * distinct elements is only computed if {@code elements} is an instance of {@code Multiset};
 * otherwise the default value of 11 is returned.
 */
static int inferDistinctElements(Iterable<?> elements) {
 if (elements instanceof Multiset) {
  return ((Multiset<?>) elements).elementSet().size();
 }
 return 11; // initial capacity will be rounded up to 16
}

代码示例来源:origin: google/guava

@Override
public Set<E> create(Object... elements) {
 Object[] duplicated = new Object[elements.length * 2];
 for (int i = 0; i < elements.length; i++) {
  duplicated[i] = elements[i];
  duplicated[i + elements.length] = elements[i];
 }
 return ((Multiset<E>) gen.create(duplicated)).elementSet();
}

代码示例来源:origin: google/guava

@Override
public Set<E> elementSet() {
 synchronized (mutex) {
  if (elementSet == null) {
   elementSet = typePreservingSet(delegate().elementSet(), mutex);
  }
  return elementSet;
 }
}

代码示例来源:origin: google/guava

@Override
public Set<E> elementSet() {
 return delegate().elementSet();
}

代码示例来源:origin: google/guava

@Override
public Set<N> successors() {
 return Collections.unmodifiableSet(successorsMultiset().elementSet());
}

代码示例来源:origin: google/guava

@CollectionFeature.Require(SUPPORTS_REMOVE)
public void testRemove_occurrences_absent() {
 int distinct = getMultiset().elementSet().size();
 assertEquals("multiset.remove(absent, 0) didn't return 0", 0, getMultiset().remove(e3(), 2));
 assertEquals(distinct, getMultiset().elementSet().size());
}

代码示例来源:origin: google/guava

@CollectionFeature.Require(SUPPORTS_REMOVE)
public void testElementSetClear() {
 getMultiset().elementSet().clear();
 assertEmpty(getMultiset());
}

代码示例来源:origin: google/guava

@CollectionSize.Require(absent = ZERO)
public void testElementSet_contains() {
 assertTrue(
   "multiset.elementSet().contains(present) returned false",
   getMultiset().elementSet().contains(e0()));
}

代码示例来源:origin: google/guava

@CollectionFeature.Require(SUPPORTS_REMOVE)
public void testElementSetRemoveAbsent() {
 Set<E> elementSet = getMultiset().elementSet();
 assertFalse(elementSet.remove(e3()));
 expectUnchanged();
}

代码示例来源:origin: google/guava

@CollectionFeature.Require(SUPPORTS_ADD)
public void testElementSetReflectsAddAbsent() {
 Set<E> elementSet = getMultiset().elementSet();
 assertFalse(elementSet.contains(e3()));
 getMultiset().add(e3(), 4);
 assertTrue(elementSet.contains(e3()));
}

代码示例来源:origin: google/guava

@CollectionFeature.Require(SERIALIZABLE_INCLUDING_VIEWS)
 public void testElementSetSerialization() {
  Set<E> expected = getMultiset().elementSet();
  assertEquals(expected, SerializableTester.reserialize(expected));
 }
}

代码示例来源:origin: google/guava

@CollectionSize.Require(absent = ZERO)
@CollectionFeature.Require(SUPPORTS_REMOVE)
public void testElementSetReflectsRemove() {
 Set<E> elementSet = getMultiset().elementSet();
 assertTrue(elementSet.contains(e0()));
 getMultiset().removeAll(Collections.singleton(e0()));
 assertFalse(elementSet.contains(e0()));
}

代码示例来源:origin: google/guava

private static long worstCaseQueryOperations(Multiset<?> multiset, CallsCounter counter) {
  long worstCalls = 0;
  for (Object k : multiset.elementSet()) {
   counter.zero();
   int unused = multiset.count(k);
   worstCalls = Math.max(worstCalls, counter.total());
  }
  return worstCalls;
 }
}

代码示例来源:origin: google/guava

public void testSerialization_elementSet() {
 Multiset<String> c = ImmutableSortedMultiset.of("a", "b", "a");
 Collection<String> copy = SerializableTester.reserializeAndAssert(c.elementSet());
 assertThat(copy).containsExactly("a", "b").inOrder();
}

代码示例来源: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

@GwtIncompatible // SerializableTester
public void testSerialization_elementSet() {
 Multiset<String> c = ImmutableMultiset.of("a", "b", "a");
 Collection<String> copy = LenientSerializableTester.reserializeAndAssertLenient(c.elementSet());
 assertThat(copy).containsExactly("a", "b").inOrder();
}

代码示例来源:origin: google/guava

@CollectionSize.Require(SEVERAL)
@CollectionFeature.Require(SUPPORTS_REMOVE)
public void testElementSetRemoveDuplicatePropagatesToMultiset() {
 initThreeCopies();
 int size = getNumElements();
 int expectedSize = size - getMultiset().count(e0());
 Set<E> elementSet = getMultiset().elementSet();
 assertTrue(elementSet.remove(e0()));
 assertEmpty(getMultiset());
 assertEquals(expectedSize, getMultiset().size());
}

代码示例来源:origin: google/guava

@CollectionSize.Require(absent = ZERO)
@CollectionFeature.Require(SUPPORTS_REMOVE)
public void testElementSetRemovePropagatesToMultiset() {
 Set<E> elementSet = getMultiset().elementSet();
 int size = getNumElements();
 int expectedSize = size - getMultiset().count(e0());
 assertTrue(elementSet.remove(e0()));
 assertFalse(getMultiset().contains(e0()));
 assertEquals(expectedSize, getMultiset().size());
}

相关文章