以下代码的结果:
public class ListIntegerDemo1 {
public static void addToList(List list) {list.add("0067");list.add("bb");}
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
addToList(list);
System.out.println(list.get(0));
}
}
Run Code Online (Sandbox Code Playgroud)
是"0067".
我本来期望RuntimeException或类似的,因为我将一个String添加到整数列表.
为什么它没有任何问题?
我正在研究Java Optional并测试一些超出标准用途的用例.
好的,我们来看看这个例子:
public void increment(Integer value) {
if (currentValue != null && endValue != null && currentValue < (endValue - value)) {
currentValue+=value;
}
}
Run Code Online (Sandbox Code Playgroud)
currentValue和endValue是Integer.
上面的例子可以使用Optional在Java8中转换吗?
我在想这样的事情:
public void increment(Integer value) {
currentValue.filter(a-> a < (endValue.get()-value)).map(???);
}
Run Code Online (Sandbox Code Playgroud)
其中currentValue和endValue是 Optional<Integer>
我实际上坚持使用.map函数.
我要感谢任何建议,谢谢
以下代码应确保在所有线程之间同步对"sync"的访问.
根据输出结果并非总是如此,请注意Thread-3和Thread-4如何读取相同的sync值.
我在代码中遗漏了什么吗?
[Thread-0] before value of sync is 0
[Thread-0] after value of sync is 1
[Thread-3] before value of sync is 1
[Thread-3] after value of sync is 2
[Thread-4] before value of sync is 1
[Thread-4] after value of sync is 3
[Thread-2] before value of sync is 3
[Thread-2] after value of sync is 4
[Thread-1] before value of sync is 4
[Thread-1] after value of sync is 5
Run Code Online (Sandbox Code Playgroud)
这里的代码:
package com.mypackage.sync;
public class LocalSync implements …Run Code Online (Sandbox Code Playgroud) 我正在检查一些简单的Lambda表达式,我在互联网上找到了以下示例,该示例应该可行.
可惜的是,它似乎的NetBeans 8.0.1不喜欢行的代码 (Person p) -> p.getAge() < 18)和.map((Person p) -> p.getName())
显示错误"lambda表达式中的不兼容参数类型".
任何人都有一个想法如何纠正这个问题?
public class Lambda {
private enum Gender {
MALE, FEMALE
};
public static class Person {
private final String name;
private final int age;
private final Gender gender;
public Person(String name, int age, Gender gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public Gender …Run Code Online (Sandbox Code Playgroud)