Deepika R:
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
public class GetMiles {
public static void main(String args[]) {
List<Student> studentList = new ArrayList<>();
Student s = new Student();
s.setFee("12000");
studentList.add(s);
Student s1 = new Student();
s1.setFee("3000");
studentList.add(s1);
Optional<Student> optionalStudent =
studentList.stream().min(Comparator.comparing(Student::getFee));
if (optionalStudent.isPresent()) {
System.out.println(optionalStudent.get().getFee());
}
}
static class Student {
private String fee;
public String getFee() {
return this.fee;
}
public void setFee(String fee) {
this.fee = fee;
}
}
}
上記の例では3000を返しますが、我々は、それはすべてのためのシナリオの大半でも、その作業罰金を2000年を返しませんが2000と3000を与える場合は12000を返す必要があります。
Imran.Khan:
解析リストのintにマップしてからのようなサンプルコードの下に料金の分を取得します:
Optional<Integer> optionalVal = studentList.stream().map(l ->
Integer.parseInt(l.getFee())).min(Comparator.comparingInt(k -> k));
if(optionalVal.isPresent()) {
String minFee = String.valueOf(optionalVal.get());
Optional<Student> studentObj = studentList.stream().filter(p ->
minFee.equals(p.getFee())).findFirst();
}