Java 8 lambda null check usage -
i have scenario checking multiple attributes inside class null check.if not null , calling method create me new object , need capture instance against reference.i succesasfully able null check using maps unable write code me in capturing return object after invocation.can please ?
    private workflowpreference buildwfprefdetails(ccarreportpreferenceconfig ccarreportpreferenceconfig) {     workflowpreference workflowpreference = new workflowpreference();     list<payloadentry> payloadentries = new arraylist<payloadentry>();     optional.of(ccarreportpreferenceconfig)         .map(ccarreportpreferenceconfig::getrwprole)         .map(rwprole::getrolename)         .ifpresent(s -> workflowpreference.setkey(s));     optional.of(ccarreportpreferenceconfig)         .map(ccarreportpreferenceconfig::getsequencenumber)         .ifpresent(s -> buildpayloadentry("seq_num", s));     optional.of(ccarreportpreferenceconfig)         .map(ccarreportpreferenceconfig::getslatype)         .ifpresent(s -> buildpayloadentry("sla_type", s));     optional.of(ccarreportpreferenceconfig)         .map(ccarreportpreferenceconfig::getslavalue)         .ifpresent(s -> buildpayloadentry("sla_value", s.tostring()));     optional.of(ccarreportpreferenceconfig)         .map(ccarreportpreferenceconfig::getrejectsequence)         .ifpresent(s -> buildpayloadentry("reject_sequence", s));     optional.of(ccarreportpreferenceconfig)         .map(ccarreportpreferenceconfig::getraisequerynotification)         .ifpresent(s -> buildpayloadentry("raise_query_notification", s));     workflowpreference.getvalue().addall(payloadentries);     return workflowpreference; }  private payloadentry buildpayloadentry(string key, string value) {     payloadentry payloadentry = new payloadentry();     payloadentry.setkey(key);     payloadentry.setvalue(value);     return payloadentry; } expected :
payloadentries list should capture returned object after each buildpayloadentry call.i want below implemented along lambda null checks
payloadentries.add(buildpayloadentry("seq_num", ccarreportpreferenceconfig.getsequencenumber())); payloadentries.add(buildpayloadentry("sla_type", ccarreportpreferenceconfig.getslavalue().tostring())); updated answer per below suggestions
optional.ofnullable(ccarreportpreferenceconfig)         .map(ccarreportpreferenceconfig::getrwprole)         .map(rwprole::getrolename)         .ifpresent(s -> workflowpreference.setkey(s));     optional.ofnullable(ccarreportpreferenceconfig)         .map(ccarreportpreferenceconfig::getsequencenumber)         .ifpresent(s -> payloadentries.add(buildpayloadentry("seq_num", s)));     optional.ofnullable(ccarreportpreferenceconfig)         .map(ccarreportpreferenceconfig::getslatype)         .ifpresent(s -> payloadentries.add(buildpayloadentry("sla_type", s)));     optional.ofnullable(ccarreportpreferenceconfig)         .map(ccarreportpreferenceconfig::getslavalue)         .ifpresent(s -> payloadentries.add(buildpayloadentry("sla_value", s.tostring())));     optional.ofnullable(ccarreportpreferenceconfig)         .map(ccarreportpreferenceconfig::getrejectsequence)         .ifpresent(s -> payloadentries.add(buildpayloadentry("reject_sequence", s)));     optional.ofnullable(ccarreportpreferenceconfig)         .map(ccarreportpreferenceconfig::getraisequerynotification)         .ifpresent(s -> payloadentries.add(buildpayloadentry("raise_query_notification", s)));     workflowpreference.getvalue().addall(payloadentries); 
if understand correctly, need transform (for example):
optional.of(ccarreportpreferenceconfig)         .map(ccarreportpreferenceconfig::getsequencenumber)         .ifpresent(s -> buildpayloadentry("seq_num", s)); to
optional.of(ccarreportpreferenceconfig)         .map(ccarreportpreferenceconfig::getsequencenumber)         .ifpresent(s -> payloadentries.add(buildpayloadentry("seq_num", s))); 
Comments
Post a Comment