Thanks a lot for all the answers/comments!
Apologize for the bad code sample I put in the original question. I was trying to simpify my question but apprently it ended up rather confusing.
I changed the code a little bit:
Map<String, Boolean> map1 = new HashMap<>();
map1.put("Yes", Boolean.FALSE);
map1.put("No", Boolean.FALSE);
Map<String, Map<String, List<String>>> map2 = new HashMap<>();
List<String> list1 = Arrays.asList("Apple", "Peach");
List<String> list2 = Arrays.asList("Pear", "Orange");
Map<String, List<String>> innerMap = new HashMap<>();
innerMap.put("fruit1", list1);
innerMap.put("fruit2", list2);
map2.put("Fruits", innerMap);
map2.put("Vege", new HashMap<>());
Optional<? extends Entry<String, ? extends Object>> optional = Stream
.of(map1.entrySet().stream().filter(e -> e.getValue()).findFirst(),
map2.entrySet().stream().filter(entry -> entry.getValue().entrySet()
.stream()
.anyMatch(e -> e.getKey().equals("Fruit") && e.getValue().stream().anyMatch(
i -> i.equals("Pear"))))
.findFirst())
.filter(Optional::isPresent).map(Optional::get).findFirst();
optional.orElse(new AbstractMap.SimpleEntry<>("NULL", null));
The I got the first error:
Cannot infer type arguments for AbstractMap.SimpleEntry<>
So I change the last line to:
optional.orElse(new AbstractMap.SimpleEntry<String, Object>("NULL", null));
Then a new error pops up on orElse:
The method orElse(capture#4-of ? extends Map.Entry) in the type Optional> is not applicable for the arguments (AbstractMap.SimpleEntry)
As I understand, both Map have the same Key type but different Value types. The filter for each map is also different. So the returned would be an Optional with an Entry of String type Key but a generic type Value.
My original problem was I didn't know how to give a default value for this Optional since the Entry's Value type is generic (which I still don't know the answer now).
Inspired by @Ole, I refactored the code like this:
String result = Stream
.of(map1.entrySet().stream().filter(e -> e.getValue()).map(Map.Entry::getKey).findFirst(), map2
.entrySet().stream().filter(entry -> entry.getValue().entrySet()
.stream()
.anyMatch(e -> e.getKey().contains("fruit") && e.getValue().stream().anyMatch(
i -> i.equals("Pear"))))
.map(Map.Entry::getKey)
.findFirst())
.filter(Optional::isPresent).map(Optional::get).findFirst().orElse("NULL");
System.out.println("result: " + result);
while I only collect the filtered Entry's Key into a stream and JAVA compiler seems working well. I get the exuection result as
result: Fruits
But it seems I'm still overcomplicating things here...