E
- the type of elements in the set.public class FrequencySortedSet<E> extends AbstractSet<E> implements SortedSet<E>, Comparator<E>, Serializable
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.
Defined in the sis-utility
module
Constructor and Description |
---|
FrequencySortedSet()
Creates 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.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(E element)
Adds the specified element to this set.
|
boolean |
add(E element,
int occurrence)
Repetitively adds the specified element to this set.
|
void |
clear()
Removes all elements from this set.
|
Comparator<E> |
comparator()
Returns the comparator used to order the elements in this set.
|
int |
compare(E o1,
E o2)
Compares the specified elements for frequency.
|
boolean |
contains(Object element)
Returns
true if this set contains the specified element. |
E |
first()
Returns the first element in this set.
|
int[] |
frequencies()
Returns the frequency of all elements in this set, in iteration order.
|
int |
frequency(E element)
Returns the frequency of the specified element in this set.
|
SortedSet<E> |
headSet(E toElement)
Returns a view of the portion of this set whose elements occur with a frequency strictly less than
toElement frequency. |
boolean |
isEmpty()
Returns
true if this set is empty. |
Iterator<E> |
iterator()
Returns an iterator over the elements in this set in frequency order.
|
E |
last()
Returns the last element in this set.
|
boolean |
remove(Object element)
Removes the specified element from this set, no matter how many time it has been added.
|
int |
size()
Returns the number of elements in this set.
|
SortedSet<E> |
subSet(E fromElement,
E toElement)
Returns a view of the portion of this set whose elements occur with a frequency in the range of
fromElement frequency inclusive to toElement frequency exclusive. |
SortedSet<E> |
tailSet(E fromElement)
Returns a view of the portion of this set whose elements occur with a frequency equal or greater than
fromElement frequency. |
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.
|
equals, hashCode, removeAll
addAll, containsAll, retainAll, toString
clone, equals, finalize, getClass, notify, notifyAll, wait, wait, wait
spliterator
addAll, containsAll, equals, hashCode, removeAll, retainAll
parallelStream, removeIf, stream
comparing, comparing, comparingDouble, comparingInt, comparingLong, equals, naturalOrder, nullsFirst, nullsLast, reversed, reverseOrder, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
public FrequencySortedSet()
public FrequencySortedSet(boolean reversed)
reversed
- true
if the elements should be sorted in reverse order
(most frequent element first, less frequent element last).public FrequencySortedSet(int initialCapacity, boolean reversed)
initialCapacity
- the initial capacity.reversed
- true
if the elements should be sorted in reverse order
(most frequent element first, less frequent element last).public int size()
size
in interface Collection<E>
size
in interface Set<E>
size
in class AbstractCollection<E>
public boolean isEmpty()
true
if this set is empty.isEmpty
in interface Collection<E>
isEmpty
in interface Set<E>
isEmpty
in class AbstractCollection<E>
public boolean add(E element, int occurrence) throws IllegalArgumentException
true
if this set changed
as a result of this operation. Changes in element order are not notified by the returned value.element
- the element to add (may be null
).occurrence
- the number of time to add the given element. The default value is 1.true
if this set changed as a result of this operation.IllegalArgumentException
- if occurrence
is negative.public boolean add(E element)
true
if 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)
.
add
in interface Collection<E>
add
in interface Set<E>
add
in class AbstractCollection<E>
element
- the element to add (may be null
).true
if this set changed as a result of this operation.public boolean contains(Object element)
true
if this set contains the specified element.contains
in interface Collection<E>
contains
in interface Set<E>
contains
in class AbstractCollection<E>
element
- the element whose presence in this set is to be tested.true
if this set contains the specified element.public boolean remove(Object element)
true
if this set changed as a result of this operation.remove
in interface Collection<E>
remove
in interface Set<E>
remove
in class AbstractCollection<E>
element
- the element to remove.true
if this set changed as a result of this operation.public void clear()
clear
in interface Collection<E>
clear
in interface Set<E>
clear
in class AbstractCollection<E>
public Iterator<E> iterator()
public SortedSet<E> headSet(E toElement)
toElement
frequency.headSet
in interface SortedSet<E>
toElement
- high endpoint (exclusive) of the returned set. May be null
.public SortedSet<E> tailSet(E fromElement)
fromElement
frequency.tailSet
in interface SortedSet<E>
fromElement
- low endpoint (inclusive) of the returned set. May be null
.public SortedSet<E> subSet(E fromElement, E toElement)
fromElement
frequency inclusive to toElement
frequency exclusive.subSet
in interface SortedSet<E>
fromElement
- low endpoint (inclusive) of the returned set. May be null
.toElement
- high endpoint (exclusive) of the returned set. May be null
.public E first() throws NoSuchElementException
first
in interface SortedSet<E>
NoSuchElementException
- if this set is empty.public E last() throws NoSuchElementException
last
in interface SortedSet<E>
NoSuchElementException
- if this set is empty.public final Comparator<E> comparator()
FrequencySortedSet
, the comparator is always this
.
This method is final because the FrequencySortedSet
implementation makes
assumptions on the comparator that would not hold if this method were overridden.
comparator
in interface SortedSet<E>
public final int compare(E o1, E o2)
FrequencySortedSet
with default ordering, this method returns a positive number if o1
has been added more frequently
to this set than o2
, a negative number if o1
has been added less frequently than o2
,
and 0 otherwise. For FrequencySortedSet
with reverse ordering, this is the converse.
This method is final because the FrequencySortedSet
implementation makes
assumptions on the comparator that would not hold if this method were overridden.
compare
in interface Comparator<E>
public int frequency(E element)
element
- the element whose frequency is to be obtained.0
if it does not occur in this set.public int[] frequencies()
public Object[] toArray()
toArray
in interface Collection<E>
toArray
in interface Set<E>
toArray
in class AbstractCollection<E>
public <T> T[] toArray(T[] array)
toArray
in interface Collection<E>
toArray
in interface Set<E>
toArray
in class AbstractCollection<E>
T
- the type of the array elements.array
- the array where to copy the elements.Copyright © 2010–2017 The Apache Software Foundation. All rights reserved.