java - Is It Possible to get Arraylist of product name from Arraylist of custom class without looping -


suppose have class:

class product {    int pid;    string pname;    int pprice; } 

i have arraylist<product>, can arraylist<string> contain product name only without loop.

say of cursor or collection utils or other thing in android.

in java 8 can use stream , map function on list of products.

assume have:

list<product> productlist = new arraylist<product>(); productlist.add(new product(1, "test", 100)); productlist.add(new product(1, "test2", 100)); 

you can use approach map list<string>:

list<string> productnames = productlist.parallelstream()                               .map(product::getpname)                               .collect(tolist()); 

edit:


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -