-1

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!

2
  • 2
    Semi-related, but you should ask yourself how an empty Optional is different from a zero-element array. Are you actually treating them differently? It's definitely possible you are — but if all of your non-empty Optionals are of arrays with at least one element (as it sounds like they are, since you're not checking the array length within your map lambda), then the most natural way to represent that could just be a "naked" String[] (not wrapped in an Optional), which you then check for temp.length > 0. (You can search "java collection optional" for more details and insights.) Commented Feb 18, 2023 at 5:43
  • Oh, and as for "it prints out a weird value": stackoverflow.com/q/29140402/1076640 Commented Feb 18, 2023 at 6:00

2 Answers 2

1

Arrays don't really have methods, per se. .get is something you call on a Collection, not a primitive array. Since the Optional contains an actual, primitive Java array, just use brackets [] to access the element.

System.out.println(email.map(e -> e[0]).orElse(null)); 
Sign up to request clarification or add additional context in comments.

8 Comments

Is there any way I can use the second approach to get the value? the .stream().findFirst()
I'm not sure I understand what you're trying to do with a stream. an Optional is a stream of zero or one objects. .stream doesn't return a stream of the underlying data, it returns a stream whose element is the underlying data. You could flatten the nested stream, but that's far less clear than just getting the element from the array in the first place.
It sounds like you may want Streams (Stream.of(temp)) rather than Optionals.
I think you're misunderstanding the point of Optional. If you want to stream over some data, then you want Stream.of. Optional isn't a magic wand you wave to eliminate the null problem in Java.
Optional.of(temp).filter(a -> a.length != 0).map(e -> e[0]) or Stream.ofNullable(temp).flatMap(Arrays::stream).findFirst() In either case, there’s no need to switch between Optional and Stream API. And there’s no point in using an Optional “because it may be null”, followed by calling get() unconditionally.
|
0

An Optional works alike an if-else test but lay out inside a special object to carry a value and make comparison to an equivalent

You put an "array" as a value into the Optional, the only object get() could return is the array not any of it's elements, also, get() for Optional does not take an argument in it.

The isPresent() boolean and the void ifPresent(ConsumerAndItsValue cmv) methods are a test to find if the "VALUE" is present, works much more like comparing using if object.equals(this object)

So of you want to use it for particular email addresses you simply put in each string , the tests cannot see into the array, those elements are more objects.

Create a java.util.Consumer≪Anobject> the functional code assigned "to a lambda", Anobject should be the type in accept method accept(T to) method. Here's a stack overflow page I found Proper usage of Optional.ifPresent()

And it is possible to iterate over an array contents (external site example). https://mkyong.com/java8/java-8-consumer-examples/

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.