3

I have the following classes:

class Employee 
{
    private String name;
    private List<Address> addresses;

    //...
    //Getters and setters for all fields
}

public class Address
{
    private String city;
    private Timestamp startDate;
    private Timestamp endDate;
    private String typeOfResidence;
    private String businessCode;
    private String businessType;


    //...
    //Getters and setters for all the fields
}

Now I have an employee object which has list of addresses. Based on businessCode, I need to populate businessType.

The businessCode is already populated.

I have a function

public String public getBusinessType(String businessCode){
...

business logic...

return businessType;
}

Now please help to update businessType field in each address element.

I am trying using

List<Address> l = employee.getAddress();

IntStream.range(0, l.size).forEach();

But not sure how to call getBusinessType for each address element and update the field in each address element.

1
  • 2
    employee.getAddress().forEach(a->a.setBusinessType(...)); Commented Apr 25, 2018 at 7:29

4 Answers 4

3

You could do it in place without the need to stream:

yourList.forEach(x -> {
   x.setBusinessType(YourClass.getBusinessType(x.getBusinessCode())) 
})
Sign up to request clarification or add additional context in comments.

2 Comments

You don't need to make it a block. A void method is a valid lambda expression for forEach().
@Andreas yup, I know that. I sometimes do that for clarity
2

The classic way using a for-each loop would be :

for(Address a :  employee.getAddress()){
    a.setBusinessType(getBusinessType(a.getBusinessCode()));
}

Using Streams it would be :

employee.getAddress().stream().forEach(a-> a.setBusinessType(getBusinessType(a.getBusinessCode())));

But (a good IDE would tell you that) the stream() is superfluous here; List.forEach() is sufficient:

employee.getAddress().forEach(a-> a.setBusinessType(getBusinessType(a.getBusinessCode())));

Comments

-2

Refer below line.

    employee.getAddresses().forEach(employee-> employee.setCity("My City"))

Comments

-2

Should be like this

employee.getAddress().stream().map(address->address.setBusinessType(getBusinessType(address.getBusinessCode())))
         .collect(Colletors.toList());

also you can use replaceAll method.

To readability create UnaryOperator<T> and then use this function in replaceAll method.

 UnaryOperator<Address> updateBusinessType = address -> {
        address.setBusinessType(...);
        return address;
    };

employee.getAddress().replaceAll(updateBusinessType);

It can also be improved by defining a method in the Address class that update businessType property.

2 Comments

replaceAll() requires a UnaryOperator<Address>, but setBusinessType does not return a modified Address object.
Sorry, I don't see the point of replacing all values in the list with the values that are already there. Using forEach() is much better. Using replaceAll() is just confusing, because we'd then sit there and try to figure out what you're replacing, and that's a waste of time, since you're not actually replacing the Address objects in the list. Or is that an exercise in code obfuscation. A bad one if so.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.