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.
 
     
     
    