Doubt with use of Optional

Asked

Viewed 187 times

3

I am trying to use this optional class to get the data with null, but returns error.

In the main class, I use the for with this method. Profession and composition of persons.

for(Pessoa p : pessoas) {

        Optional<Profissao> temNome = p.profissao.getNomeP();

Using as a String

Optional<String> temNome =Optional.ofNullable(p.profissao.getNomeP());

works, but I didn’t understand the difference

  • If you do so p.profissao.ifPresent(prof -> System.out.println(prof.getNomeP())); shows the name?

  • The ifPresent option does not appear, Voce meant ? temNome.ifPresent() { }

  • Actually I understood your doubt now. Your second option works because you are calling last the getNomeP that returns a String and not a profession

2 answers

2

Hello!

As far as I’m concerned, you’re confusing it. See, you are accessing the name of the profession, a string, and are waiting for it to return the parent object, profession.

for(Pessoa p : pessoas) {
   Optional<Profissao> temNome = p.profissao.getNomeP();
   ...
}

Right here would be:

for(Pessoa p : pessoas) {
   Optional<Profissao> possivelProfissao = Optional.ofNullable(p.profissao);
   possivelProfissao.ifPresent( profissao -> System.out.println(p.getNomeP()) );
   ...
}

Or:

for(Pessoa p : pessoas) {
   Optional<Profissao> possivelProfissao = Optional.ofNullable(p.profissao);
   if(possivelProfissao.isPresent()){
      String nome = possivelProfissao.get().getNomeP();
      System.out.println(nome );
   }
   ...
}

I recommend a read on this article from Caelum. It provides a legal basis. To documentation, although well completed, there will be no didactic mto Any questions, just ask.

Edition 01

If you want to return a standard string when there is no profession, you can do something like this:

public String temNome(Profissao profissao) {
  return Optional.ofNullable(profissao.getNomeP()).orElse("Nao tem Profissao");
}

The coolest thing is that you do in your person class, writing a getNomeProfissao(), like this:

public class Pessoa{
   ...
   public String getNomeProfissao(){
      return Optional.ofNullable(this.profissao.getNomeP()).orElse("Nao tem Profissao");
   }
   ...
}

So you will follow Demeter’s Law, and will not chain calls.

0

I’ve created a method, I think you’re right

public  String temNome(Profissao profissao) {

        Optional<String> nomeProfissao = Optional.ofNullable(profissao.getNomeP());

        if(nomeProfissao.isPresent()) {

            return profissao.getNomeP();
        }  

        return "Nao tem Profissao";

    }
  • To be what you want is to know if there is a profession, the most polite is to use the "nomeProfissao.isPresent()" and leave the decision-making to the top layer, leaving more cohesive. What you can do is encapsulate in your person class the method temProfissao(), and in this vc returns a Boolean. in the case of the method you posted, do so: public String temNome(Professional) { Return Optional.ofNullable(professional.getNomeP()). orelse("Not Professional"); }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.