Reputation: 815
Consider I have a VO Employee with following fields.
Employee
EmployeeId
EmployeeName
EmployeeDOB
EmployeeAddress
I have a list of Employee VOs, say EmployeeList.
I want to create another list of all the EmployeeIds in the EmployeeList.
I know the most common way is to iterate EmployeeList and add one by one to EmployeeIdList, but is there any way to do it in single shot ?
Here is the current code.
List<Employee> employeeList = new ArrayList<Employee>();
populate(employeeList); //this will add EmployeeVOs to the list
List<String> employeeIdList = new ArrayList<String>();
for(Employee emp:employeeList)
{
employeeIdList.add(emp.getEmployeeId()) ;
}
I want to know if there is a way to add all the employeeIds without the iteration of employeeList like above.
I don't want to change the existing populate method since its happening in EJB and is used by many other classes as well.
Upvotes: 3
Views: 2541
Reputation: 7858
Well, there is a way to transform the original List<Employee>
into a List<String>
one without iterating it. My approach is usually using the Guava Collections framework:
List<Employee> employeeList = new ArrayList<Employee>();
populate(employeeList);
List<String> employeeIdList = Lists.transform(employeeList, new Function<Employee, String>() {
public String apply(Employee input) {
Preconditions.checkNotNull(input);
return input.getEmployeeId();
}
});
Guava doesn't iterate through the original list at that point yet, it wraps it into another different list and performs the transformation on the fly when the elements are accessed. Problem is: you can't perform additions/insertions in the result list and, at some point, you'll need to iterate through it.
Upvotes: 2
Reputation: 530
Try LambdaJ:
List<String> employeeIds = extract(employeeList,on(Employee.class).getEmployeeId());
I've been using the project for a while and while it has a bit of a learning curve, I've really come to appreciate it.
Note that those method calls (extract
and on
) are static methods in the Lambda
class so you'd do a static import on that class so the code looks pretty.
Upvotes: 4
Reputation: 8323
AFAIK there isn't.
2 things however. You could populate the id list at the same time you populate the VO one, this will avoid you an additional iteration on the VO List.
If you remain on the way presented here, i would advise to provide the list size at initialization of the id list, to avoid multiple reallocation of the underlying array in the ArrayList.
List<String> employeeIdList = new ArrayList<String>(employeeIdList.size());
To optimize Collection manipulation you should consider operations done on this collection to choose the more appropriate implementation. (add / remove operation mainly => LinkedList, random access (get(n)) => arrayList, contains => Set, etc...)
Upvotes: 0
Reputation: 6870
You could use a Map with the ID as key, this way you will have one collection and you can add both in populate(employeeList)
in one iteration. What you want (adding id one-liner) sounds like a job for lambdas in java8.
Upvotes: 3
Reputation: 336
No matter if there is any other way or not, but eventually it will be done via iteration only.
Further, I don't think there is any direct API that can help you in achieving that.
So, whatever you are doing is the best way of doing this.
And one more thing, you can do it while populating Employee VOs. At that time you can either create a Map or a separate List for employee ids.
But again, it will be just an optimization.
Upvotes: 0