我有一个名为
在另一个类中,我使用map来调用存储的数据:
并以逗号分隔的形式将其打印出来:
ShoppingCartHelper
的类和一个方法。private static Map<Product, ShoppingCartEntry> cartMap = new HashMap<Product, ShoppingCartEntry>();
存储产品数据到购物车存储器(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;
}
在另一个类中,我使用map来调用存储的数据:
private List<Product> mCartList;
mCartList = ShoppingCartHelper.getCartList();
并以逗号分隔的形式将其打印出来:
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());
它的打印格式为 com.android.test@34566f3,com.android.test@29f9042
如何将Map中的数据转换成字符串(人类可读)?