Top Constructors Methods
org.eclipse.collections.impl.bag.mutable

public abstract Class AbstractMutableBag<T>

extends AbstractMutableBagIterable<T>
implements MutableBag<T>
Class Inheritance
All Implemented Interfaces
org.eclipse.collections.api.bag.MutableBag, org.eclipse.collections.api.bag.MutableBagIterable, org.eclipse.collections.api.collection.MutableCollection, org.eclipse.collections.api.RichIterable, org.eclipse.collections.api.InternalIterable, java.lang.Iterable, java.util.Collection, org.eclipse.collections.api.bag.Bag, org.eclipse.collections.api.bag.UnsortedBag
Known Direct Subclasses
org.eclipse.collections.impl.bag.mutable.AbstractHashBag
Imports
java.util.Collection, java.util.concurrent.ExecutorService, org.eclipse.collections.api.RichIterable, org.eclipse.collections.api.annotation.Beta, org.eclipse.collections.api.bag.ImmutableBag, .MutableBag, .ParallelUnsortedBag, org.eclipse.collections.api.block.function.Function, .Function2, org.eclipse.collections.api.block.function.primitive.ObjectIntToObjectFunction, org.eclipse.collections.api.block.predicate.Predicate, .Predicate2, org.eclipse.collections.api.block.procedure.Procedure, org.eclipse.collections.api.factory.Bags, .Sets, org.eclipse.collections.api.ordered.OrderedIterable, org.eclipse.collections.api.partition.bag.PartitionMutableBag, org.eclipse.collections.api.set.MutableSet, org.eclipse.collections.api.tuple.Pair, org.eclipse.collections.impl.lazy.parallel.bag.NonParallelUnsortedBag, org.eclipse.collections.impl.partition.bag.PartitionHashBag, org.eclipse.collections.impl.utility.Iterate

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public ParallelUnsortedBag<T>
asParallel(ExecutorService executorService, int batchSize)

public SynchronizedBag<T>
public UnmodifiableBag<T>
public <V> MutableBag<V>
collect(Function<? super T, ? extends V> function)

Implements org.eclipse.collections.api.bag.MutableBag.collect, org.eclipse.collections.api.RichIterable.collect, org.eclipse.collections.api.collection.MutableCollection.collect.

Returns a new collection with the results of applying the specified function on each element of the source collection.

public <V> MutableBag<V>
collectIf(Predicate<? super T> predicate, Function<? super T, ? extends V> function)

Implements org.eclipse.collections.api.bag.MutableBag.collectIf, org.eclipse.collections.api.RichIterable.collectIf, org.eclipse.collections.api.collection.MutableCollection.collectIf.

Returns a new collection with the results of applying the specified function on each element of the source collection, but only for those elements which return true upon evaluation of the predicate.

public <P, V> MutableBag<V>
collectWith(Function2<? super T, ? super P, ? extends V> function, P parameter)

Implements org.eclipse.collections.api.bag.MutableBag.collectWith, org.eclipse.collections.api.RichIterable.collectWith, org.eclipse.collections.api.collection.MutableCollection.collectWith.

Same as collect(Function) with a Function2 and specified parameter which is passed to the block.

public <V> MutableBag<V>
collectWithOccurrences(ObjectIntToObjectFunction<? super T, ? extends V> function)

Implements org.eclipse.collections.api.bag.MutableBag.collectWithOccurrences, org.eclipse.collections.api.bag.MutableBagIterable.collectWithOccurrences, org.eclipse.collections.api.bag.Bag.collectWithOccurrences.

Iterates over the unique elements and their occurrences and collects the results of applying the specified function.

public <V> MutableBag<V>
countBy(Function<? super T, ? extends V> function)

Overrides org.eclipse.collections.impl.bag.mutable.AbstractMutableBagIterable.countBy.

Overrides default org.eclipse.collections.api.bag.MutableBag.countBy, org.eclipse.collections.api.RichIterable.countBy, org.eclipse.collections.api.collection.MutableCollection.countBy.

This method will count the number of occurrences of each value calculated by applying the function to each element of the collection.

public <V> MutableBag<V>
countByEach(Function<? super T, ? extends Iterable<V>> function)

Overrides org.eclipse.collections.impl.bag.mutable.AbstractMutableBagIterable.countByEach.

Overrides default org.eclipse.collections.api.bag.MutableBag.countByEach, org.eclipse.collections.api.RichIterable.countByEach, org.eclipse.collections.api.collection.MutableCollection.countByEach.

