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

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 -