获取 Optional 中的第一个数组项。

huangapple go评论95阅读模式
英文:

Java Get First Array Item from Optional

问题

以下是您的代码翻译部分:

我在尝试从Optional中获取第一个数组项时遇到了一些问题以下是我的代码

String[] temp = new String[2];
temp[0] = "email1";
temp[1] = "email2";
Optional<String[]> email = Optional.of(temp);
System.out.println(email.map(e -> e[0]).orElse(null));

我尝试获取第一个电子邮件否则返回null然而当我运行这段代码时我收到以下错误消息

System.out.println(email.map(e -> e[0]).orElse(null));
                            ^
符号   方法get(int)
位置 类型为String[]的变量e
1个错误

当我尝试这样做时

System.out.println(email.stream().findFirst().get());

它打印出奇怪的值

[Ljava.lang.String;@7adf9f5f

有什么想法吗谢谢
英文:

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] = &quot;email1&quot;;
temp[1] = &quot;email2&quot;;
Optional&lt;String[]&gt; email = Optional.of(temp);
System.out.println(email.map(e -&gt; 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 -&gt; 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!

答案1

得分: 1

.get 是在 Collection 上调用的方法,而不是原始数组上调用的方法。由于 Optional 包含一个实际的原始 Java 数组,只需使用方括号 [] 来访问元素。

System.out.println(email.map(e -> e[0]).orElse(null));
英文:

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 -&gt; e[0]).orElse(null));

答案2

得分: 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 if 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 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
https://stackoverflow.com/questions/24228279/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/

英文:

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&Lt;Anobject&gt; 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
https://stackoverflow.com/questions/24228279/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/

huangapple
  • 本文由 发表于 2023年2月18日 13:28:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491399.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定