2

I'm wondering if there is an exit in the use of Optional.ofNullable()

for(String name: names){
  if(name != null)
     person = "Mr./Mrs."+name;
  else
      continue;
  greeting(person);
}

My code is like above. I want to check the argument before calling the function. I have similar codes in my class and to reduce complexity I want to use Optional.Nullable().

Is there a way to apply Optional.Nullable() in here? Or is there another way to help me to reduce complexity of multiple if checks to check null variables? Thanks in advance.

dreamcrash
  • 47,137
  • 25
  • 94
  • 117
Vincent
  • 21
  • 2

2 Answers2

3

My code is like above. I want to check the argument before calling the function. I have similar codes in my class and to reduce complexity I want to use Optional.Nullable().

You have several options depending on your given context:

Approach 1: Rearrange the conditionals

  for(String name: names){
        if(name != null) {
            person = "Mr./Mrs." + name;
            greeting(person);
            ... 
        }
    }

or :

    for(String name: names){
        if(name == null)
            continue;
        person = "Mr./Mrs." + name;
        greeting(person);
        ...
    }

Approach 2: Using Optional

for(String name: names){     
   Optional.ofNullable(name)
           .map("Mr/Mrs"::concat)
           .ifPresent(YourClass::greeting); 
}

the YourClass is just a place holder for the class where the method greeting was defined.

Approach 3: Using Streams

Assuming that names is a Collection, then you are actually better off using Java Streams, namely:

names.stream()
     .filter(Objects::nonNull)
     .forEach(name -> greeting("Mr./Mrs."+name));

If names is an array of Strings then you can do:

Arrays.stream(names)
      .filter(Objects::nonNull)
      .forEach(name -> greeting("Mr./Mrs."+name));
dreamcrash
  • 47,137
  • 25
  • 94
  • 117
  • What if I want to check it for a single variable? Is there a way to check a variable is null or not and if so, then make sure it doesn't enter this block? – Vincent Jan 29 '21 at 14:10
  • @Vincent Depends on the context really, you can do Optional.ofNullable(name).map(n -> ).map(p -> greeting()) (like suggested in the comments) or simply if(name != null){ person = "Mr./Mrs."+name; greeting(person);} – dreamcrash Jan 29 '21 at 14:14
2

You can involve Optional:

for (String name : names) {
  Optional.ofNullable(name).map(n -> "Mr/Mrs" + name).map(p -> greeting(p));
}

or Streams:

names.stream()
    .filter(Objects::nonNull)
    .map(n -> "Mr./Mrs." + name)
    .forEach(p -> greeting(p));

But there's really no advantage to doing either. It's certainly not more performant to do so.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • 3
    `.map(p -> greeting(p))` requires `greeting` to return a value. Why not use `.ifPresent(p -> greeting(p))`… And `.map(n -> "Mr./Mrs." + name)` uses a different names (`n` and `name`). When you use `.map("Mr./Mrs."::concat)` instead, you don’t need to deal with names at all. But generally, `if(name != null) greeting("Mr./Mrs." + name);` still is simpler than both approaches. – Holger Jan 29 '21 at 20:19