com.gs.collections.api.bag.Bag Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gs-collections-api Show documentation
Show all versions of gs-collections-api Show documentation
GS Collections is a collections framework for Java. It has JDK-compatible List, Set and Map
implementations with a rich API and set of utility classes that work with any JDK compatible Collections,
Arrays, Maps or Strings. The iteration protocol was inspired by the Smalltalk collection framework.
/*
* Copyright 2013 Goldman Sachs.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gs.collections.api.bag;
import java.util.Map;
import com.gs.collections.api.RichIterable;
import com.gs.collections.api.bag.primitive.BooleanBag;
import com.gs.collections.api.bag.primitive.ByteBag;
import com.gs.collections.api.bag.primitive.CharBag;
import com.gs.collections.api.bag.primitive.DoubleBag;
import com.gs.collections.api.bag.primitive.FloatBag;
import com.gs.collections.api.bag.primitive.IntBag;
import com.gs.collections.api.bag.primitive.LongBag;
import com.gs.collections.api.bag.primitive.ShortBag;
import com.gs.collections.api.block.function.Function;
import com.gs.collections.api.block.function.primitive.BooleanFunction;
import com.gs.collections.api.block.function.primitive.ByteFunction;
import com.gs.collections.api.block.function.primitive.CharFunction;
import com.gs.collections.api.block.function.primitive.DoubleFunction;
import com.gs.collections.api.block.function.primitive.FloatFunction;
import com.gs.collections.api.block.function.primitive.IntFunction;
import com.gs.collections.api.block.function.primitive.LongFunction;
import com.gs.collections.api.block.function.primitive.ShortFunction;
import com.gs.collections.api.block.predicate.Predicate;
import com.gs.collections.api.block.predicate.primitive.IntPredicate;
import com.gs.collections.api.block.procedure.primitive.ObjectIntProcedure;
import com.gs.collections.api.map.MutableMap;
import com.gs.collections.api.multimap.bag.BagMultimap;
import com.gs.collections.api.partition.bag.PartitionBag;
import com.gs.collections.api.tuple.Pair;
/**
* A Bag is a Collection whose elements are unordered and may contain duplicate entries. It varies from
* MutableCollection in that it adds a protocol for determining, adding, and removing the number of occurrences for an
* item.
*
* @since 1.0
*/
public interface Bag
extends RichIterable
{
/**
* Two bagsb1 and b2 are equal if m1.toMapOfItemToCount().equals(m2.toMapOfItemToCount()).
*
* @see Map#equals(Object)
*/
@Override
boolean equals(Object object);
/**
* Returns the hash code for this Bag, defined as this.{@link #toMapOfItemToCount()}.hashCode().
*
* @see Map#hashCode()
*/
@Override
int hashCode();
Bag select(Predicate super T> predicate);
Bag reject(Predicate super T> predicate);
PartitionBag partition(Predicate super T> predicate);
Bag selectInstancesOf(Class clazz);
Bag collect(Function super T, ? extends V> function);
BooleanBag collectBoolean(BooleanFunction super T> booleanFunction);
ByteBag collectByte(ByteFunction super T> byteFunction);
CharBag collectChar(CharFunction super T> charFunction);
DoubleBag collectDouble(DoubleFunction super T> doubleFunction);
FloatBag collectFloat(FloatFunction super T> floatFunction);
IntBag collectInt(IntFunction super T> intFunction);
LongBag collectLong(LongFunction super T> longFunction);
ShortBag collectShort(ShortFunction super T> shortFunction);
Bag collectIf(Predicate super T> predicate, Function super T, ? extends V> function);
Bag flatCollect(Function super T, ? extends Iterable> function);
BagMultimap groupBy(Function super T, ? extends V> function);
BagMultimap groupByEach(Function super T, ? extends Iterable> function);
Bag> zip(Iterable that);
Bag> zipWithIndex();
/**
* For each distinct item, with the number of occurrences, execute the specified procedure.
*/
void forEachWithOccurrences(ObjectIntProcedure super T> procedure);
/**
* The occurrences of a distinct item in the bag.
*/
int occurrencesOf(Object item);
/**
* Returns all elements of the bag that have a number of occurrences that satisfy the predicate.
*
* @since 3.0
*/
Bag selectByOccurrences(IntPredicate predicate);
/**
* The size of the Bag when counting only distinct elements.
*/
int sizeDistinct();
/**
* Convert the Bag to an ImmutableBag. If the bag is immutable, it returns itself.
*/
ImmutableBag toImmutable();
/**
* Converts the Bag to a Map of the Item type to its count as an Integer.
*/
MutableMap toMapOfItemToCount();
/**
* Returns a string representation of this bag. The string representation consists of a list of element-count mappings.
* The elements each appear once, in an order consistent with other methods like {@link #forEachWithOccurrences(ObjectIntProcedure)}
* and {@link #iterator()}. The element-count mappings are enclosed in braces ("{}"). Adjacent mappings are
* separated by the characters ", " (comma and space). Each element-count mapping is rendered as the element
* followed by an equals sign ("=") followed by the number of ooccurrences. Elements and are converted to
* strings as by {@link String#valueOf(Object)}.
*
* The string representation is similar to {@link java.util.AbstractMap#toString()}, not {@link RichIterable#toString()}.
*
* @return a string representation of this bag
* @since 3.0
*/
String toStringOfItemToCount();
}