创建线程时,我看到如下代码:
Runnable watdaheck = new Runnable()
{
System.out.println("java with time contradicts itself");
}
Run Code Online (Sandbox Code Playgroud)
据我所知,无法实例化接口,因此我无法理解我们如何编写Runnable()来创建匿名类。可以给接口一个参考,但是不能实例化多态性。
相对较新到Java 8,我想知道为什么它允许第一个变体(merge
功能不是必要的)Collectors.toMap()
一起工作时List
:
static <T,K,U> Collector<T,?,Map<K,U>> toMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper)
Run Code Online (Sandbox Code Playgroud)
AList
允许完全重复的值。想象一个用例,其中开发人员用于stream
转换List
为Map
Java 8 将 RUNTIME 公开为:
Exception in thread "main" java.lang.IllegalStateException: Duplicate key ...
Run Code Online (Sandbox Code Playgroud)
不应该要求在编译时捕获这种情况吗?AFAIK,哈希图用于在放置Entry
重复项时简单地替换旧的key
。如果开发人员确定数据中存在重复值并希望将此异常作为警告处理,他会不会首先使用Set
代替List
?例如。:-
public class Employee{
public String name;
public String surname;
public Employee(String firstname, String secondName) {
this.name=firstname;this.surname=secondName;
}
public boolean equals(Object o){
return (o instanceof Employee) && ((Employee)o).name.equals(this.name) …
Run Code Online (Sandbox Code Playgroud)