Link to home
Start Free TrialLog in
Avatar of SunnyX
SunnyX

asked on

Core java. Create the class with method that transform a collection of customer objects to string object collection.

Let there is pretty straightforward class

public class Employees {
    String lastName;

    Employees(String str) {
        this.lastName = str;

    }

    public String getLastName() {
        return lastName;
    }
}

Open in new window


public class EmployeesTest {

    public static void main(String[] args) { 
        new Employees("Smith");
        new Employees("Page");
        new Employees("Musk");
        new Employees("Dorsy");
        new Employees("Gates");
       
    }
}

Open in new window

I need to create some transform method that convert my employees collection into a String collection with last names of the employees. It is be best if the argument(s) of the method will be at topest level of collection hierarchy as much as possible ( Collection at best if it is possible  ) .
CollectionUtils.transform(stringList, employeesList);

Open in new window


Thx in advance.
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SunnyX
SunnyX

ASKER

   public static Collection<String> transform(Collection<String> strCollection, Collection myCollection) {
        Iterator it = myCollection.iterator();
        while (it.hasNext()) {
            strCollection.add(it.next().toString());
        }
        return strCollection;
    }

Open in new window


Like this ?
is there a way to do it smarter ?
Avatar of SunnyX

ASKER

thx for the help
why are you passing in the string collection as the first argument:  transform(Collection<String> strCollection, Collection myCollection) {


You don't need to.
>> thx for the help
You're welcome.
Thanx 4 axxepting