Usage of function type congruent lambda expressions in Java 8 -


i struggle definition , usage of

stream.collect(supplier<r> supplier, biconsumer<r,? super t> accumulator, biconsumer<r,r> combiner) 

method in java 8.

the method signature includes biconsumer typed parameters. biconsumer functionalinterface defines 1 functional method accept(object, object). far understand can use lambda expression congruent functional interface.

but example mentioned in stream.collect javadoc e.g.

 list<string> aslist = stringstream.collect(arraylist::new, arraylist::add, arraylist::addall); 

i not understand why arraylist.add(e e) (single parameter) congruent biconsumer.accept(t t, u u) method (two parameters) , can used accumulator function in collect method.

as see have lack of understanding , appreciate explanation.

the accumulator biconsumer's 2 parameters (1) list , (2) item add it. this:

list<string> aslist = stringstream.collect(     arraylist::new,     arraylist::add,     arraylist::addall ); 

is equivalent this:

list<string> aslist = stringstream.collect(     () -> new arraylist<>(),     (list, item) -> list.add(item),     (list1, list2) -> list1.addall(list2) ); 

which give same result this:

list<string> aslist = stringstream.collect(     new supplier<arraylist<string>>() {         @override         public arraylist<string> get() {             return new arraylist<>();         }     },      new biconsumer<arraylist<string>,string>() {         @override         public void accept(arraylist<string> list, string item) {             list.add(item);         }     },      new biconsumer<arraylist<string>,arraylist<string>>() {         @override         public void accept(arraylist<string> list1, arraylist<string> list2) {             list1.addall(list2);         }     } ); 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -