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
35 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

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…
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 learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

747 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

9 Experts available now in Live!

Get 1:1 Help Now