Extract a fields data from a Map into a String

I have an Customer Object like below.

public class Custoemr {
    private String Id;
    Private String Name;
    Private String Address;
    Private String Description;

    Setter/Getter;
    toString;
}

This is Contained in Map<String, Customer> map, which contains the customerId and Object as key and value respectively. For analysis purposes, I need to collect all the customer description data in String to be written in a file.

To do that I need to Extract data from description in String and not List<String>.

I saw several examples on the internet which collects them as a List<String> but I need it in a single String. Is there a way to extract the information without iterating I mean by using java Streams.

Answer

If I understood correctly:

yourMap.values()
       .stream()
       .map(Customer::getDescription)
       .collect(Collectors.joining(","));

Leave a Reply

Your email address will not be published. Required fields are marked *