How to get an object with the Optional interface?

0

I try to obtain an object from the Person class (class already created) but I want to obtain it with the Optional interface, because the object could or could not be found in a Set of People. The method would be like this:

public Optional<Persona> getPersona(Persona pe) 
{
    Optional<Persona> user = Optional.ofNullable(null);

    Predicate<Persona> ver = umano -> 
    {       
        boolean b = false;
        for(Persona i: personas) // personas es el Set de personas ya creado
        {
            if(i.equals(pe)) 
            b = true;
        }
        return b;
    };
    user.filter(ver);
    if(user.isPresent())
        user.get();
    return user;
}
    
asked by Kevin Velasco 10.05.2017 в 09:57
source

0 answers