java 함수 프로 그래 밍 java.util.function.Consumer

java.util.function Consumer 는 T 대상 을 받 고 값 을 되 돌려 주지 않 습 니 다.
      :
              

    Iterable   forEach      Consumer,             ,    Iterator      。
    Iterable  forEach   :
    default void forEach(Consumer super T> action) {
    Objects.requireNonNull(action);
    for (T t : this) {
        action.accept(t);
    }
}

        :
        forEach           ,    

        demo

         public static void main(String[] args) {

    Consumer methodParam  = HelloHandler::staticMethod;
    Consumer methodParam1  = HelloHandler::staticMethod;
    Consumer methodParam2 =  methodParam.andThen(methodParam1);
    methodParam2.accept(1);
    BiConsumer biConsumer = HelloHandler::normalMethod;

// methodParam1.accept(2);// HelloHandler helloHandler = new HelloHandler();//// Function function = new HelloHandler()::normalMethod;// System.out.println( function.apply(1));
// Consumer methodParam1 = helloHandler::normalMethod;
    List list = new ArrayList<>();
    list.add("a");
    list.add("b");
    list.add("c");
    list.forEach(new DemoConsumer());
}
    static class DemoConsumer implements Consumer{

    @Override
    public void accept(String s) {
        //        
        System.out.println("s = [" + s + "]");
    }
}

                

          lambda    :
    list.forEach(c->{
        //        
        System.out.println("c = [" + c + "]");
    });

좋은 웹페이지 즐겨찾기