Advertisement
cardel

Ejemplo optional

Dec 3rd, 2022
659
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.60 KB | None | 0 0
  1. import java.util.Optional;
  2.  
  3. class Main {
  4.  
  5.     int a;
  6.     int b;
  7.    
  8.   public static void main(String[] args) {
  9.         Main objMain = new Main();
  10.        
  11.         Arbol objArbol = new Arbol(
  12.             null,
  13.             new Arbol(null,null,4),
  14.             6
  15.         );
  16.  
  17.         Optional.ofNullable(objArbol.izq).ifPresent(x -> objMain.a = x.value);
  18.         Optional.ofNullable(objArbol.der).ifPresent(x -> objMain.b = x.value);
  19.  
  20.         System.out.println("a " + objMain.a + " b " + objMain.b);
  21.        
  22.         }
  23. }
  24.  
  25. class Arbol {
  26.     Arbol izq;
  27.     Arbol der;
  28.     int value;
  29.     Arbol (Arbol izq, Arbol der, int value) {
  30.         this.izq = izq;
  31.         this.der = der;
  32.         this.value = value;
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement