Top Description Inners Methods
org.apache.commons.collections4

public Interface MultiSet<E>

extends Collection<E>
Known Direct Implementers
org.apache.commons.collections4.multiset.AbstractMultiSet, org.apache.commons.collections4.multiset.AbstractMultiSetDecorator, org.apache.commons.collections4.multiset.PredicatedMultiSet, org.apache.commons.collections4.multiset.SynchronizedMultiSet
Type Parameters
<E>
the type held in the multiset
Imports
java.util.Collection, .Iterator, .Set

Defines a collection that counts the number of times an object appears in the collection.

Suppose you have a MultiSet that contains {a, a, b, c}. Calling getCount(Object) on a would return 2, while calling uniqueSet() would return {a, b, c}.

Since
4.1

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
MultiSet.Entry<
the element type
E
>

An unmodifiable entry for an element and its occurrence as contained in a MultiSet.

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true always, as the size of the MultiSet is increased in any case
add
(E
the object to add
object
)

Redeclares java.util.Collection.add.

Adds one copy of the specified object to the MultiSet.
public int

Returns:

the number of occurrences of the object in the multiset before this operation; possibly zero
add
(E
the object to add
object
,
int
the number of occurrences to add, may be zero, in which case no change is made to the multiset
occurrences
)

Adds a number of occurrences of the specified object to the MultiSet.

public boolean

Returns:

true if the MultiSet contains all the collection
containsAll
(Collection<?>
the collection to check against
coll
)

Redeclares java.util.Collection.containsAll.

Returns true if the MultiSet contains at least one occurrence for each element contained in the given collection.
public Set<MultiSet.Entry<E>>

Returns:

the Set of MultiSet entries
entrySet
()

Returns a Set of all entries contained in the MultiSet.

public boolean

Returns:

true if equal
equals
(Object
the object to compare to
obj
)

Redeclares java.util.Collection.equals.

Compares this MultiSet to another object.
public int

Returns:

the number of occurrences of the object, zero if not found
getCount
(Object
the object to search for
object
)

Returns the number of occurrences of the given object currently in the MultiSet.

public int

Returns:

the hash code of the MultiSet
hashCode
()

Redeclares java.util.Collection.hashCode.

Gets a hash code for the MultiSet compatible with the definition of equals.
public Iterator<E>

Returns:

iterator over all elements in the MultiSet
iterator
()

Redeclares java.util.Collection.iterator.

Returns an Iterator over the entire set of members, including copies due to cardinality.
public boolean

Returns:

true if this call changed the collection
remove
(Object
the object to remove
object
)

Redeclares java.util.Collection.remove.

Removes one occurrence of the given object from the MultiSet.
public int

Returns:

the number of occurrences of the object in the multiset before the operation; possibly zero
remove
(Object
the object to remove
object
,
int
the number of occurrences to remove, may be zero, in which case no change is made to the multiset
occurrences
)

Removes a number of occurrences of the specified object from the MultiSet.

public boolean

Returns:

true if this call changed the multiset
removeAll
(Collection<?>
the collection of elements to remove
coll
)

Redeclares java.util.Collection.removeAll.

Remove all occurrences of all elements from this MultiSet represented in the given collection.
public boolean

Returns:

true if this call changed the multiset
retainAll
(Collection<?>
the collection of elements to retain
coll
)

Redeclares java.util.Collection.retainAll.

Remove any elements of this MultiSet that are not contained in the given collection.
public int

Returns:

the number of occurrences of the object before this operation, zero if the object was not contained in the multiset
setCount
(E
the object to update
object
,
int
the number of occurrences of the object
count
)

Sets the number of occurrences of the specified object in the MultiSet to the given count.

public int

Returns:

the total size of the multiset
size
()

Redeclares java.util.Collection.size.

Returns the total number of items in the MultiSet.
public Set<E>

Returns:

the Set of unique MultiSet elements
uniqueSet
()

Returns a Set of unique elements in the MultiSet.

Inherited from java.util.Collection:
addAllclearcontainsisEmptyparallelStreamremoveIfspliteratorstreamtoArraytoArraytoArray

Method Detail

addback to summary
public boolean add(E object)

Redeclares java.util.Collection.add.

Adds one copy of the specified object to the MultiSet.

If the object is already in the uniqueSet() then increment its count as reported by getCount(Object). Otherwise add it to the uniqueSet() and report its count as 1.

Parameters
object:E

the object to add

Returns:boolean

true always, as the size of the MultiSet is increased in any case

Annotations
@Override
addback to summary
public int add(E object, int occurrences)

Adds a number of occurrences of the specified object to the MultiSet.

If the object is already in the uniqueSet() then increment its count as reported by getCount(Object). Otherwise add it to the uniqueSet() and report its count as occurrences.

Parameters
object:E

the object to add

occurrences:int

the number of occurrences to add, may be zero, in which case no change is made to the multiset

Returns:int

the number of occurrences of the object in the multiset before this operation; possibly zero

Exceptions
IllegalArgumentException:
if occurrences is negative
containsAllback to summary
public boolean containsAll(Collection<?> coll)

Redeclares java.util.Collection.containsAll.

Returns true if the MultiSet contains at least one occurrence for each element contained in the given collection.

Parameters
coll:Collection<?>

the collection to check against

Returns:boolean

true if the MultiSet contains all the collection

Annotations
@Override
entrySetback to summary
public Set<MultiSet.Entry<E>> entrySet()

Returns a Set of all entries contained in the MultiSet.

The returned set is backed by this multiset, so any change to either is immediately reflected in the other.

Returns:Set<MultiSet.Entry<E>>

the Set of MultiSet entries

