- Type Parameters:
E- the type of elements in the set.
- All Implemented Interfaces:
Serializable,Iterable<E>,Collection<E>,Comparator<E>,SequencedCollection<E>,SequencedSet<E>,Set<E>,SortedSet<E>
An optional boolean argument in the constructor allows the construction of set in reversed order
(most frequently added elements first, less frequently added last). This is similar but not identical
to creating a default FrequencySortedSet and iterating through it in reverse order.
The difference is that elements added the same amount of time will still be traversed in their insertion order.
This class is not thread-safe. Synchronizations (if wanted) are caller responsibility.
- Since:
- 0.8
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreates an initially empty set with less frequent elements first.FrequencySortedSet(boolean reversed) Creates an initially empty set with the default initial capacity.FrequencySortedSet(int initialCapacity, boolean reversed) Creates an initially empty set with the specified initial capacity. -
Method Summary
Modifier and TypeMethodDescriptionbooleanAdds the specified element to this set.booleanRepetitively adds the specified element to this set.voidclear()Removes all elements from this set.final Comparator<E> Returns the comparator used to order the elements in this set.final intCompares the specified elements for frequency.booleanReturnstrueif this set contains the specified element.first()Returns the first element in this set.int[]Returns the frequency of all elements in this set, in iteration order.intReturns the frequency of the specified element in this set.Returns a view of the portion of this set whose elements occur with a frequency strictly less thantoElementfrequency.booleanisEmpty()Returnstrueif this set is empty.iterator()Returns an iterator over the elements in this set in frequency order.last()Returns the last element in this set.booleanRemoves the specified element from this set, no matter how many time it has been added.intsize()Returns the number of elements in this set.Returns a view of the portion of this set whose elements occur with a frequency in the range offromElementfrequency inclusive totoElementfrequency exclusive.Returns a view of the portion of this set whose elements occur with a frequency equal or greater thanfromElementfrequency.Object[]toArray()Returns the content of this set as an array.<T> T[]toArray(T[] array) Returns the content of this set as an array.Methods inherited from class AbstractSet
equals, hashCode, removeAllMethods inherited from class AbstractCollection
addAll, containsAll, retainAll, toStringMethods inherited from class Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface Comparator
equals, reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLongMethods inherited from interface Set
addAll, containsAll, equals, hashCode, removeAll, retainAllMethods inherited from interface SortedSet
addFirst, addLast, getFirst, getLast, removeFirst, removeLast, reversed, spliterator
-
Constructor Details
-
FrequencySortedSet
public FrequencySortedSet()Creates an initially empty set with less frequent elements first. -
FrequencySortedSet
public FrequencySortedSet(boolean reversed) Creates an initially empty set with the default initial capacity.- Parameters:
reversed-trueif the elements should be sorted in reverse order (most frequent element first, less frequent element last).
-
FrequencySortedSet
public FrequencySortedSet(int initialCapacity, boolean reversed) Creates an initially empty set with the specified initial capacity.- Parameters:
initialCapacity- the initial capacity.reversed-trueif the elements should be sorted in reverse order (most frequent element first, less frequent element last).
-
-
Method Details
-
size
public int size()Returns the number of elements in this set.- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein interfaceSet<E>- Specified by:
sizein classAbstractCollection<E>
-
isEmpty
public boolean isEmpty()Returnstrueif this set is empty.- Specified by:
isEmptyin interfaceCollection<E>- Specified by:
isEmptyin interfaceSet<E>- Overrides:
isEmptyin classAbstractCollection<E>
-
add
Repetitively adds the specified element to this set. Returnstrueif this set changed as a result of this operation. Changes in element order are not notified by the returned value.- Parameters:
element- the element to add (may benull).occurrence- the number of time to add the given element. The default value is 1.- Returns:
trueif this set changed as a result of this operation.- Throws:
IllegalArgumentException- ifoccurrenceis negative.
-
add
Adds the specified element to this set. Returnstrueif this set changed as a result of this operation. Changes in element order are not notified by the returned value.The default implementation delegates to
add(element, 1).- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceSet<E>- Overrides:
addin classAbstractCollection<E>- Parameters:
element- the element to add (may benull).- Returns:
trueif this set changed as a result of this operation.
-
contains
Returnstrueif this set contains the specified element.- Specified by:
containsin interfaceCollection<E>- Specified by:
containsin interfaceSet<E>- Overrides:
containsin classAbstractCollection<E>- Parameters:
element- the element whose presence in this set is to be tested.- Returns:
trueif this set contains the specified element.
-
remove
Removes the specified element from this set, no matter how many time it has been added. Returnstrueif this set changed as a result of this operation.- Specified by:
removein interfaceCollection<E>- Specified by:
removein interfaceSet<E>- Overrides:
removein classAbstractCollection<E>- Parameters:
element- the element to remove.- Returns:
trueif this set changed as a result of this operation.
-
clear
public void clear()Removes all elements from this set.- Specified by:
clearin interfaceCollection<E>- Specified by:
clearin interfaceSet<E>- Overrides:
clearin classAbstractCollection<E>
-
iterator
Returns an iterator over the elements in this set in frequency order. -
headSet
Returns a view of the portion of this set whose elements occur with a frequency strictly less thantoElementfrequency.- Specified by:
headSetin interfaceSortedSet<E>- Parameters:
toElement- high endpoint (exclusive) of the returned set. May benull.- Returns:
- a view of the portion of this set delimited by the given endpoint.
-
tailSet
Returns a view of the portion of this set whose elements occur with a frequency equal or greater thanfromElementfrequency.- Specified by:
tailSetin interfaceSortedSet<E>- Parameters:
fromElement- low endpoint (inclusive) of the returned set. May benull.- Returns:
- a view of the portion of this set delimited by the given endpoint.
-
subSet
Returns a view of the portion of this set whose elements occur with a frequency in the range offromElementfrequency inclusive totoElementfrequency exclusive.- Specified by:
subSetin interfaceSortedSet<E>- Parameters:
fromElement- low endpoint (inclusive) of the returned set. May benull.toElement- high endpoint (exclusive) of the returned set. May benull.- Returns:
- a view of the portion of this set delimited by the given endpoints.
-
first
Returns the first element in this set.- For sets created with the default order, this is the less frequently added element. If more than one element were added with the same frequency, this is the first one that has been added to this set at this frequency.
- For sets created with the reverse order, this is the most frequently added element. If more than one element were added with the same frequency, this is the first one that has been added to this set at this frequency.
- Specified by:
firstin interfaceSortedSet<E>- Throws:
NoSuchElementException- if this set is empty.
-
last
Returns the last element in this set.- For sets created with the default order, this is the most frequently added element. If more than one element were added with the same frequency, this is the last one that has been added to this set at this frequency.
- For sets created with the reverse order, this is the less frequently added element. If more than one element were added with the same frequency, this is the last one that has been added to this set at this frequency.
- Specified by:
lastin interfaceSortedSet<E>- Throws:
NoSuchElementException- if this set is empty.
-
comparator
Returns the comparator used to order the elements in this set. For aFrequencySortedSet, the comparator is alwaysthis.This method is final because the
FrequencySortedSetimplementation makes assumptions on the comparator that would not hold if this method were overridden.- Specified by:
comparatorin interfaceSortedSet<E>
-
compare
Compares the specified elements for frequency. ForFrequencySortedSetwith default ordering, this method returns a positive number ifo1has been added more frequently to this set thano2, a negative number ifo1has been added less frequently thano2, and 0 otherwise. ForFrequencySortedSetwith reverse ordering, this is the converse.This method is final because the
FrequencySortedSetimplementation makes assumptions on the comparator that would not hold if this method were overridden.- Specified by:
comparein interfaceComparator<E>
-
frequency
Returns the frequency of the specified element in this set.- Parameters:
element- the element whose frequency is to be obtained.- Returns:
- the frequency of the given element, or
0if it does not occur in this set.
-
frequencies
public int[] frequencies()Returns the frequency of all elements in this set, in iteration order.- Returns:
- the frequency of all elements in this set.
-
toArray
Returns the content of this set as an array.- Specified by:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceSet<E>- Overrides:
toArrayin classAbstractCollection<E>
-
toArray
public <T> T[] toArray(T[] array) Returns the content of this set as an array.- Specified by:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceSet<E>- Overrides:
toArrayin classAbstractCollection<E>- Type Parameters:
T- the type of the array elements.- Parameters:
array- the array where to copy the elements.- Returns:
- the elements in the given array, or in a new array if the given array does not have a sufficient capacity.
-