This method will count the number of occurrences of each value calculated by applying the function to each element of the collection.

public <V, P> MutableBag<V>
countByWith(Function2<? super T, ? super P, ? extends V> function, P parameter)

Overrides org.eclipse.collections.impl.bag.mutable.AbstractMutableBagIterable.countByWith.

Overrides default org.eclipse.collections.api.bag.MutableBag.countByWith, org.eclipse.collections.api.RichIterable.countByWith, org.eclipse.collections.api.collection.MutableCollection.countByWith.

This method will count the number of occurrences of each value calculated by applying the function to each element of the collection with the specified parameter as the second argument.

public <V> MutableBag<V>
public PartitionMutableBag<T>
public <P> PartitionMutableBag<T>
public MutableBag<T>
reject(Predicate<? super T> predicate)

Implements org.eclipse.collections.api.bag.MutableBag.reject, org.eclipse.collections.api.bag.MutableBagIterable.reject, org.eclipse.collections.api.bag.Bag.reject, org.eclipse.collections.api.RichIterable.reject.

Returns all elements of the source collection that return false when evaluating of the predicate.

public <P> MutableBag<T>
public MutableBag<T>
public <S> MutableBag<S>
public <P> MutableBag<T>
public MutableBag<T>
public ImmutableBag<T>
public <S> MutableBag<Pair<T, S>>
zip(Iterable<S> that)

Implements org.eclipse.collections.api.bag.MutableBag.zip, org.eclipse.collections.api.RichIterable.zip, org.eclipse.collections.api.collection.MutableCollection.zip.

Deprecated in 6.0. Use OrderedIterable#zip(Iterable) instead.

Returns a RichIterable formed from this RichIterable and another RichIterable by combining corresponding elements in pairs.

public MutableSet<Pair<T, Integer>>
Inherited from org.eclipse.collections.impl.bag.mutable.AbstractMutableBagIterable:
addAlladdAllBagaddAllIterableallSatisfyallSatisfyWithanySatisfyanySatisfyWithbottomOccurrenceschunkdetectdetectIfNonedetectOptionaldetectWithdetectWithIfNonedetectWithOptionalgetFirstgetKeysViewgetLastgroupByUniqueKeymaxmaxmaxByminminminBynoneSatisfynoneSatisfyWithremoveAllretainAllretainAllIterableselectAndRejectWithsumByDoublesumByFloatsumByIntsumByLongtopOccurrences

Constructor Detail

AbstractMutableBagback to summary
public AbstractMutableBag()

Method Detail

asParallelback to summary
public ParallelUnsortedBag<T> asParallel(ExecutorService executorService, int batchSize)
Annotations
@Beta
asSynchronizedback to summary
public SynchronizedBag<T> asSynchronized()

Implements org.eclipse.collections.api.bag.MutableBag.asSynchronized, org.eclipse.collections.api.collection.MutableCollection.asSynchronized.

Doc from org.eclipse.collections.api.collection.MutableCollection.asSynchronized.

Returns a synchronized wrapper backed by this collection. This is the equivalent of using Collections.synchronizedCollection(this) only with a return type that supports the full iteration protocols available on MutableCollection. The preferred way of iterating over a synchronized collection is to use the internal iteration methods which are properly synchronized internally.

 MutableCollection synchedCollection = collection.asSynchronized();
    ...
 synchedCollection.forEach(each -> ... );
 synchedCollection.select(each -> ... );
 synchedCollection.collect(each -> ... );
If you want to iterate using an imperative style, you must protect external iterators using a synchronized block. This includes explicit iterators as well as JDK 5 style for loops.

Annotations
@Override
asUnmodifiableback to summary
public UnmodifiableBag<T> asUnmodifiable()

Implements org.eclipse.collections.api.bag.MutableBag.asUnmodifiable, org.eclipse.collections.api.collection.MutableCollection.asUnmodifiable.

Doc from org.eclipse.collections.api.collection.MutableCollection.asUnmodifiable.

Returns an unmodifiable view of this collection. This is the equivalent of using Collections.unmodifiableCollection(this) with a return type that supports the full iteration protocols available on MutableCollection. Methods which would mutate the underlying collection will throw UnsupportedOperationExceptions.

Annotations
@Override
collectback to summary
public <V> MutableBag<V> collect(Function<? super T, ? extends V> function)