equalsback to summary
public boolean equals(Object obj)

Redeclares java.util.Collection.equals.

Compares this MultiSet to another object.

This MultiSet equals another object if it is also a MultiSet that contains the same number of occurrences of the same elements.

Parameters
obj:Object

the object to compare to

Returns:boolean

true if equal

Annotations
@Override
getCountback to summary
public int getCount(Object object)

Returns the number of occurrences of the given object currently in the MultiSet. If the object does not exist in the multiset, return 0.

Parameters
object:Object

the object to search for

Returns:int

the number of occurrences of the object, zero if not found

hashCodeback to summary
public int hashCode()

Redeclares java.util.Collection.hashCode.

Gets a hash code for the MultiSet compatible with the definition of equals. The hash code is defined as the sum total of a hash code for each element. The per element hash code is defined as (e==null ? 0 : e.hashCode()) ^ noOccurances).

Returns:int

the hash code of the MultiSet

Annotations
@Override
iteratorback to summary
public Iterator<E> iterator()

Redeclares java.util.Collection.iterator.

Returns an Iterator over the entire set of members, including copies due to cardinality. This iterator is fail-fast and will not tolerate concurrent modifications.

Returns:Iterator<E>

iterator over all elements in the MultiSet

Annotations
@Override
removeback to summary
public boolean remove(Object object)

Redeclares java.util.Collection.remove.

Removes one occurrence of the given object from the MultiSet.

If the number of occurrences after this operations is reduced to zero, the object will be removed from the uniqueSet().

Parameters
object:Object

the object to remove

Returns:boolean

true if this call changed the collection

Annotations
@Override
removeback to summary
public int remove(Object object, int occurrences)

Removes a number of occurrences of the specified object from the MultiSet.

If the number of occurrences to remove is greater than the actual number of occurrences in the multiset, the object will be removed from the multiset.

Parameters
object:Object

the object to remove

occurrences:int

the number of occurrences to remove, may be zero, in which case no change is made to the multiset

Returns:int

the number of occurrences of the object in the multiset before the operation; possibly zero

Exceptions
IllegalArgumentException:
if occurrences is negative
removeAllback to summary
public boolean removeAll(Collection<?> coll)

Redeclares java.util.Collection.removeAll.

Remove all occurrences of all elements from this MultiSet represented in the given collection.

Parameters
coll:Collection<?>

the collection of elements to remove

Returns:boolean

true if this call changed the multiset

Annotations
@Override
retainAllback to summary
public boolean retainAll(Collection<?> coll)

Redeclares java.util.Collection.retainAll.

Remove any elements of this MultiSet that are not contained in the given collection.

Parameters
coll:Collection<?>

the collection of elements to retain

Returns:boolean

true if this call changed the multiset

Annotations
@Override
setCountback to summary
public int setCount(E object, int count)

Sets the number of occurrences of the specified object in the MultiSet to the given count.

If the provided count is zero, the object will be removed from the uniqueSet().

Parameters
object:E

the object to update

count:int

the number of occurrences of the object

Returns:int

the number of occurrences of the object before this operation, zero if the object was not contained in the multiset

Exceptions
IllegalArgumentException:
if count is negative
sizeback to summary
public int size()

Redeclares java.util.Collection.size.

Returns the total number of items in the MultiSet.

Returns:int

the total size of the multiset

Annotations
@Override
uniqueSetback to summary
public Set<E> uniqueSet()

Returns a Set of unique elements in the MultiSet.

Uniqueness constraints are the same as those in java.util.Set.

The returned set is backed by this multiset, so any change to either is immediately reflected in the other. Only removal operations are supported, in which case all occurrences of the element are removed from the backing multiset.

Returns:Set<E>

the Set of unique MultiSet elements

org.apache.commons.collections4 back to summary

public Interface MultiSet.Entry<E>

Known Direct Implementers
org.apache.commons.collections4.multiset.AbstractMultiSet.AbstractEntry
Type Parameters
<E>
the element type

An unmodifiable entry for an element and its occurrence as contained in a MultiSet.

The MultiSet#entrySet() method returns a view of the multiset whose elements implements this interface.

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true if the specified object is equal to this multiset entry
equals
(Object
object to be compared for equality with this multiset entry
o
)

Compares the specified object with this entry for equality.

public int

Returns:

the number of occurrences of the element
getCount
()

Returns the number of occurrences for the element of this entry.

public E

Returns:

the element corresponding to this entry
getElement
()

Returns the element corresponding to this entry.

public int

Returns:

the hash code value for this multiset entry
hashCode
()

Returns the hash code value for this multiset entry.

Method Detail

equalsback to summary
public boolean equals(Object o)

Compares the specified object with this entry for equality. Returns true if the given object is also a multiset entry and the two entries represent the same element with the same number of occurrences.

More formally, two entries e1 and e2 represent the same mapping if

    (e1.getElement()==null ? e2.getElement()==null
                           : e1.getElement().equals(e2.getElement())) &&
    (e1.getCount()==e2.getCount())
Parameters
o:Object

object to be compared for equality with this multiset entry

Returns:boolean

true if the specified object is equal to this multiset entry

Annotations
@Override
getCountback to summary
public int getCount()

Returns the number of occurrences for the element of this entry.

Returns:int

the number of occurrences of the element

getElementback to summary
public E getElement()

Returns the element corresponding to this entry.

Returns:E

the element corresponding to this entry

hashCodeback to summary
public int hashCode()

Returns the hash code value for this multiset entry.

The hash code of a multiset entry e is defined to be:

     (e==null ? 0 : e.hashCode()) ^ noOccurances)
Returns:int

the hash code value for this multiset entry

Annotations
@Override