zoukankan      html  css  js  c++  java
  • Java 8 : Predicate和Consumer接口

    1.consumer

     jdk 1.8 的 Iterable 接口中的 forEach 默认方法:

    public interface Iterable<T> {
        default void forEach(Consumer<? super T> action) {
            Objects.requireNonNull(action);
            for (T t : this) {
                action.accept(t);
            }
        }
    }

    2.

    public class PredicateConsumerDemo {
    
        public static void main(String[] args) {
    
            Student stu = new Student(90);
            stu = updateStudentFee(stu,
                    student -> student.grade > 80,
                    student -> student.discount = 0.2);
            System.out.println(stu);
    
        }
    
        public static Student updateStudentFee(Student student, Predicate<Student> predicate, Consumer<Student> consumer){
            if ( predicate.test(student)){
                consumer.accept(student);
            }
            return student;
        }
    
    }
    class Student{
        Double discount = 0.0;
        Double fee = 20000.0;
        Integer grade ;
    
        public Student(Integer grade) {
            this.grade = grade;
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "discount=" + discount +
                    ", fee=" + fee +
                    '}';
        }
    }
  • 相关阅读:
    QtDBus编程详解
    QProcess详解
    python 爬虫 亚航 指定日期间的航线
    python 模块
    centos postgres 安装、远程连接
    python 爬虫 anyproxy
    python_scrapy_filespipe重写
    python_xpath
    常见问题汇总
    python_scrapy_log日志
  • 原文地址:https://www.cnblogs.com/yuyutianxia/p/6322985.html
Copyright © 2011-2022 走看看