-
-
Save blabadi/89c4aa03ec868e938939 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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