英文:
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] = "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!
答案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 -> 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
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≪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
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/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论