Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. param = foo.getSomthing1().getSomthig2().getSomthing3() ...
  2.  
  3. if (foo != null && foo.getSomthing1() != null && ....) {
  4. param = foo.getSomthing1().getSomthig2().getSomthing3() ...
  5. }
  6.  
  7. param = foo?.getSomthing1()?.getSomthig2()?.getSomthing3() ...
  8.  
  9. public class TestClass{
  10.  
  11. public Optional<TestClass> getSomthing1(){
  12. return Optional.of(this);
  13. }
  14.  
  15. public Optional<TestClass> getSomthing2(){
  16. return Optional.of(this);
  17. }
  18. }
  19.  
  20. TestClass test = new TestClass();
  21. test.getSomthing1().ifPresent(t -> t.getSomthing2());
  22.  
  23. private Optional<String> get(First first) {
  24. return Optional.ofNullable(first.getSecond())
  25. .map(Second::getThird)
  26. .map(Third::getFourth)
  27. .map(Fourth::getString);
  28. }
  29.  
  30. get(something).orElse("is null")
  31.  
  32. Object fooMethod(Object fooObject) {
  33. if (fooObject == null)
  34. return null;
  35. else
  36. return fooMethod(fooObject.getSomething());
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement