java.lang.Object
com.blackhillsoftware.smf.summary.Top<T>
- Type Parameters:
T
- The type of values to be processed.
- All Implemented Interfaces:
Iterable<T>
Collect the top values for a specific type, as determined
by a supplied
Comparator
function. This class is designed
to handle sequences of millions of entries or more. It only
keeps a reference to the largest values seen so far, rather
than the entire list.
You can create an instance of the class to add values
one at a time, or use it to collect values from a Stream
.
Process values one at a time:
try (SmfRecordReader reader = SmfRecordReader.fromName("//DD:INPUT")
.include(30, 5))
{
Top<Smf30Record> topCpu =
new Top<>(10,
Comparator.comparing(r30 -> r30.processorAccountingSection().smf30cpt())
);
for (SmfRecord record : reader)
{
Smf30Record r30 = Smf30Record.from(record);
if (r30.processorAccountingSection() != null)
{
topCpu.add(r30);
}
}
for (Smf30Record r30 : topCpu)
{
System.out.format("%-24s %-8s %-8s %6.2f%n",
r30.smfDateTime(),
r30.identificationSection().smf30jbn(),
r30.identificationSection().smf30jnm(),
r30.processorAccountingSection().smf30cptSeconds());
}
}
Collect values from a Stream:
try (SmfRecordReader reader = SmfRecordReader.fromName("//DD:INPUT")
.include(30, 5))
{
List<Smf30Record> topCpu = reader.stream()
.map(record -> Smf30Record.from(record))
.filter(r30 -> r30.processorAccountingSection() != null)
.collect(Top.values(10,
Comparator.comparing(r30 -> r30.processorAccountingSection().smf30cpt())
));
for (Smf30Record r30 : topCpu)
{
System.out.format("%-24s %-8s %-8s %6.2f%n",
r30.smfDateTime(),
r30.identificationSection().smf30jbn(),
r30.identificationSection().smf30jnm(),
r30.processorAccountingSection().smf30cptSeconds());
}
}
-
Constructor Summary
ConstructorDescriptionTop
(int n, Comparator<? super T> comparator) Create an instance to find the top n values in a sequence of values (the largest according to the supplied comparator) -
Method Summary
Modifier and TypeMethodDescriptionvoid
Add a new value.boolean
addAll
(Collection<? extends T> collection) Add all entries from a collection.iterator()
Get an iterator for the values.toList()
Get the values as a List.values
(int n, Comparator<? super T> comparator) Collect the top values from aStream
(the largest according to the supplied comparator).Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
Top
Create an instance to find the top n values in a sequence of values (the largest according to the supplied comparator)- Parameters:
n
- the number of values to keep, ie top ncomparator
- the comparison function to use to compare values.
-
-
Method Details
-
values
Collect the top values from aStream
(the largest according to the supplied comparator). This collector is designed to work with streams containing millions of entries.- Type Parameters:
T
- the type of value- Parameters:
n
- the number of values to collectcomparator
- the comparison function to use to compare values.- Returns:
- the top value collector
-
add
Add a new value. It will be retained or discarded depending on whether it is larger than other values according to the comparator.- Parameters:
value
- the value to add
-
addAll
Add all entries from a collection.- Parameters:
collection
- the source collection- Returns:
- true if values were processed
-
toList
Get the values as a List. The returned list will be in the order largest to smallest, according to the supplied comparator.- Returns:
-
iterator
Get an iterator for the values. The iterator will return values in the order largest to smallest, according to the supplied comparator.
-