1

I have a POJO class called Employee with empName & Salary. I have a collection of Employee as a object with employee details. Now i want to reset the value of particular field to some common value. In this case i want to assign salary to "0" for all employee items in the list.

List<Employee> empList;

public class Employee{
    private String empName = "";
    private int empSalary = "";
}

Instead of iterating through the list of employee using For/Foreach and assigning the required value for all items in list.

Is there any easy or efficient way to achieve the same using any CollectionUtils (Apache commons or any other).

Donato Szilagyi
  • 4,279
  • 4
  • 36
  • 53
Karthikeyan
  • 506
  • 2
  • 8
  • 20

3 Answers3

5

As of Java 7, there is no neat way for 'bulk'-updates on collections. Hopefully this will get better with Java 8, but for now I suggest looking at libraries like for example guava: http://code.google.com/p/guava-libraries/wiki/FunctionalExplained

rethab
  • 7,170
  • 29
  • 46
2

Salary being a class variable will automatically be initialized to its default value which is 0. This happens as soon as the object is created irrespective of the fact that it is in some collection or not.

If you want some other common value you can initialize it appropriately in constructor.

If you want to modify the properties of an object after it has been created and added to a collection, then there is no other way apart from iterating the collection and explicitly setting the values.

prashant
  • 1,805
  • 12
  • 19
  • Thanks for your answer, i was looking for alternative approach to modify the values after adding elements. As list might have 500+ elements in it. Iterating using for/foreach and assigning seems bit costly. – Karthikeyan Apr 23 '13 at 09:02
  • 1
    In my opinion whatever language/paradigm/approach you follow, If you want to process each element in a collection you would always need to iterate over them individually. At best what another library/approach can do is to make your code more succinct/readable (by maybe hiding this from you) etc. I don't think you can gain on performance in this case. – prashant Apr 23 '13 at 09:45
2

I believe you can do this with Commons Collections (which is what you suggest to use):

CollectionUtils.forAllDo(empList, new Closure() {
    public void execute(Object empObject) {
        Employee emp = (Employee)empObject;
        emp.setEmpSalary(salaryValueToSet);
    }
});
NilsH
  • 13,705
  • 4
  • 41
  • 59
  • Is this approach more efficient than a simple for/foreach loop? – Drogba Apr 23 '13 at 07:48
  • I wonder why it got 2 up votes with a more complicated approach. – Drogba Apr 23 '13 at 07:50
  • 1
    True. He gave an impression that he wanted to solve it with Commons Collection. So I provided an example. But I agree that it's not a simpler solution. It just shows that it is possible. – NilsH Apr 23 '13 at 07:54
  • What version of Commons Collections do you use? Can't seem to find forAllDo or anything similar on ClosureUtils in 3.2.1 or 4.0-SNAPSHOT. – Simon Hellinger Apr 23 '13 at 08:06
  • @NilsH: Thanks for your answer. Also, I was looking for some options like CollectionUtils, but not CollectionUtils only. So is there any other way, because the list might have 500 elements in it. manually iterating & assigning value seems bit costly. – Karthikeyan Apr 23 '13 at 08:57
  • @Karthikeyan Why do you think iterating the list and assigning value is costly but using CollectionUtils isn't? Take a look at CollectionUtils.forAllDo source code. It is actually an iteration of the list and triggers the execute method which is assigning value to your Object. – Drogba Apr 23 '13 at 09:22
  • 1
    Karthikeyan - No, all solutions would require som sort of iteration. I wouldn't be too worried about iterating 500 elements though. @Drogba He's not saying CollectionUtils is "cheaper". I think we all agree that it's just another way of iterating. – NilsH Apr 23 '13 at 09:26