Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 99
  • Last Modified:

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.
0
SunnyX
Asked:
SunnyX
  • 2
  • 2
1 Solution
 
zzynxSoftware engineerCommented:
Pretty easy:
Iterate over your List of Employees objects (remark: the name of your class should better be Employee instead of Employees) for each item, insert the last name in your List of Strings.

You know how to add an item to a List, right?
0
 
SunnyXAuthor Commented:
   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 ?
0
 
SunnyXAuthor Commented:
thx for the help
0
 
gurpsbassiCommented:
why are you passing in the string collection as the first argument:  transform(Collection<String> strCollection, Collection myCollection) {


You don't need to.
0
 
zzynxSoftware engineerCommented:
>> thx for the help
You're welcome.
Thanx 4 axxepting
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now