Java 8 streams intermediary map/collect to a stream with 2 values -


imagine have following working lambda expression:

        map<field, string> fields = arrays.stream(resultclass.getdeclaredfields())             .filter(f -> f.isannotationpresent(column.class))             .collect(tomap(f -> {                 f.setaccessible(true);                 return f;             }, f -> f.getannotation(column.class).name())); 

i create stream 2 values before filter statement. want mapping still keep original value aside it. want achieve this:

        this.fields = arrays.stream(resultclass.getdeclaredfields())             //map <field, annotation> stream             .filter((f, a) -> != null)             .collect(tomap(f -> {                 f.setaccessible(true);                 return f;             }, f -> a.name())); 

is possible java 8 streams? have looked @ collect(groupingby()) still without succes.

you need pair holds 2 values. can write own, here code repurposes abstractmap.simpleentry:

     map<field, string> fields = arrays.stream(resultclass.getdeclaredfields())             .map(f -> new abstractmap.simpleentry<>(f, f.getannotation(column.class)))             .filter(entry -> entry.getvalue() != null)             .peek(entry -> entry.getkey().setaccessible(true))             .collect(tomap(map.entry::getkey, entry -> entry.getvalue().name())); 

Comments

Popular posts from this blog

How to check data type in C++? -

javascript - Is getTimezoneOffset() stable during daylight saving transition? -

sql server - SQL Query returns one result, does not return 0 or NULL result -