失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Java 8 基础教程 - Predicate java基础面试笔试题

Java 8 基础教程 - Predicate java基础面试笔试题

时间:2021-08-27 19:46:53

相关推荐

Java 8 基础教程 - Predicate java基础面试笔试题

我总结出了很多互联网公司的面试题及答案,并整理成了文档,以及各种学习的进阶学习资料,免费分享给大家。

扫描二维码或搜索下图红色VX号,加VX好友,拉你进【程序员面试学习交流群】免费领取。也欢迎各位一起在群里探讨技术。

在Java 8中,Predicate是一个函数式接口,可以被应用于lambda表达式和方法引用。其抽象方法非常简单:

/*** Evaluates this predicate on the given argument.** @param t the input argument* @return {@code true} if the input argument matches the predicate,* otherwise {@code false}*/boolean test(T t);

即对t进行断言,返回true或者false。 例如:在filter中 就会接收一个Predicate

/*** Returns a stream consisting of the elements of this stream that match* the given predicate.** <p>This is an <a href="package-summary.html#StreamOps">intermediate* operation</a>.** @param predicate a non-interfering stateless predicate to apply to each element to determine if it* should be included in the new returned stream.* @return the new stream*/Stream<T> filter(Predicate<? super T> predicate);

下面来演示一下如何使用Predicate

package predicateExample;public class Employee {public Employee(Integer id, Integer age, String gender, String fName, String lName){this.id = id;this.age = age;this.gender = gender;this.firstName = fName;this.lastName = lName;}private Integer id;private Integer age;private String gender;private String firstName;private String lastName;//Please generate Getter and Setters@Overridepublic String toString() {return this.id.toString()+" - "+this.age.toString(); //To change body of generated methods, choose Tools | Templates.}public static Predicate<Employee> isAdultMale() {return p -> p.getAge() > 21 && p.getGender().equalsIgnoreCase("M");}public static Predicate<Employee> isAdultFemale() {return p -> p.getAge() > 18 && p.getGender().equalsIgnoreCase("F");}public static Predicate<Employee> isAgeMoreThan(Integer age) {return p -> p.getAge() > age;}}

上面的代码定义了多个Predicate,分别对应多个筛选条件,下面开始使用这些断言:

package predicateExample;import java.util.List;import java.util.function.Predicate;import java.util.stream.Collectors;public class EmployeePredicates{public static Predicate<Employee> isAdultMale() {return p -> p.getAge() > 21 && p.getGender().equalsIgnoreCase("M");}public static Predicate<Employee> isAdultFemale() {return p -> p.getAge() > 18 && p.getGender().equalsIgnoreCase("F");}public static Predicate<Employee> isAgeMoreThan(Integer age) {return p -> p.getAge() > age;}public static List<Employee> filterEmployees (List<Employee> employees, Predicate<Employee> predicate) {return employees.stream().filter( predicate ).collect(Collectors.<Employee>toList());}}

package predicateExample;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import static predicateExample.EmployeePredicates.*;public class TestEmployeePredicates {public static void main(String[] args){Employee e1 = new Employee(1,23,"M","Rick","Beethovan");Employee e2 = new Employee(2,13,"F","Martina","Hengis");Employee e3 = new Employee(3,43,"M","Ricky","Martin");Employee e4 = new Employee(4,26,"M","Jon","Lowman");Employee e5 = new Employee(5,19,"F","Cristine","Maria");Employee e6 = new Employee(6,15,"M","David","Feezor");Employee e7 = new Employee(7,68,"F","Melissa","Roy");Employee e8 = new Employee(8,79,"M","Alex","Gussin");Employee e9 = new Employee(9,15,"F","Neetu","Singh");Employee e10 = new Employee(10,45,"M","Naveen","Jain");List<Employee> employees = new ArrayList<Employee>();employees.addAll(Arrays.asList(new Employee[]{e1,e2,e3,e4,e5,e6,e7,e8,e9,e10}));System.out.println(filterEmployees(employees, isAdultMale()));System.out.println(filterEmployees(employees, isAdultFemale()));System.out.println(filterEmployees(employees, isAgeMoreThan(35)));//Employees other than above collection of "isAgeMoreThan(35)" can be get using negate()System.out.println(filterEmployees(employees, isAgeMoreThan(35).negate()));}}

输出:

[1 - 23, 3 - 43, 4 - 26, 8 - 79, 10 - 45][5 - 19, 7 - 68][3 - 43, 7 - 68, 8 - 79, 10 - 45][1 - 23, 2 - 13, 4 - 26, 5 - 19, 6 - 15, 9 - 15]

正则表达式表示为Predicate

可以通过pile().asPredicate()将正则表达式转换为Predicate。 在Java 8之前,从一个数组中找出符合正则规则的字符串的方法是

public static void main(String[] args){Pattern pattern = pile("^(.+)@$");// Input listList<String> emails = Arrays.asList("alex@", "bob@","cat@", "david@");for(String email : emails){Matcher matcher = pattern.matcher(email);if(matcher.matches()){System.out.println(email);}}}

转换为Predicat为:

import java.util.Arrays;import java.util.List;import java.util.function.Predicate;import java.util.regex.Pattern;import java.util.stream.Collectors;public class RegexPredicateExample {public static void main(String[] args) {// Compile regex as predicatePredicate<String> emailFilter = pile("^(.+)@$").asPredicate();// Input listList<String> emails = Arrays.asList("alex@", "bob@","cat@", "david@");// Apply predicate filterList<String> desiredEmails = emails.stream().filter(emailFilter).collect(Collectors.<String>toList());// Now perform desired operationdesiredEmails.forEach(System.out::println);}}原文连接:https://www.codemore.top/cates/Backend/post/-05-06/predicate

转载:/hitandrew/p/9007451.html

推荐内容:

java常见面试总结

Java岗 面试考点精讲(基础篇01期)

java面试

互联网高级Java面试总结

java 面试题总结01

java.lang.Thread类详解

java常见面试题及答案

聊聊Java 8 Lambda 表达式

java中equals,hashcode和==的区别

Java面试题180309

如果觉得《Java 8 基础教程 - Predicate java基础面试笔试题》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。