prabu
prabu

Reputation: 1277

Convert string in list objects to comma separated

I have an arraylist of objects, where one of the instance variables in the object is string. I would like to convert the string variables in the object list into a single comma-separated string.

For example,

I have an object employee as below.

public class Employee {

    private String name;
    private int age;
}

Consider a list of employees,

List<Employee> empList = new ArrayList<Employee>
Employee emp1 = new Employee ("Emp 1",25);
Employee emp2 = new Employee ("Emp 2",25);
empList.add(emp1);
empList.add(emp2);

Expected output (Type : String):

Emp 1,Emp 2

I know it can be done through looping. But I'm looking for some sophisticated ways to do it and keep the code simpler.

Upvotes: 3

Views: 6775

Answers (5)

yglodt
yglodt

Reputation: 14551

Meanwhile, using the Java 8 Stream API:

String res = String.join(",", empList.stream().map(c -> c.getName()).toList());

Upvotes: 0

Konstantin Yovkov
Konstantin Yovkov

Reputation: 62864

Override the toString() method in the Employee class

public String toString() {
   return name;
}

Then, print the list:

String listToString = empList.toString();
System.out.println(listToString.substring(1, listToString.length() - 1));

This is not that sophisticated way to print it, but I doesn't involve the usage of third-party libraries.

If you'd like to use third party libraries, here are several way you can print the list.

// Using Guava
String guavaVersion = Joiner.on(", ").join(items);

// Using Commons / Lang
String commonsLangVersion = StringUtils.join(items, ", ");

Upvotes: 4

Garrett Hall
Garrett Hall

Reputation: 30032

If you want a real clean way to do this, use function literals in Java 8. Otherwise,

In Employee class:

public String toString() { return name; }

Print out the list, removing the square brackets

list.toString().replaceAll("\\[(.*)\\]", "$1");

Upvotes: 0

Adam Siemion
Adam Siemion

Reputation: 16039

I would like to convert the string variables in the object list in to a single comma separated string.

  • implement your own toString():

    public String toString() { return name; }
    
  • call method toString() on your java.util.List:

    empList.toString();
    
  • get rid of '[' and ']':

    String s = empList.toString();
    s = s.substring(1, s.length()-1);
    

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 122008

Without loop ,Using list.toString()

public class Employee {

     public Employee(String string, int i) {
        this.age=i;
        this.name=string;
    }
    private String name;
     private int age;

     @Override
     public String toString() {
           return name + " " + age;
        }

   public static void main(String[] args) {
       List<Employee> empList = new ArrayList<Employee>();
         Employee emp1 = new Employee ("Emp 1",25);
         Employee emp2 = new Employee ("Emp 2",25);
         empList.add(emp1);
         empList.add(emp2);
         System.out.println(empList.toString().
                       substring(1,empList.toString().length()-1));
}
}

Prints

Emp 1 25, Emp 2 25

Upvotes: 0

Related Questions