Skip to content

Instantly share code, notes, and snippets.

@blabadi
Created September 8, 2014 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blabadi/89c4aa03ec868e938939 to your computer and use it in GitHub Desktop.
Save blabadi/89c4aa03ec868e938939 to your computer and use it in GitHub Desktop.
package com.blabadi.jdk8.optional;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class Driver {
private static List<Student> students = new ArrayList<Student>();
public static void main(String[] args) {
students.add(new Student("AAA", 1));
students.add(new Student("BBB", 1));
students.add(new Student("CCC", 1));
students.add(new Student("DDD", 1));
int id = findStudentByName("ZZ").map(x-> x.id).orElse(-1);
System.out.println(id);
}
public static Optional<Student> findStudentByName(String name)
{
return students.stream().filter(x-> x.name.equals("name")).findFirst();
}
}
class Student {
String name;
int id;
public Student() {
}
public Student(String name, int i) {
this.name = name;
this.id = i;
}
public int getId(){
return this.id;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment