我理解集合的get和put原则:如果一个方法接受一个集合,它将写入一个类型T,参数必须是Collection<? super T>,而如果它将读取一个类型T,参数必须是Collection<? extends T>.
但有人可以解释Collections.max()签名:
public static <T> T max(Collection<? extends T> coll,
Comparator<? super T> comp)
Run Code Online (Sandbox Code Playgroud)
特别是为什么它Comparator<? super T>而不是Comparator<? extends T>?
为什么这段代码不能编译(Parent是一个接口)?
List<? extends Parent> list = ...
Parent p = factory.get(); // returns concrete implementation
list.set(0, p); // fails here: set(int, ? extends Parent) cannot be applied to (int, Parent)
Run Code Online (Sandbox Code Playgroud) 我主要是一名C#开发人员,我正在向我的朋友教授Data Structures,他们在他们的大学里使用Java,我在Java中看到了这样一个表达式:
void printCollection(Collection<?> c) {
for (Object e : c) {
System.out.println(e);
}
}
Run Code Online (Sandbox Code Playgroud)
我还没有在C#中看到这样的东西,所以我想知道Java Collection<T>和Collection<?>Java 之间的区别是什么?
void printCollection(Collection<T> c) {
for (Object e : c) {
System.out.println(e);
}
}
Run Code Online (Sandbox Code Playgroud)
我认为它也可以用上面的方式编写.在文档中的家伙比较Collection<Object>和Collection<T>虽然.
示例来自http://docs.oracle.com/javase/tutorial/extra/generics/wildcards.html
我知道已经发布了类似的问题,虽然我认为我的情况有所不同......
假设您有两种方法:
// Bounded type parameter
private static <T extends Number> void processList(List<T> someList) {
}
// Upper bound wildcard
private static void processList2(List<? extends Number> someList) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
据我所知,这两种方法接受参数,是List类型Number或List的亚型的Number.
但毕竟这两种方法有什么区别?
public static void main(String[] args) {
List<? extends Object> mylist = new ArrayList<Object>();
mylist.add("Java"); // compile error
}
Run Code Online (Sandbox Code Playgroud)
上面的代码不允许您向列表中添加元素,并且通配符只能用作方法中的签名,同样不能用于添加,而只能用于访问.在这种情况下,上述目的是什么?
我希望理解这个概念:
T object - generic,将被删除为实际类型.? 对象 - 会被抹去什么?Object 宾语;之间有什么区别T,?和Object?
我很容易理解#1,但是怎么样:
Object var;
? var;
Run Code Online (Sandbox Code Playgroud)
两者有什么区别?我已经读过,我不能?明确地使用,像T或任何其他变量,并且它?与对象有关,而不是类型.
但实际原因是什么?为什么我不能只写一个List对象(List<Object>)而不是List通配符(List<?>)?因为我不知道两种情况下的对象类型.
另外,我想知道什么是擦除??
在下列情况下,我遇到问题,请查看下面的内联评论:
public void exampleMethod() {
//Intuitively I would expect this to mean that test is set containing objects
//that subclass AbstractGroup
Set<? extends AbstractGroup> test;
//Yet the compiler complains here and I do not understand why?
test.add(new AnyAbstractGroupSubGroup());
//I would guess that a method call such as this at runtime
test = new HashSet<SubGroupA>()
//would mean that only objects of subgroupA can be added to the collection, but then
//what is the point in using the wildcard in the first …Run Code Online (Sandbox Code Playgroud) 我实例化以下列表:
// I am just revising generics again and the following is just cursory code!
List<? super Integer> someList = new ArrayList<Object>();
someList.add(new Object());
Run Code Online (Sandbox Code Playgroud)
以上不起作用.我收到编译器错误.但是,以下工作:
List<? super Integer> someList = new ArrayList<Object>();
someList.add(11);
Run Code Online (Sandbox Code Playgroud)
我知道您可以将对象添加到包含无界通配符的集合中,而不是有界通配符.
但是,为什么以上不起作用?Object是Integer的超类型,为什么我不能添加它?
java ×8
generics ×7
collections ×2
wildcard ×2
comparator ×1
object ×1
type-erasure ×1
types ×1