Arya
Arya

Reputation: 8995

Initialize new object and set values with Java 8

Right now I am passing to the following way, by creating the object first, calling the methods to set the values, then passing it to another object method.

To to = new To();
to.setEmail("[email protected]");
to.setName("John Smith");

Headers headers = new Headers();
headers.setTo(Arrays.asList(to));

Is it possible to do everything I am doing above related to the to object while calling headers.setTo?

Headers headers = new Headers();
headers.setTo(Is it possible to Initialize and set the "To" values in here?);

Upvotes: 1

Views: 13609

Answers (6)

Nishit Charania
Nishit Charania

Reputation: 93

As much I can understand, you are looking for Lambda to create a list of the object. As Arrays.asList is allowing Object (public static List asList(T... a)), we can not pass a function to that.

I can see one workaround by writing a new method and pass Supplier to that. This way you can write your own Supplier while using that method.

i.e:

List<To> tos = createList(()->{
  To to = new To();
  to.setEmail("[email protected]");
  to.setName("John Smith");
  return to;
},()->{
  To to = new To();
  to.setEmail("[email protected]");
  to.setName("John Smith");
  return to;
});


private <T> List<T> createList(Supplier<T>... suppliers)
{
    return Arrays.stream(suppliers).map(supplier -> {
        return supplier.get();
    }).collect(Collectors.toList());
}

Upvotes: 0

Yassin Hajaj
Yassin Hajaj

Reputation: 21995

Since , you can use To as a record which will make it immutable by default too

public record To(String email, String name) {

}

Headers headers = new Headers();
headers.setTo(new To("[email protected]", "John Smith"));

Upvotes: 0

Danny
Danny

Reputation: 541

Unless you can add a constructor with input parameters for the To class the only option (and a good one) is the factory/builder pattern.

Define this:

public class ToBuilder {
    public static To createTo(String email, String name) {
        To to = new To();
        to.setEmail(email);
        to.setName(name);
        return to;
    }
}

and then you can do:

Headers headers = new Headers();
headers.setTo(ToBuilder.createTo("[email protected]","John Smith"));

Upvotes: 0

Jude Niroshan
Jude Niroshan

Reputation: 4460

I think you are not using com.sun.net.httpserver.Headers. If you are having your own customized Headers class and To class, you can try the below modification.

class Headers{
    //....Other variables, getters & setters
    List<? extends To> to = new ArrayList<>();

    public void setTo(Supplier<List> toSupplier){
        toSupplier.get().foreach(to::add);
    }
}

//Add a new constructor to To class which accept the name & email. Then you can re-write it as follows:

Headers headers = new Headers();
Supplier<List> toSupplier = () -> Arrays.asList(new To("John Smith","[email protected]"));
headers.setTo(toSupplier);

OR

Headers headers = new Headers();
headers.setTo(() -> Arrays.asList(new To("John Smith","[email protected]")));

Upvotes: 0

IUnknown
IUnknown

Reputation: 83

It is possible only if there is a constructor in the class To that takes the mentioned arguments like this:

public class To {

    private String Email;
    private String Name;

    // The constructor
    public To(String Email, String Name) {
        this.Email = Email;
        this.Name = Name;
    }

    // ... more code ...
}

Then your call could look like:

Headers headers = new Headers();
headers.setTo(Arrays.asList(new To("[email protected]","John Smith")));

Upvotes: 0

J&#252;rgen
J&#252;rgen

Reputation: 418

Not with the POJO approach. Either you create a constructor in To having all fields you need as parameters or go for the builder pattern.

Upvotes: 2

Related Questions