Java Stream Map T -> ?T:List<T> -


i'm new java , love stream api. got loop:

list<filetree> expanded = new arraylist<>(); for(filetree item : tree){     if(item.gettype().equals("tree")){         expanded.addall(gettreeofsubstudpackage(item.getname()));     }     else{         expanded.add(item);     } } 

and wonder if converted neat stream. first guess following:

tree.stream().map(filetree -> {     if(filetree.gettype().equals("tree")){         return gettreeofsubstudpackage(filetree.getname());     }     else{         return filetree;     } }).collect(collectors.tolist()); 

it compiles fine. recommended, have implemented no-go or there nicer way? , last not least: there overhead in .stream() make improvement worthless?

appendix

list<filetree> gettreeofsubstudpackage(...){     //... }  class filetree {     private string name;     private string type;     private string mode;     private string id;     //... public getter , setter ... } 

you need map , flatmap.

arrays.aslist(item) add element list flat in next flatmap

    tree.stream()         .map(item -> item.gettype().equals("tree") ? gettreeofsubstudpackage(item.getname()) : arrays.aslist(item))         .flatmap(collection::stream)         .collect(collectors.tolist()); 

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -