/** * Returns a collection of multiset entries representing the counts of the distinct elements, in * sorted order. Does not check for null. */ public static <E> Collection<Multiset.Entry<E>> sortedCounts( Comparator<? super E> comparator, Iterable<E> elements) { if (elements instanceof Multiset) { Multiset<E> multiset = (Multiset<E>) elements; if (hasSameComparator(comparator, elements)) { return multiset.entrySet(); } List<Multiset.Entry<E>> entries = Lists.newArrayList(multiset.entrySet()); Collections.sort( entries, Ordering.from(comparator) .onResultOf( new Function<Multiset.Entry<E>, E>() { @Override public E apply(Entry<E> entry) { return entry.getElement(); } })); return entries; } else if (elements instanceof Set) { // known distinct Collection<E> sortedElements; if (hasSameComparator(comparator, elements)) { sortedElements = (Collection<E>) elements; } else { List<E> list = Lists.newArrayList(elements); Collections.sort(list, comparator); sortedElements = list; } return singletonEntries(sortedElements); } else if (hasSameComparator(comparator, elements)) { E current = null; int currentCount = 0; List<Multiset.Entry<E>> sortedEntries = Lists.newArrayList(); for (E e : elements) { if (currentCount > 0) { if (comparator.compare(current, e) == 0) { currentCount++; } else { sortedEntries.add(Multisets.immutableEntry(current, currentCount)); current = e; currentCount = 1; } } else { current = e; currentCount = 1; } } if (currentCount > 0) { sortedEntries.add(Multisets.immutableEntry(current, currentCount)); } return sortedEntries; } TreeMultiset<E> multiset = TreeMultiset.create(comparator); Iterables.addAll(multiset, elements); return multiset.entrySet(); }
/** * Returns an immutable sorted multiset containing the given elements sorted by the given {@code * Comparator}. This method iterates over {@code elements} at most once. * * <p>Despite the method name, this method attempts to avoid actually copying the data when it is * safe to do so. The exact circumstances under which a copy will or will not be performed are * undocumented and subject to change. * * @throws NullPointerException if {@code comparator} or any of {@code elements} is null */ public static <E> ImmutableSortedMultiset<E> copyOf( Comparator<? super E> comparator, Iterable<? extends E> elements) { if (elements instanceof ImmutableSortedMultiset) { @SuppressWarnings("unchecked") // immutable collections are always safe for covariant casts ImmutableSortedMultiset<E> multiset = (ImmutableSortedMultiset<E>) elements; if (comparator.equals(multiset.comparator())) { if (multiset.isPartialView()) { return copyOfSortedEntries(comparator, multiset.entrySet().asList()); } else { return multiset; } } } elements = Lists.newArrayList(elements); // defensive copy TreeMultiset<E> sortedCopy = TreeMultiset.create(checkNotNull(comparator)); Iterables.addAll(sortedCopy, elements); return copyOfSortedEntries(comparator, sortedCopy.entrySet()); }
/** * Returns a collection of multiset entries representing the counts of the distinct elements, in * sorted order. Does not check for null. */ public static <E> Collection<Multiset.Entry<E>> sortedCounts( Comparator<? super E> comparator, Iterator<E> elements) { TreeMultiset<E> multiset = TreeMultiset.create(comparator); Iterators.addAll(multiset, elements); return multiset.entrySet(); }