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.
getOrDefault.It's just not obvious why you would use the right tool for the job (if/else or switch).