I have this code:
if (myClass != null){ myClass2.setId(myClass.getId()); myClass2.setName(myClass.getName()); myClass2.setPosition(myClass.getPosition()); }
I want to replace this code to this:
Optional.ofNullable(myClass)
and map this field from myClass to myClass2
How i can do it?
Advertisement
Answer
Did you mean to use:
Optional.ofNullable(myClass) .ifPresent(m -> { myClass2.setId(m.getId()); myClass2.setName(m.getName()); myClass2.setPosition(m.getPosition()); });
For mapping I would suggest to read about MapStruct, it is a good tool.