I was having some problem when trying to get the first array item out of Optional. Following as my code:
String[] temp = new String[2]; temp[0] = "email1"; temp[1] = "email2"; Optional<String[]> email = Optional.of(temp); System.out.println(email.map(e -> e.get(0)).orElse(null)); I am trying to get the first email, otherwise just return as null. However, when I run this, I am getting these error messages:
System.out.println(email.map(e -> e.get(0)).orElse(null)); ^ symbol: method get(int) location: variable e of type String[] 1 error When I tried to do this:
System.out.println(email.stream().findFirst().get()); It prints out weird value:
[Ljava.lang.String;@7adf9f5f Any ideas? Thanks!
maplambda), then the most natural way to represent that could just be a "naked"String[](not wrapped in an Optional), which you then check fortemp.length > 0. (You can search "java collection optional" for more details and insights.)