java - Categorize ArrayList of objects based on their property -
i have arraylist of products, , each product has category property (so each category can have many products). need format data products categorized according category property.
i think hashmap useful, use category key , arraylist of products value.
if correct approach, can assist me logic involved in turning arraylist hashmap have described? or maybe there better way of handling it.
/** update **/
here sample method, i'm not sure how make logic happen:
private hashmap<string, arraylist> sortproductsbycategory (arraylist<product> productlist) { // hashmap value category name, , value array of products hashmap<string, arraylist> map; for(product product: productlist) { // if key not exist in hashmap if(!map.containskey(product.getcategory()) { // add key map, add product new arraylist } else { // add product arraylist corresponds key } return map; } }
yes, absolutely valid approach want switch "1-dimensional" view "2-dimensional".
Comments
Post a Comment