# Optional

Optional

**Data.java**

```
public class Data {
    private String id;
    private String data;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

}
```

**Procesar.java**

```
public class Procesar {
    public Optional<Data> procesar(String id){
        Data d = new Data();
        try {
            if(id.equals("a")){
              d.setId("a");
            d.setData("Aveces");  
            }else{
              return Optional.empty();  
            }


        } catch (Exception e) {
            System.out.println("Error "+e.getLocalizedMessage());
        }
        return Optional.of(d);        
    }
}
```

Start.java

public class Start {

```
/\*\*

 \* @param args the command line arguments

 \*/

public static void main\(String\[\] args\) {

    // TODO code application logic here

    try {

        Procesar p = new Procesar\(\);

        Optional&lt;Data&gt; d =p.procesar\("a1"\);



       if\(d.isPresent\(\)\){

         Data d1=  d.get\(\);

           System.out.println\("esta presente"+d1.getData\(\)\);



       }else{

           System.out.println\("No esta presente"\);

       }

    } catch \(Exception e\) {

        System.out.println\("main\(\) "+e.getLocalizedMessage\(\)\);

    }

}
```

}
