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
49 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 500 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 15

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Article by: Nadia
Linear search (searching each index in an array one by one) works almost everywhere but it is not optimal in many cases. Let's assume, we have a book which has 42949672960 pages. We also have a table of contents. Now we want to read the content on p…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now