Reputation: 123
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)?
Upvotes: 4
Views: 9043
Reputation: 17553
Below code works for me by using 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(","));
Upvotes: 0
Reputation: 29276
Override toString in Product class and use Java 8 Streams to make it more simpler, as below:
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
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 + "]";
}
}
Upvotes: 0
Reputation: 131
You can override toString method in your Product class like below,
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.
Upvotes: 0
Reputation: 159086
Don't use Vector
, use ArrayList
. Quoting javadoc:
If a thread-safe implementation is not needed, it is recommended to use
ArrayList
in place ofVector
The shorter version of getCartList()
is:
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.
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:
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
:
StringJoiner sj = new StringJoiner(", ");
for (Product product : ShoppingCartHelper.getCartList())
sj.add(product);
System.out.println(sj.toString());
Upvotes: 2
Reputation: 28106
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());
Upvotes: 3