Implements org.eclipse.collections.api.bag.MutableBag.collect, org.eclipse.collections.api.RichIterable.collect, org.eclipse.collections.api.collection.MutableCollection.collect.

Doc from org.eclipse.collections.api.RichIterable.collect.

Returns a new collection with the results of applying the specified function on each element of the source collection. This method is also commonly called transform or map.

Example using a Java 8 lambda expression:

RichIterable<String> names =
    people.collect(person -> person.getFirstName() + " " + person.getLastName());
Annotations
@Override
collectIfback to summary
public <V> MutableBag<V> collectIf(Predicate<? super T> predicate, Function<? super T, ? extends V> function)

Implements org.eclipse.collections.api.bag.MutableBag.collectIf, org.eclipse.collections.api.RichIterable.collectIf, org.eclipse.collections.api.collection.MutableCollection.collectIf.

Doc from org.eclipse.collections.api.RichIterable.collectIf.

Returns a new collection with the results of applying the specified function on each element of the source collection, but only for those elements which return true upon evaluation of the predicate. This is the optimized equivalent of calling iterable.select(predicate).collect(function).

Example using a Java 8 lambda and method reference:

RichIterable<String> strings = Lists.mutable.with(1, 2, 3).collectIf(e -> e != null, Object::toString);

Example using Predicates factory:

RichIterable<String> strings = Lists.mutable.with(1, 2, 3).collectIf(Predicates.notNull(), Functions.getToString());
Annotations
@Override
collectWithback to summary
public <P, V> MutableBag<V> collectWith(Function2<? super T, ? super P, ? extends V> function, P parameter)

Implements org.eclipse.collections.api.bag.MutableBag.collectWith, org.eclipse.collections.api.RichIterable.collectWith, org.eclipse.collections.api.collection.MutableCollection.collectWith.

Doc from org.eclipse.collections.api.RichIterable.collectWith.

Same as collect(Function) with a Function2 and specified parameter which is passed to the block.

Example using a Java 8 lambda expression:

RichIterable<Integer> integers =
    Lists.mutable.with(1, 2, 3).collectWith((each, parameter) -> each + parameter, Integer.valueOf(1));
Annotations
@Override
collectWithOccurrencesback to summary
public <V> MutableBag<V> collectWithOccurrences(ObjectIntToObjectFunction<? super T, ? extends V> function)

Implements org.eclipse.collections.api.bag.MutableBag.collectWithOccurrences, org.eclipse.collections.api.bag.MutableBagIterable.collectWithOccurrences, org.eclipse.collections.api.bag.Bag.collectWithOccurrences.

Doc from org.eclipse.collections.api.bag.Bag.collectWithOccurrences.

Iterates over the unique elements and their occurrences and collects the results of applying the specified function.

Annotations
@Override
countByback to summary
public <V> MutableBag<V> countBy(Function<? super T, ? extends V> function)

Overrides org.eclipse.collections.impl.bag.mutable.AbstractMutableBagIterable.countBy.

Overrides default org.eclipse.collections.api.bag.MutableBag.countBy, org.eclipse.collections.api.RichIterable.countBy, org.eclipse.collections.api.collection.MutableCollection.countBy.

Doc from org.eclipse.collections.api.RichIterable.countBy.

This method will count the number of occurrences of each value calculated by applying the function to each element of the collection.

Annotations
@Override
Since
9.0
countByEachback to summary
public <V> MutableBag<V> countByEach(Function<? super T, ? extends Iterable<V>> function)

Overrides org.eclipse.collections.impl.bag.mutable.AbstractMutableBagIterable.countByEach.

Overrides default org.eclipse.collections.api.bag.MutableBag.countByEach, org.eclipse.collections.api.RichIterable.countByEach, org.eclipse.collections.api.collection.MutableCollection.countByEach.

Doc from org.eclipse.collections.api.RichIterable.countByEach.

This method will count the number of occurrences of each value calculated by applying the function to each element of the collection.

Annotations
@Override
Since
10.0.0
countByWithback to summary
public <V, P> MutableBag<V> countByWith(Function2<? super T, ? super P, ? extends V> function, P parameter)

Overrides org.eclipse.collections.impl.bag.mutable.AbstractMutableBagIterable.countByWith.

Overrides default org.eclipse.collections.api.bag.MutableBag.countByWith, org.eclipse.collections.api.RichIterable.countByWith, org.eclipse.collections.api.collection.MutableCollection.countByWith.

Doc from org.eclipse.collections.api.RichIterable.countByWith.

