将HashMap转换为逗号分隔的字符串

时间:2015-11-17 03:45:02

标签: java android arraylist hashmap

我有一个班级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;
}

在其他课程中,我在地图上调用存储的数据:

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上的数据打印到字符串(人类可读)?

4 个答案:

答案 0 :(得分:3)

让你的Product类重写toString()方法或在自定义逻辑中,使字符串构建器不附加元素本身,但它是fileds,您希望将其作为产品的文本描述接收实例。我的意思是这样的:

//since I don't know, what is the Product class, I supposed it has a name filed
commaSepValueBuilder.append(mCartList.get(i).getName());

答案 1 :(得分:2)

请勿使用Vector,请使用ArrayList。引用javadoc:

  

如果不需要线程安全的实现,建议使用ArrayList代替Vector

getCartList()的较短版本是:

public static List<Product> getCartList() {
    return new ArrayList<>(cartMap.keySet());
}

至于如何构建逗号分隔的产品列表,最好的方法是实现Product方法toString()。这在调试时也会有所帮助。

public class Product {

    // lots of code here

    @Override
    public String toString() {
        return getName(); // Assuming Product has such a method
    }
}

然后,您可以在简单的for-each循环中使用StringBuilder

StringBuilder buf = new StringBuilder();
for (Product product : ShoppingCartHelper.getCartList()) {
    if (buf.length() != 0)
        buf.append(", ");
    buf.append(product); // or product.getName() if you didn't implement toString()
}
System.out.println(buf.toString());

在Java 8中,可以使用StringJoiner简化:

StringJoiner sj = new StringJoiner(", ");
for (Product product : ShoppingCartHelper.getCartList())
    sj.add(product);
System.out.println(sj.toString());

答案 2 :(得分:0)

您可以在Product类中覆盖toString方法,如下所示,

public class Product {

//sample properties
private String name;
private Long id;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

@Override
public String toString() {
    return "Product{" +
            "name='" + name + '\'' +
            ", id=" + id +
            '}';
}

}

它可以以人类可读的格式打印字符串。

答案 3 :(得分:0)

覆盖Product类中的toString并使用Java 8 Streams使其更简单,如下所示:

 mCartList.stream().map(e -> e.toString())
                .collect(Collectors.joining(","));

<强>用法:

List<Product> mCartList = new ArrayList<>();
        mCartList.add(new Product(1, "A"));
        mCartList.add(new Product(2, "B"));

        String commaSepValue = mCartList.stream().map(e -> e.toString())
                .collect(Collectors.joining(","));
        System.out.println(commaSepValue);

Product.java

final class Product {
        private final int id;
        private final String name;

        public Product(int id, String name) {
            this.id = id;
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public int getId() {
            return id;
        }

        @Override
        public String toString() {
            return "Product [id=" + id + ", name=" + name + "]";
        }
    }