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.