Splitting objects inside Java stream -


i wondering possible split object inside stream. example employee:

public class employee {      string name;     int age;     double salary;      public employee(string name, int age, double salary) {         this.name = name;         this.age = age;         this.salary = salary;     }      public string getname() { return name; }      public int getage() { return age; }      public double getsalary() { return salary; } } 

i perform operation in stream. simplicity, let (assume code architecture not allow put inside employee class - otherwise easy):

public void someoperationwithemployee(string name, int age, double salary) {     system.out.format("%s %d %.0f\n", name, age, salary); } 

now looks this:

stream.of(new employee("adam", 38, 3000), new employee("john", 19, 2000))         // conversations go here ...         .foreach(e -> someoperationwithemployee(e.getname, e.getage(), e.getsalary)); 

the question - possible put code inside stream this?

stream.of(new employee("adam", 38, 3000), new employee("john", 19, 2000))         // conversations go here         .foreach((a, b, c) -> someoperationwithemployee(a, b, c)); 

what i'm trying achieve? - think if map object fields , process .foreach(this::someoperationwithemployee) code readability improved slightly.


update 14.05.2015

without doubt pass employee object someoperationwithemployee prettiest solution in case can not in real life , should universal solution lambdas.

i not sure fit needs works bit of refection , not checking types.

you can run solution way:

    stream.of(new employee("adam", 38, 3000), new employee("john", 19, 2000))         .foreach(                 e->arraycaller.<triconsumer<string, integer, double>>convert(e::getname, e::getage, e::getsalary)                                                                      .call((a, b, c) -> operation(a, b, c))); 

it call simple method of 'main' class:

private void operation(string name, int age, double salary) {     system.out.format("%s %d %.0f\n", name, age, salary); } 

of course needs auxiliary types:

/** extending interfaces must have method called consume n args */ interface nconsumer {}  /*  * method must called consume reflection.  *  * can define n interfaces this.  */ nterface triconsumer<a, b, c> extends nconsumer {     void consume(a a, b b, c c); }  interface arraycaller<e extends nconsumer> {     void call(e code);     static <t extends nconsumer> arraycaller<t> convert(supplier<?>...argsuppliers) {         final object[] args = new object[argsuppliers.length];         (int = 0; < argsuppliers.length; i++) {             args[i] = argsuppliers[i].get();         }         return new arraycaller<t>() {             @override             public void call(t code) {                 (method m: code.getclass().getmethods()) {                     if (m.getname().equals("consume")) {                         try {                             m.invoke(code, args);                         } catch (illegalaccessexception                                 | illegalargumentexception                                 | invocationtargetexception e) {                             throw new runtimeexception(e);                         }                     }                 }             }         };     } } 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -