Class FrequencySortedSet<E>
- Type Parameters:
- E- the type of elements in the set.
- All Implemented Interfaces:
- Serializable,- Iterable<E>,- Collection<E>,- Comparator<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:
Defined in the sis-utility module
- 
Constructor SummaryConstructorsConstructorDescriptionCreates 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 SummaryModifier 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 AbstractSetequals, hashCode, removeAllMethods inherited from class AbstractCollectionaddAll, containsAll, retainAll, toStringMethods inherited from class Objectclone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface CollectionparallelStream, removeIf, stream, toArrayMethods inherited from interface Comparatorequals, reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLongMethods inherited from interface SetaddAll, containsAll, equals, hashCode, removeAll, retainAllMethods inherited from interface SortedSetspliterator
- 
Constructor Details- 
FrequencySortedSetpublic FrequencySortedSet()Creates an initially empty set with less frequent elements first.
- 
FrequencySortedSetpublic 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).
 
- 
FrequencySortedSetpublic 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- 
sizepublic int size()Returns the number of elements in this set.- Specified by:
- sizein interface- Collection<E>
- Specified by:
- sizein interface- Set<E>
- Specified by:
- sizein class- AbstractCollection<E>
 
- 
isEmptypublic boolean isEmpty()Returnstrueif this set is empty.- Specified by:
- isEmptyin interface- Collection<E>
- Specified by:
- isEmptyin interface- Set<E>
- Overrides:
- isEmptyin class- AbstractCollection<E>
 
- 
addRepetitively 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 be- null).
- 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- if- occurrenceis negative.
 
- 
addAdds 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 interface- Collection<E>
- Specified by:
- addin interface- Set<E>
- Overrides:
- addin class- AbstractCollection<E>
- Parameters:
- element- the element to add (may be- null).
- Returns:
- trueif this set changed as a result of this operation.
 
- 
containsReturnstrueif this set contains the specified element.- Specified by:
- containsin interface- Collection<E>
- Specified by:
- containsin interface- Set<E>
- Overrides:
- containsin class- AbstractCollection<E>
- Parameters:
- element- the element whose presence in this set is to be tested.
- Returns:
- trueif this set contains the specified element.
 
- 
removeRemoves 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 interface- Collection<E>
- Specified by:
- removein interface- Set<E>
- Overrides:
- removein class- AbstractCollection<E>
- Parameters:
- element- the element to remove.
- Returns:
- trueif this set changed as a result of this operation.
 
- 
clearpublic void clear()Removes all elements from this set.- Specified by:
- clearin interface- Collection<E>
- Specified by:
- clearin interface- Set<E>
- Overrides:
- clearin class- AbstractCollection<E>
 
- 
iteratorReturns an iterator over the elements in this set in frequency order.
- 
headSetReturns a view of the portion of this set whose elements occur with a frequency strictly less thantoElementfrequency.- Specified by:
- headSetin interface- SortedSet<E>
- Parameters:
- toElement- high endpoint (exclusive) of the returned set. May be- null.
- Returns:
- a view of the portion of this set delimited by the given endpoint.
 
- 
tailSetReturns a view of the portion of this set whose elements occur with a frequency equal or greater thanfromElementfrequency.- Specified by:
- tailSetin interface- SortedSet<E>
- Parameters:
- fromElement- low endpoint (inclusive) of the returned set. May be- null.
- Returns:
- a view of the portion of this set delimited by the given endpoint.
 
- 
subSetReturns a view of the portion of this set whose elements occur with a frequency in the range offromElementfrequency inclusive totoElementfrequency exclusive.- Specified by:
- subSetin interface- SortedSet<E>
- Parameters:
- fromElement- low endpoint (inclusive) of the returned set. May be- null.
- toElement- high endpoint (exclusive) of the returned set. May be- null.
- Returns:
- a view of the portion of this set delimited by the given endpoints.
 
- 
firstReturns 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 interface- SortedSet<E>
- Throws:
- NoSuchElementException- if this set is empty.
 
- 
lastReturns 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 interface- SortedSet<E>
- Throws:
- NoSuchElementException- if this set is empty.
 
- 
comparatorReturns 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 interface- SortedSet<E>
 
- 
compareCompares 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 interface- Comparator<E>
 
- 
frequencyReturns 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.
 
- 
frequenciespublic int[] frequencies()Returns the frequency of all elements in this set, in iteration order.- Returns:
- the frequency of all elements in this set.
 
- 
toArrayReturns the content of this set as an array.- Specified by:
- toArrayin interface- Collection<E>
- Specified by:
- toArrayin interface- Set<E>
- Overrides:
- toArrayin class- AbstractCollection<E>
 
- 
toArraypublic <T> T[] toArray(T[] array) Returns the content of this set as an array.- Specified by:
- toArrayin interface- Collection<E>
- Specified by:
- toArrayin interface- Set<E>
- Overrides:
- toArrayin class- AbstractCollection<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.
 
 
-