@SuppressWarnings("unchecked") @Override public Set<Multiset.Entry<E>> entrySet() { Set<Multiset.Entry<E>> es = entrySet; return (es == null) // Safe because the returned set is made unmodifiable and Entry // itself is readonly ? entrySet = (Set) Collections.unmodifiableSet(delegate.entrySet()) : es; }
@Override Iterator<Entry<E>> entryIterator() { final Iterator<Entry<E>> iterator1 = multiset1.entrySet().iterator(); return new AbstractIterator<Entry<E>>() { @Override protected Entry<E> computeNext() { while (iterator1.hasNext()) { Entry<E> entry1 = iterator1.next(); E element = entry1.getElement(); int count = entry1.getCount() - multiset2.count(element); if (count > 0) { return immutableEntry(element, count); } } return endOfData(); } }; }
@Override Iterator<Entry<E>> entryIterator() { final Iterator<Entry<E>> iterator1 = multiset1.entrySet().iterator(); // TODO(lowasser): consider making the entries live views return new AbstractIterator<Entry<E>>() { @Override protected Entry<E> computeNext() { while (iterator1.hasNext()) { Entry<E> entry1 = iterator1.next(); E element = entry1.getElement(); int count = Math.min(entry1.getCount(), multiset2.count(element)); if (count > 0) { return immutableEntry(element, count); } } return endOfData(); } }; }
@Override public Set<Entry<E>> entrySet() { synchronized (mutex) { if (entrySet == null) { entrySet = typePreservingSet(delegate().entrySet(), mutex); } return entrySet; } }
/** * An implementation of {@link Multiset#iterator}. */ static <E> Iterator<E> iteratorImpl(Multiset<E> multiset) { return new MultisetIteratorImpl<E>(multiset, multiset.entrySet().iterator()); }
@Override public int size() { return multiset().entrySet().size(); } }
@Override Set<Entry<E>> createEntrySet() { return Sets.filter( unfiltered.entrySet(), new Predicate<Entry<E>>() { @Override public boolean apply(Entry<E> entry) { return predicate.apply(entry.getElement()); } }); }
@Override public Iterator<E> iterator() { return new TransformedIterator<Entry<E>, E>(multiset().entrySet().iterator()) { @Override E transform(Entry<E> entry) { return entry.getElement(); } }; }
@Override public Set<Entry<E>> entrySet() { return delegate().entrySet(); }
/** * Stores the contents of a multiset in an output stream, as part of * serialization. It does not support concurrent multisets whose content may * change while the method is running. * * <p>The serialized output consists of the number of distinct elements, the * first element, its count, the second element, its count, and so on. */ static <E> void writeMultiset(Multiset<E> multiset, ObjectOutputStream stream) throws IOException { int entryCount = multiset.entrySet().size(); stream.writeInt(entryCount); for (Multiset.Entry<E> entry : multiset.entrySet()) { stream.writeObject(entry.getElement()); stream.writeInt(entry.getCount()); } }
/** * Returns a copy of {@code multiset} as an {@link ImmutableMultiset} whose iteration order is * highest count first, with ties broken by the iteration order of the original multiset. * * @since 11.0 */ @Beta public static <E> ImmutableMultiset<E> copyHighestCountFirst(Multiset<E> multiset) { Entry<E>[] entries = (Entry<E>[]) multiset.entrySet().toArray(new Entry[0]); Arrays.sort(entries, DecreasingCount.INSTANCE); return ImmutableMultiset.copyFromEntries(Arrays.asList(entries)); }
private static <E> ImmutableMultiset<E> copyFromElements(E... elements) { Multiset<E> multiset = LinkedHashMultiset.create(); Collections.addAll(multiset, elements); return copyFromEntries(multiset.entrySet()); }
/** * An implementation of {@link Multiset#size}. */ static int sizeImpl(Multiset<?> multiset) { long size = 0; for (Entry<?> entry : multiset.entrySet()) { size += entry.getCount(); } return Ints.saturatedCast(size); }
/** * {@inheritDoc} * * <p>Elements that occur multiple times in the multiset will be passed to the {@code Consumer} * correspondingly many times, though not necessarily sequentially. */ @Override default void forEach(Consumer<? super E> action) { checkNotNull(action); entrySet() .forEach( entry -> { E elem = entry.getElement(); int count = entry.getCount(); for (int i = 0; i < count; i++) { action.accept(elem); } }); }
/** * Returns an immutable multiset containing the given elements, in the "grouped iteration order" * described in the class documentation. * * @throws NullPointerException if any of {@code elements} is null */ public static <E> ImmutableMultiset<E> copyOf(Iterator<? extends E> elements) { Multiset<E> multiset = LinkedHashMultiset.create(); Iterators.addAll(multiset, elements); return copyFromEntries(multiset.entrySet()); }
/** * An implementation of {@link Multiset#addAll}. */ static <E> boolean addAllImpl(Multiset<E> self, Collection<? extends E> elements) { if (elements.isEmpty()) { return false; } if (elements instanceof Multiset) { Multiset<? extends E> that = cast(elements); for (Entry<? extends E> entry : that.entrySet()) { self.add(entry.getElement(), entry.getCount()); } } else { Iterators.addAll(self, elements.iterator()); } return true; }
/** * Runs the specified action for each distinct element in this multiset, and the number of * occurrences of that element. For some {@code Multiset} implementations, this may be more * efficient than iterating over the {@link #entrySet()} either explicitly or with {@code * entrySet().forEach(action)}. * * @since 21.0 */ @Beta default void forEachEntry(ObjIntConsumer<? super E> action) { checkNotNull(action); entrySet().forEach(entry -> action.accept(entry.getElement(), entry.getCount())); }
static <E> Spliterator<E> spliteratorImpl(Multiset<E> multiset) { Spliterator<Entry<E>> entrySpliterator = multiset.entrySet().spliterator(); return CollectSpliterators.flatMap( entrySpliterator, entry -> Collections.nCopies(entry.getCount(), entry.getElement()).spliterator(), Spliterator.SIZED | (entrySpliterator.characteristics() & (Spliterator.ORDERED | Spliterator.NONNULL | Spliterator.IMMUTABLE)), multiset.size()); }
/** * Returns {@code true} if {@code subMultiset.count(o) <= * superMultiset.count(o)} for all {@code o}. * * @since 10.0 */ @CanIgnoreReturnValue public static boolean containsOccurrences(Multiset<?> superMultiset, Multiset<?> subMultiset) { checkNotNull(superMultiset); checkNotNull(subMultiset); for (Entry<?> entry : subMultiset.entrySet()) { int superCount = superMultiset.count(entry.getElement()); if (superCount < entry.getCount()) { return false; } } return true; }