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上的数据打印到字符串(人类可读)?
4 个解决方案
#1
3
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:
使您的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());
#2
2
Don't use Vector
, use ArrayList
. Quoting javadoc:
不要使用Vector,请使用ArrayList。引用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()
. This will also help when debugging.
至于如何构建以逗号分隔的产品列表,最好的方法是实现Product方法toString()。这在调试时也会有所帮助。
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());
#3
0
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.
它可以以人类可读的格式打印字符串。
#4
0
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
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 + "]";
}
}
#1
3
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:
使您的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());
#2
2
Don't use Vector
, use ArrayList
. Quoting javadoc:
不要使用Vector,请使用ArrayList。引用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()
. This will also help when debugging.
至于如何构建以逗号分隔的产品列表,最好的方法是实现Product方法toString()。这在调试时也会有所帮助。
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());
#3
0
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.
它可以以人类可读的格式打印字符串。
#4
0
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
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 + "]";
}
}