假设我有两个接口:
public interface I1
{
default String getGreeting() {
return "Good Morning!";
}
}
public interface I2
{
default String getGreeting() {
return "Good Afternoon!";
}
}
Run Code Online (Sandbox Code Playgroud)
如果我想实现它们,将使用什么实现?
public class C1 implements I1, I2
{
public static void main(String[] args)
{
System.out.println(new C1().getGreeting());
}
}
Run Code Online (Sandbox Code Playgroud) 在Java 8中,您可以返回Optional而不是a null.Java 8文档说可选是"容器对象,可能包含也可能不包含非空值.如果存在值,isPresent()将返回true,get()将返回值."
在实践中,为什么这有用?此外,是否有任何null优先使用的情况?性能怎么样?
使用Java 8 Stream从Collection中查找与Property值匹配的对象.
List<Person> objects = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)
人员属性 - >姓名,电话,电子邮件.
迭代人员列表并找到匹配电子邮件的对象.看到这可以通过Java 8流轻松完成.但那还会收回一个系列吗?
例如:
List<Person> matchingObjects = objects.stream.
filter(p -> p.email().equals("testemail")).
collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
但我知道它总会有一个独特的对象.我们可以做一些事情,而不是Collectors.toList直接得到实际的对象.而不是获取对象列表.
我注意到使用Java 8方法引用的未处理异常有些奇怪.这是我的代码,使用lambda表达式() -> s.toLowerCase():
public class Test {
public static void main(String[] args) {
testNPE(null);
}
private static void testNPE(String s) {
Thread t = new Thread(() -> s.toLowerCase());
// Thread t = new Thread(s::toLowerCase);
t.setUncaughtExceptionHandler((t1, e) -> System.out.println("Exception!"));
t.start();
}
}
Run Code Online (Sandbox Code Playgroud)
它打印"Exception",所以它工作正常.但是当我Thread t改为使用方法引用时(甚至IntelliJ建议):
Thread t = new Thread(s::toLowerCase);
Run Code Online (Sandbox Code Playgroud)
异常没有被捕获:
Exception in thread "main" java.lang.NullPointerException
at Test.testNPE(Test.java:9)
at Test.main(Test.java:4)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Run Code Online (Sandbox Code Playgroud)
有人能解释一下这里发生了什么吗?
与Java-8 I可以很容易地处理一个String(或任何CharSequence),为IntStream使用任一chars或codePoints方法.
IntStream chars = "Hello world.".codePoints();
Run Code Online (Sandbox Code Playgroud)
然后我可以操纵流的内容
IntStream stars = chars.map(c -> c == ' ' ? ' ': '*');
Run Code Online (Sandbox Code Playgroud)
我一直在寻找一种整洁的方式来打印结果,我甚至找不到一个简单的方法.如何将这个ints流放回一个可以像我一样打印的形式String.
从上面stars我希望打印
***** ******
Run Code Online (Sandbox Code Playgroud) 我理解@Native注释的使用.
指示可以从本机代码引用定义常量值的字段.注释可以用作生成本机头文件的工具的提示,以确定是否需要头文件,如果需要,它应该包含哪些声明.
然而,在阅读Java源代码我注意到,在阶级Integer和Long的SIZE常数@Native,而它不是浮动,字节,双,短期和字符.
请注意,SIZE常量表示用于表示实际值的位数.
public static final int SIZE = 8;//Byte
public static final int SIZE = 16;//Character
public static final int SIZE = 16;//Short
public static final int SIZE = 32;//Float
@Native public static final int SIZE = 32;//Integer
@Native public static final int SIZE = 64;//Long
public static final int SIZE = 64;//Double
Run Code Online (Sandbox Code Playgroud)
编辑:我刚刚注意到,这也适用于MAX_VALUE和MIN_VALUE同一类的.
编辑2:我有空闲时间对此进行一些研究,并查看Long,Float等类的头文件,我希望找出常量不存在于其他头文件中,但不幸的是它们是.
static const jint SIZE = 8L;//java/lang/Byte.h
static …Run Code Online (Sandbox Code Playgroud) 我有以下内容Stream:
Stream<T> stream = stream();
T result = stream.filter(t -> {
double x = getX(t);
double y = getY(t);
return (x == tx && y == ty);
}).findFirst().get();
return result;
Run Code Online (Sandbox Code Playgroud)
但是,并不总是有一个结果给我以下错误:
NoSuchElementException:没有值存在
那么null如果没有价值,我怎么能回来?
我已经快速阅读了Oracle Lambda Expression文档.
这种例子帮助我更好地理解了:
//Old way:
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
for(Integer n: list) {
System.out.println(n);
}
//New way:
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
list.forEach(n -> System.out.println(n));
//or we can use :: double colon operator in Java 8
list.forEach(System.out::println);
Run Code Online (Sandbox Code Playgroud)
不过,我不明白为什么会有这样的创新.它只是一种在"方法变量"结束时死亡的方法,对吗?为什么我应该使用它而不是真正的方法?在性能方面哪个是更好的选择.Lambda或简单的循环.
我想用Lambda对列表进行排序:
List<Message> messagesByDeviceType = new ArrayList<Message>();
messagesByDeviceType.sort((Message o1, Message o2)->o1.getTime()-o2.getTime());
Run Code Online (Sandbox Code Playgroud)
但是我得到了这个编译错误:
Multiple markers at this line
- Type mismatch: cannot convert from long to int
- The method sort(Comparator<? super Message>) in the type List<Message> is not applicable for the arguments ((Message o1, Message o2)
-> {})
Run Code Online (Sandbox Code Playgroud) 从昨天起,我一直在尝试在我的Ubuntu机器上安装JDK8,但它一直在失败.
我一直在尝试运行命令:
sudo add-apt-repository ppa:webupd8team/java -y
sudo apt-get update
sudo apt-get install oracle-java8-installer
sudo apt-get install oracle-java8-set-default
Run Code Online (Sandbox Code Playgroud)
但是我无法继续,因为在运行命令时,sudo apt-get install oracle-java8-installer我得到的是:
...
Connecting to download.oracle.com (download.oracle.com)|23.215.130.99|:80... connected.
HTTP request sent, awaiting response... 404 Not Found
2017-10-18 11:07:34 ERROR 404: Not Found.
download failed
Oracle JDK 8 is NOT installed.
dpkg: error processing package oracle-java8-installer (--configure):
subprocess installed post-installation script returned error exit status 1
...
Run Code Online (Sandbox Code Playgroud)
我的安装程序是64位Ubuntu 14.04.
java ×10
java-8 ×10
java-stream ×3
lambda ×3
comparator ×1
filter ×1
interface ×1
optional ×1
ubuntu ×1