1

How to change the below code to remove if-else and use Java8 Optional instead

public class IfTest { public static void main(String[] args) { String foodItem = "Apple"; if(foodItem.equals("Apple") || foodItem.equals("A")) foodItem = "Fruit"; else if(foodItem.equals("Potato") || foodItem.equals("P")) foodItem = "Vegetable"; else foodItem = "Food"; System.out.println(foodItem); } } 
4
  • 5
    Why do you want to do that? Optional is clearly a bad idea here. Maybe you want a switch expression. Commented Mar 26, 2021 at 5:55
  • I wanted to know if there is any Java8 feature that can do away with if-else or switch Commented Mar 26, 2021 at 5:57
  • 1
    @astar you could use a Map, using Java 8-added getOrDefault. It's just not obvious why you would use the right tool for the job (if/else or switch). Commented Mar 26, 2021 at 6:00
  • Checkout this baeldung.com/java-replace-if-statements, it may help you Commented Mar 26, 2021 at 6:05

2 Answers 2

3

Optional isn't a general-purpose replacement for if/else. It is not a good choice here.

I think you could contrive to use Optional something like this:

Optional.of(foodItem) .map(f -> f.equals("Apple") || f.equals("A") ? "Fruit" : f) .map(f -> !f.equals("Fruit") && (f.equals("Potato") || f.equals("P")) ? "Vegetable" : f) .filter(f -> !f.equals("Fruit") && !f.equals("Vegetable")) .orElse("Food"); 

which is just a total unreadable mess.

An alternative would be switch: this is better because it doesn't search through all the cases linearly, but rather jumps to the matching one:

switch (foodItem) { case "Apple": case "A": foodItem = "Fruit"; break; case "Potato": case "P": foodItem = "Vegetable"; break; default: foodItem = "Food"; } 

Or a switch expression (in Java 12+):

foodItem = switch (foodItem) { "Apple", "A" -> "Fruit"; "Potato", "P" -> "Vegetable"; default -> "Food"; } 

If you want to use a feature added in Java 8, you can create a Map:

Map<String, String> map = new HashMap<>(); map.put("Apple", "Fruit"); map.put("A", "Fruit"); map.put("Potato", "Vegetable"); map.put("P", "Vegetable"); 

And then use map.getOrDefault(foodItem, "Food"). That's basically just a dynamic form of the switch.

Sign up to request clarification or add additional context in comments.

Comments

0

Optional is not a good replacement for a chain of if-else if-else statements. You might want to perform a look-up in a hash-based data structure such as HashSet inside a class for the given food group:

@Getter @AllArgsConstructor // basically getters and all-args constructor public class FoodGroup { String name; Set<String> items; } 
List<FoodGroup> list = List.of( // I use Java-9+ static method new FoodGroup("Fruit", Set.of("Apple", "A")), // for Java 8, use Arrays.asList(..) new FoodGroup("Vegetable", Set.of("Potato", "P"))); String foodItem = "Apple"; String result = list.stream() .filter(group -> group.getItems().contains(foodItem)) .map(FoodGroup::getName) .findFirst() .orElse("Food"); 

This solution might be an overkill for such a simple use-case, however, this solution is scalable.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.