как
Например, используя Stream API :)
import java.util.List;
import java.util.Random;
import java.util.stream.Stream;
public class Test {
public static void main(String[] args) {
// Create List
List<MyObj> objects = Stream.generate(MyObj::new).limit(10).toList();
System.out.println(objects);
// Get minimal index item
MyObj minIndexObj = objects
.stream()
.reduce((a, b) -> a.getIndex() < b.getIndex() ? a : b).orElse(null);
System.out.println(minIndexObj);
}
}
class MyObj {
private final int index;
public MyObj() {
index = new Random().nextInt();
}
public int getIndex() {
return index;
}
@Override
public String toString() {
return "MyObj { index= " + index + " }";
}
}