I have a Set
of some objects. I need to get the two min objects from the Set
.
My example is as follows:
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Example {
public static void main( String[] args ) {
SomeObject obj1 = new SomeObject(1);
SomeObject obj2 = new SomeObject(2);
SomeObject obj3 = new SomeObject(3);
Set<SomeObject> set = Stream.of(obj1,obj2,obj3)
.collect(Collectors.toSet());
SomeObject minObject= set.stream()
.min(Comparator.comparingInt(object->object.getValue()))
.get();
System.out.println(minObject);
}
}
class SomeObject{
private int value;
SomeObject(int value){
this.value=value;
}
public int getValue() {
return value;
}
@Override
public String toString() {
return this.value+"";
}
}
In my example I can get the min object from this Set
based on the value attribute.
So, I need to get the two min values using Java stream operations.
The answers should be fast because in my real program I call this method many times and my sets are very large.
To get max or min date from a stream of dates, you can use Comparator. comparing( LocalDate::toEpochDay ) Comparator. The toEpochDay() function returns the count of days since epoch i.e. 1970-01-01.
Stream of(T t) returns a sequential Stream containing a single element. Syntax : static Stream of(T t) Parameters: This method accepts a mandatory parameter t which is the single element in the Stream. Return Value: Stream of(T t) returns a sequential Stream containing the single specified element.
Syntax : Stream<T> limit(long N) Where N is the number of elements the stream should be limited to and this function returns new stream as output. Exception : If the value of N is negative, then IllegalArgumentException is thrown by the function.
Being a type of Collection, we can convert a set to Stream using its stream() method.
You can get the second min value as follows:
set.stream()
.filter(s -> s.getValue() != minObject.getValue())
.min(Comparator.comparingInt(object -> object.getValue()))
.get();
filter
.min
or you can get both at the same time:
Stream<SomeObject> twoMinValues = set.stream()
.sorted(Comparator.comparingInt(object -> object.getValue()))
.limit(2);
The awnser should be fast because In my real program i call this method many times and my Sets are very Large.
As for needing a "fast" program, I'd recommend that you first try to solve the problem at hand just using the typical imperative approach as in most cases they're faster than using streams.
if it's not much better then "consider" using parallel streams if and only if you know that you can leverage parallelism.
see Should I always use a parallel stream when possible?
You can pass your Set<E>
to an instance of NavigableSet<E>
, then you can poll the first two (lowest) elements from it :
final NavigableSet<SomeObject> navigableSet = new TreeSet<>(Comparator.comparingInt(SomeObject::getValue));
navigableSet.addAll(set);
final SomeObject firstMin = navigableSet.pollFirst();
final SomeObject secondMin = navigableSet.pollFirst();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With