I have a class ShoppingCartHelper and a method.
private static Map<Product, ShoppingCartEntry> cartMap = new HashMap<Product, ShoppingCartEntry>();
And method that stores data product to cartMap:
public static List<Product> getCartList() {
List<Product> cartList = new Vector<Product>(cartMap.keySet().size());
for(Product p : cartMap.keySet()) {
cartList.add(p);
}
return cartList;
}
In other class I call stored data on map:
private List<Product> mCartList;
mCartList = ShoppingCartHelper.getCartList();
and print it in comma separated:
StringBuilder commaSepValueBuilder = new StringBuilder();
for ( int i = 0; i< mCartList.size(); i++) {
commaSepValueBuilder.append(mCartList.get(i));
if ( i != mCartList.size()-1) {
commaSepValueBuilder.append(", ");
}
}
System.out.println(commaSepValueBuilder.toString());
Its printed like com.android.test@34566f3,com.android.test@29f9042
How do I print data on Map to string (human readable)?
Make your Product class overriding the toString() method or in your custom logic, make a string builder appending not the element itself, but it's fileds, which you wish to recieve as text description of the product instance. I mean something like this:
//since I don't know, what is the Product class, I supposed it has a name filed
commaSepValueBuilder.append(mCartList.get(i).getName());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With