This method will count the number of occurrences of each value calculated by applying the function to each element of the collection with the specified parameter as the second argument.

Annotations
@Override
Since
9.0
flatCollectback to summary
public <V> MutableBag<V> flatCollect(Function<? super T, ? extends Iterable<V>> function)

Implements org.eclipse.collections.api.bag.MutableBag.flatCollect, org.eclipse.collections.api.RichIterable.flatCollect, org.eclipse.collections.api.collection.MutableCollection.flatCollect.

Doc from org.eclipse.collections.api.RichIterable.flatCollect.

flatCollect is a special case of collect(Function). With collect, when the Function returns a collection, the result is a collection of collections. flatCollect outputs a single "flattened" collection instead. This method is commonly called flatMap.

Consider the following example where we have a Person class, and each Person has a list of Address objects. Take the following Function:

Function<Person, List<Address>> addressFunction = Person::getAddresses;
RichIterable<Person> people = ...;
Using collect returns a collection of collections of addresses.
RichIterable<List<Address>> addresses = people.collect(addressFunction);
Using flatCollect returns a single flattened list of addresses.
RichIterable<Address> addresses = people.flatCollect(addressFunction);
Annotations
@Override
partitionback to summary
public PartitionMutableBag<T> partition(Predicate<? super T> predicate)

Implements org.eclipse.collections.api.bag.MutableBag.partition, org.eclipse.collections.api.bag.MutableBagIterable.partition, org.eclipse.collections.api.bag.Bag.partition, org.eclipse.collections.api.RichIterable.partition.

Doc from org.eclipse.collections.api.RichIterable.partition.

Filters a collection into a PartitionedIterable based on the evaluation of the predicate.

Example using a Java 8 lambda expression:

PartitionIterable<Person> newYorkersAndNonNewYorkers =
    people.partition(person -> person.getAddress().getState().getName().equals("New York"));
Annotations
@Override
partitionWithback to summary
public <P> PartitionMutableBag<T> partitionWith(Predicate2<? super T, ? super P> predicate, P parameter)

Implements org.eclipse.collections.api.bag.MutableBag.partitionWith, org.eclipse.collections.api.bag.MutableBagIterable.partitionWith, org.eclipse.collections.api.bag.Bag.partitionWith, org.eclipse.collections.api.RichIterable.partitionWith.

Doc from org.eclipse.collections.api.RichIterable.partitionWith.

Filters a collection into a PartitionIterable based on the evaluation of the predicate.

Example using a Java 8 lambda expression:

PartitionIterable<Person> newYorkersAndNonNewYorkers =
    people.partitionWith((Person person, String state) -> person.getAddress().getState().getName().equals(state), "New York");
Annotations
@Override
rejectback to summary
public MutableBag<T> reject(Predicate<? super T> predicate)

Implements org.eclipse.collections.api.bag.MutableBag.reject, org.eclipse.collections.api.bag.MutableBagIterable.reject, org.eclipse.collections.api.bag.Bag.reject, org.eclipse.collections.api.RichIterable.reject.

Doc from org.eclipse.collections.api.RichIterable.reject.

Returns all elements of the source collection that return false when evaluating of the predicate. This method is also sometimes called filterNot and is the equivalent of calling iterable.select(Predicates.not(predicate)).

Example using a Java 8 lambda expression:

RichIterable<Person> rejected =
    people.reject(person -> person.person.getLastName().equals("Smith"));
Annotations
@Override
rejectWithback to summary
public <P> MutableBag<T> rejectWith(Predicate2<? super T, ? super P> predicate, P parameter)

Implements org.eclipse.collections.api.bag.MutableBag.rejectWith, org.eclipse.collections.api.bag.MutableBagIterable.rejectWith, org.eclipse.collections.api.bag.Bag.rejectWith, org.eclipse.collections.api.RichIterable.rejectWith.

Doc from org.eclipse.collections.api.RichIterable.rejectWith.

Similar to reject(Predicate), except with an evaluation parameter for the second generic argument in Predicate2.

E.g. return a Collection of Person elements where the person has an age greater than or equal to 18 years

Example using a Java 8 lambda expression:

RichIterable<Person> rejected =
    people.rejectWith((Person person, Integer age) -> person.getAge() < age, Integer.valueOf(18));
Annotations
@Override
selectback to summary
public MutableBag<T> select(Predicate<? super T> predicate)

