Predicates In Java

 package java8;


import java.util.Arrays;

import java.util.List;

import java.util.function.Function;

import java.util.function.Predicate;

import java.util.stream.Collectors;


public class PredicateExample {


static class Student {

String name;

int marks;


Student() {


}


Student(String name, int marks) {

super();

this.name = name;

this.marks = marks;

}

}


public static void main(String[] args) {


Function<Student, String> f = s -> {

int marks = s.marks;

String grade = "";

if (marks >= 80)

grade = "A";

else if (marks >= 70)

grade = "B";

else

grade = "E";

return grade;

};


Predicate<Student> p = s -> s.marks > 75;


List<Student> list = Arrays.asList(new Student("upendra", 100), new Student("deepa", 75));


System.out.println(list.stream().filter(p).map(x -> f.apply(x)).collect(Collectors.joining(",")));


}

}


Comments

Popular posts from this blog

3 Lines of Code And You Can Download Your Favourite Youtube Video.

Leetcode Problem(Easy) Roman to Integer

Leetcode : (Find Minimum in Rotated Sorted Array)