Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2016-09-29
5
Medium Priority
?
87 Views
Last Modified: 2016-09-29
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
Comment
Question by:SunnyX
  • 2
  • 2
5 Comments
 
LVL 37

Accepted Solution

by:
zzynx earned 2000 total points
ID: 41821461
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
 

Author Comment

by:SunnyX
ID: 41821501
   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
 

Author Closing Comment

by:SunnyX
ID: 41821506
thx for the help
0
 
LVL 16

Expert Comment

by:gurpsbassi
ID: 41821530
why are you passing in the string collection as the first argument:  transform(Collection<String> strCollection, Collection myCollection) {


You don't need to.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 41821538
>> thx for the help
You're welcome.
Thanx 4 axxepting
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Iteration: Iteration is repetition of a process. A student who goes to school repeats the process of going to school everyday until graduation. We go to grocery store at least once or twice a month to buy products. We repeat this process every mont…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question