相关疑难解决方法(0)

Java泛型:Collections.max()签名和Comparator

我理解集合的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>

java generics comparator

25
推荐指数
1
解决办法
4129
查看次数

无法使用通配符泛型类型为Java集合添加值

为什么这段代码不能编译(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)

java generics

25
推荐指数
2
解决办法
1万
查看次数

Collection <?>和Collection <T>之间有什么区别?

我主要是一名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

java generics collections types unbounded-wildcard

23
推荐指数
4
解决办法
1063
查看次数

有界类型参数与上限通配符的区别

我知道已经发布了类似的问题,虽然我认为我的情况有所不同......

假设您有两种方法:

// 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类型NumberList亚型Number.

但毕竟这两种方法有什么区别?

java generics bounded-types bounded-wildcard

15
推荐指数
1
解决办法
2076
查看次数

使用通配符的Java集合

public static void main(String[] args) {

    List<? extends Object> mylist = new ArrayList<Object>();

    mylist.add("Java"); // compile error

}
Run Code Online (Sandbox Code Playgroud)

上面的代码不允许您向列表中添加元素,并且通配符只能用作方法中的签名,同样不能用于添加,而只能用于访问.在这种情况下,上述目的是什么?

java

9
推荐指数
2
解决办法
5166
查看次数

Java泛型,对象和通配符的差异和说明

我希望理解这个概念:

  1. T object - generic,将被删除为实际类型.
  2. ? 对象 - 会被抹去什么?
  3. Object 宾语;

之间有什么区别T,?Object

我很容易理解#1,但是怎么样:

Object var;
? var;
Run Code Online (Sandbox Code Playgroud)

两者有什么区别?我已经读过,我不能?明确地使用,像T或任何其他变量,并且它?与对象有关,而不是类型.
但实际原因是什么?为什么我不能只写一个List对象(List<Object>)而不是List通配符(List<?>)?因为我不知道两种情况下的对象类型.
另外,我想知道什么是擦除?

java generics wildcard object type-erasure

5
推荐指数
1
解决办法
624
查看次数

java中的通用集合和通配符

在下列情况下,我遇到问题,请查看下面的内联评论:

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)

java generics collections wildcard

3
推荐指数
1
解决办法
2609
查看次数

泛型 - 无法添加到具有无界通配符的列表

我实例化以下列表:

// 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 generics

3
推荐指数
1
解决办法
1596
查看次数