[英]Convert HashMap to string comma separated
I have a class ShoppingCartHelper
and a method.我有一个类
ShoppingCartHelper
和一个方法。
private static Map<Product, ShoppingCartEntry> cartMap = new HashMap<Product, ShoppingCartEntry>();
And method that stores data product to cartMap:以及将数据产品存储到 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
它的打印像
com.android.test@34566f3,com.android.test@29f9042
How do I print data on Map to string (human readable)?如何将 Map 上的数据打印到字符串(人类可读)?
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.使您的
Product
类覆盖toString()
方法或在您的自定义逻辑中,创建一个字符串构建器,而不是附加元素本身,而是附加文件,您希望将其作为产品实例的文本描述接收。 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());
Don't use Vector
, use ArrayList
.不要使用
Vector
,使用ArrayList
。 Quoting javadoc:引用 javadoc:
If a thread-safe implementation is not needed, it is recommended to use
ArrayList
in place ofVector
如果不需要线程安全的实现,建议使用
ArrayList
代替Vector
The shorter version of getCartList()
is: getCartList()
的较短版本是:
public static List<Product> getCartList() {
return new ArrayList<>(cartMap.keySet());
}
As for how to build comma-separated list of products, the best way is to implement the Product
method toString()
.至于如何构建以逗号分隔的产品列表,最好的方法是实现
Product
方法toString()
。 This will also help when debugging.这在调试时也会有所帮助。
public class Product {
// lots of code here
@Override
public String toString() {
return getName(); // Assuming Product has such a method
}
}
Then you can use StringBuilder
in a simple for-each
loop:然后您可以在一个简单
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());
In Java 8 that can be simplified by using StringJoiner
:在 Java 8 中,可以使用
StringJoiner
进行简化:
StringJoiner sj = new StringJoiner(", ");
for (Product product : ShoppingCartHelper.getCartList())
sj.add(product);
System.out.println(sj.toString());
You can override toString method in your Product class like below,您可以在 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 +
'}';
}
} }
it can print string in human readable format.它可以以人类可读的格式打印字符串。
Override toString in Product class and use Java 8 Streams to make it more simpler, as below:覆盖 Product 类中的toString并使用 Java 8 Streams使其更简单,如下所示:
mCartList.stream().map(e -> e.toString())
.collect(Collectors.joining(","));
Usage:用法:
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产品.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 + "]";
}
}
Below code works for me by using Java 8 Streams:下面的代码使用Java 8 Streams 为我工作:
For Hashmap :对于哈希图:
hashmapObject.entrySet().stream().map(e -> e.toString())
.collect(Collectors.joining(","));
For List :对于列表:
listObject.stream().map(e -> e.toString())
.collect(Collectors.joining(","));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.