private static <E> ImmutableMultiset<E> copyOfInternal( Multiset<? extends E> multiset) { long size = 0; ImmutableMap.Builder<E, Integer> builder = ImmutableMap.builder(); for (Entry<? extends E> entry : multiset.entrySet()) { int count = entry.getCount(); if (count > 0) { // Since ImmutableMap.Builder throws an NPE if an element is null, no // other null checks are needed. builder.put(entry.getElement(), count); size += count; } } return new ImmutableMultiset<E>( builder.build(), (int) Math.min(size, Integer.MAX_VALUE)); }
/** * Returns a collection, which may contain duplicates, of all keys. The number * of times of key appears in the returned multiset equals the number of * mappings the key has in the multimap. Duplicate keys appear consecutively * in the multiset's iteration order. */ public ImmutableMultiset<K> keys() { ImmutableMultiset<K> result = keys; return (result == null) ? (keys = new ImmutableMultiset<K>(new CountMap<K, V>(this), size)) : result; }
private static <E> ImmutableMultiset<E> copyOfInternal( Multiset<? extends E> multiset) { long size = 0; ImmutableMap.Builder<E, Integer> builder = ImmutableMap.builder(); for (Entry<? extends E> entry : multiset.entrySet()) { int count = entry.getCount(); if (count > 0) { // Since ImmutableMap.Builder throws an NPE if an element is null, no // other null checks are needed. builder.put(entry.getElement(), count); size += count; } } if (size == 0) { return of(); } return new ImmutableMultiset<E>( builder.build(), (int) Math.min(size, Integer.MAX_VALUE)); }
private static <E> ImmutableMultiset<E> copyOfInternal( Multiset<? extends E> multiset) { long size = 0; ImmutableMap.Builder<E, Integer> builder = ImmutableMap.builder(); for (Entry<? extends E> entry : multiset.entrySet()) { int count = entry.getCount(); if (count > 0) { // Since ImmutableMap.Builder throws an NPE if an element is null, no // other null checks are needed. builder.put(entry.getElement(), count); size += count; } } if (size == 0) { return of(); } return new ImmutableMultiset<E>( builder.build(), (int) Math.min(size, Integer.MAX_VALUE)); }
private static <E> ImmutableMultiset<E> copyOfInternal( Multiset<? extends E> multiset) { long size = 0; ImmutableMap.Builder<E, Integer> builder = ImmutableMap.builder(); for (Entry<? extends E> entry : multiset.entrySet()) { int count = entry.getCount(); if (count > 0) { // Since ImmutableMap.Builder throws an NPE if an element is null, no // other null checks are needed. builder.put(entry.getElement(), count); size += count; } } if (size == 0) { return of(); } return new ImmutableMultiset<E>( builder.build(), (int) Math.min(size, Integer.MAX_VALUE)); }
private static <E> ImmutableMultiset<E> copyOfInternal( Multiset<? extends E> multiset) { long size = 0; ImmutableMap.Builder<E, Integer> builder = ImmutableMap.builder(); for (Entry<? extends E> entry : multiset.entrySet()) { int count = entry.getCount(); if (count > 0) { // Since ImmutableMap.Builder throws an NPE if an element is null, no // other null checks are needed. builder.put(entry.getElement(), count); size += count; } } if (size == 0) { return of(); } return new ImmutableMultiset<E>(builder.build(), Ints.saturatedCast(size)); }