fork download
  1. import java.util.List;
  2.  
  3. class Ideone {
  4. public static void main (String[] args) {
  5. List students = List.of(
  6. Student.of(1.0),
  7. "foo",
  8. Student.of(2.0),
  9. new Object(),
  10. Student.of(3.0),
  11. Student.of(4.0));
  12.  
  13. System.out.println(sumScore(students));
  14. }
  15.  
  16. public static double sumScore(List students) {
  17. return ((List<?>) students).stream()
  18. .filter(o -> o instanceof Student)
  19. .map(o -> (Student) o)
  20. .mapToDouble(Student::getScore)
  21. .sum();
  22. }
  23. }
  24.  
  25. class Student {
  26. final double score;
  27.  
  28. private Student(double score) {
  29. this.score = score;
  30. }
  31.  
  32. public static Student of(double score) {
  33. return new Student(score);
  34. }
  35.  
  36. public double getScore() {
  37. return score;
  38. }
  39. }
Success #stdin #stdout 0.08s 34316KB
stdin
Standard input is empty
stdout
10.0