Implements org.eclipse.collections.api.bag.MutableBag.select, org.eclipse.collections.api.bag.MutableBagIterable.select, org.eclipse.collections.api.bag.Bag.select, org.eclipse.collections.api.RichIterable.select.

Doc from org.eclipse.collections.api.RichIterable.select.

Returns all elements of the source collection that return true when evaluating the predicate. This method is also commonly called filter.

Example using a Java 8 lambda expression:

RichIterable<Person> selected =
    people.select(person -> person.getAddress().getCity().equals("London"));
Annotations
@Override
selectInstancesOfback to summary
public <S> MutableBag<S> selectInstancesOf(Class<S> clazz)

Implements org.eclipse.collections.api.bag.MutableBag.selectInstancesOf, org.eclipse.collections.api.bag.MutableBagIterable.selectInstancesOf, org.eclipse.collections.api.bag.Bag.selectInstancesOf, org.eclipse.collections.api.RichIterable.selectInstancesOf.

Doc from org.eclipse.collections.api.RichIterable.selectInstancesOf.

Returns all elements of the source collection that are instances of the Class clazz.

RichIterable<Integer> integers =
    List.mutable.with(new Integer(0), new Long(0L), new Double(0.0)).selectInstancesOf(Integer.class);
Annotations
@Override
selectWithback to summary
public <P> MutableBag<T> selectWith(Predicate2<? super T, ? super P> predicate, P parameter)

Implements org.eclipse.collections.api.bag.MutableBag.selectWith, org.eclipse.collections.api.bag.MutableBagIterable.selectWith, org.eclipse.collections.api.bag.Bag.selectWith, org.eclipse.collections.api.RichIterable.selectWith.

Doc from org.eclipse.collections.api.RichIterable.selectWith.

Similar to select(Predicate), except with an evaluation parameter for the second generic argument in Predicate2.

E.g. return a Collection of Person elements where the person has an age greater than or equal to 18 years

Example using a Java 8 lambda expression:

RichIterable<Person> selected =
    people.selectWith((Person person, Integer age) -> person.getAge()>= age, Integer.valueOf(18));
Annotations
@Override
tapback to summary
public MutableBag<T> tap(Procedure<? super T> procedure)

Implements org.eclipse.collections.api.bag.MutableBag.tap, org.eclipse.collections.api.bag.MutableBagIterable.tap, org.eclipse.collections.api.bag.Bag.tap, org.eclipse.collections.api.RichIterable.tap.

Doc from org.eclipse.collections.api.RichIterable.tap.

Executes the Procedure for each element in the iterable and returns this.

Example using a Java 8 lambda expression:

RichIterable<Person> tapped =
    people.tap(person -> LOGGER.info(person.getName()));
Annotations
@Override
toImmutableback to summary
public ImmutableBag<T> toImmutable()

Implements org.eclipse.collections.api.bag.MutableBag.toImmutable, org.eclipse.collections.api.bag.Bag.toImmutable, org.eclipse.collections.api.collection.MutableCollection.toImmutable.

Doc from org.eclipse.collections.api.collection.MutableCollection.toImmutable.

Converts this MutableCollection to an ImmutableCollection.

Annotations
@Override
zipback to summary
public <S> MutableBag<Pair<T, S>> zip(Iterable<S> that)

Implements org.eclipse.collections.api.bag.MutableBag.zip, org.eclipse.collections.api.RichIterable.zip, org.eclipse.collections.api.collection.MutableCollection.zip.

Doc from org.eclipse.collections.api.RichIterable.zip.

Deprecated

in 6.0. Use OrderedIterable#zip(Iterable) instead.

Returns a RichIterable formed from this RichIterable and another RichIterable by combining corresponding elements in pairs. If one of the two RichIterables is longer than the other, its remaining elements are ignored.

Annotations
@Override
@Deprecated
zipWithIndexback to summary
public MutableSet<Pair<T, Integer>> zipWithIndex()

Implements org.eclipse.collections.api.bag.MutableBag.zipWithIndex, org.eclipse.collections.api.bag.MutableBagIterable.zipWithIndex, org.eclipse.collections.api.bag.Bag.zipWithIndex, org.eclipse.collections.api.RichIterable.zipWithIndex.

Doc from org.eclipse.collections.api.RichIterable.zipWithIndex.

Deprecated

in 6.0. Use OrderedIterable#zipWithIndex() instead.

Zips this RichIterable with its indices.

Annotations
@Override
@Deprecated