对于不确定类型泛型,Lambda表达式是怎么推断出参数类型的呢?
PriorityQueue构造方法:
public PriorityQueue(Comparator<? super E> comparator) {
this(11, comparator);
}
使用Lambda表达式创建对象
public static void main(String[] args) {
PriorityQueue<Integer> left = new PriorityQueue<Integer>(
(o1, o2)-> {
System.out.println(o1.getClass().getName());
return o1 - o2;
}
);
left.add(1);
left.add(2);
}
运行发现o1类型自动设定为Integer,而不是Object,还是说将PriorityQueue的泛型默认认定为Comparator的泛型类型。
java.lang.Integer
Process finished with exit code 0