下面的代码对我来说是完全合理的 - 它是关于添加某种类型的元素,它是类型T的超类型而类型S绝对是超类型,那么为什么编译器拒绝将'element'添加到集合中?
class GenericType<S,T extends S>{
void add1(Collection<? super T> col ,S element ){
col.add(element); // error
// The method add(capture#9-of ? super T) in the type
// Collection<capture#9-of ? super T> is not applicable for the arguments (S)
}
}
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
List<MassEditionObject> objects = getProjects();
Run Code Online (Sandbox Code Playgroud)
哪个MassEditionObject是由Project类实现的接口.
getProjects()返回List<Project>,看起来应该没问题,因为Project是一个MassEditionObject.
但是,Eclipse给了我这个错误:
Type mismatch: cannot convert from List<Project> to List<MassEditionObject>
我理解接口的一般概念以及如何使用它们,我只是不确定为什么这是无效的.
谢谢,对不起,如果这个问题已经发布.我搜索并发现了类似的情况,但没有人回答这个问题.
我想知道这段代码有什么问题:
Map <? extends String, ? extends Integer> m = null;
Set<Map.Entry<? extends String, ? extends Integer>> s = m.entrySet();
Run Code Online (Sandbox Code Playgroud)
编译器抱怨错误消息:
类型不匹配:无法转换
Set<Map.Entry<capture#1-of ? extends String,capture#2-of ? extends Integer>>为Set<Map.Entry<? extends String,? extends Integer>>
应该是什么类型的s?Eclipse建议,Set<?>但我想尝试更具体.
我在第二行收到错误,不是所有项目都是一个吗Object?
Map<String, ?> value; // I get value from some place
Map<String, Object> val = value;
Run Code Online (Sandbox Code Playgroud) 我有一个名为FamilyHistoryPersonModel扩展名为的类的类PersonModel.
MapValue mapValue = new MapValue("relatives",
new GenericType<FamilyHistoryPersonModel>() {},
new GenericType<List<FamilyHistoryPersonModel>>() {} //error here
);
Run Code Online (Sandbox Code Playgroud)
构造函数:
private MapValue(String pluralUrl, GenericType<? extends PersonModel> singleResultGenericType, GenericType<List<? extends PersonModel>> listResultGenericType) {
this.pluralUrl = pluralUrl;
this.singleResultGenericType = singleResultGenericType;
this.listResultGenericType = listResultGenericType;
}
Run Code Online (Sandbox Code Playgroud)
错误:
cannot find symbol
symbol : constructor MapValue(java.lang.String,<anonymous com.sun.jersey.api.client.GenericType<com.models.FamilyHistoryPersonModel>>,<anonymous com.sun.jersey.api.client.GenericType<java.util.List<com.models.FamilyHistoryPersonModel>>>)
location: class com.rest.v2.resource.CreatePersonModelIntegrationTest.MapValue
Run Code Online (Sandbox Code Playgroud)
为什么我收到此错误?我如何解决它?我不想改变构造函数来接受GenericType<List<FamilyHistoryPersonModel>> listResultGenericType
我使用的是JDK 1.6
我有两个分支:
public class A {
//attibutes and methods
}
public class B extends A {
//atributes and methods
}
Run Code Online (Sandbox Code Playgroud)
现在我有一个服务返回一个包含A类元素的List.让我们称之为generateAElements() ;
我想调用那个方法,过滤得到的List只保留B类元素,它们也是A类.
List<A> listA = generateAElements();
List<A> listAA = listA.filter( p -> p instanceof B).collect(Collectors.toList());
List<B> listB = new ArrayList<>();
// need to create a new list, iterate overListA and add elements of type B?
for (A itemA : listA) {
listB.add((B) itemA);
}
Run Code Online (Sandbox Code Playgroud)
有没有一种有效的方法来做到这一点?
重要提示:列表可能包含大量元素.
我真的不知道如何解释我的问题.下面是我拥有的一些对象的示例.
让我们说我上课了
public class Foo{}
public class FooBar extends Foo{}
Run Code Online (Sandbox Code Playgroud)
所有这一切都很好,但是如果我拿一个列表说我希望它充满了Foo对象,我就不能提供它FooBar.为什么是这样?
例:
List<Foo> fooList = Arrays.asList(new FooBar());
Run Code Online (Sandbox Code Playgroud)
如果FooBar extends Foo,为什么它不能被抛回Foo?
嗨我安静了一个简单的代码,给我一个错误
void changeHeaders(HttpURLConnection url){
Map<String, Iterable<String>> m ;
m = url.getHeaderFields();//<-- this line gives an error
...
}
Run Code Online (Sandbox Code Playgroud)
错误是: Type mismatch: cannot convert from Map<String,List<String>> to Map<String,Iterable<String>>
为什么我无法将List转换为Iterable?
Java代码:
class Animal {}
class Cat extends Animal {}
class Box<T> {}
public class Test {
public void hello(Box<? extends Animal> box) {}
}
Box<Animal> box1 = new Box<Animal>();
Box<Cat> box2 = new Box<Cat>();
new Test().hello(box1);
new Test().hello(box2);
Run Code Online (Sandbox Code Playgroud)
从Liskov替换原则,因为box2类型Box<Cat>可以在需要类型的地方使用Box<? extends Animal>,我可以说:
Box<Cat> is subtype of Box<? extends Animal>
其实:我不是,如果甚至不确定Box<? extends Animal>和? extends Animal有型
你能解释为什么第一个不起作用吗?
import java.util.ArrayList;
import java.util.List;
public class MyClass {
private <T extends Object> List<Class<T>> getList1() {
return new ArrayList<>();
}
private <T extends Object> List<Class<T>> getList2(Class<T> cls) {
List<Class<T>> list = new ArrayList<>();
list.add(cls);
return list;
}
private List<Class<? extends Object>> getList3() {
return new ArrayList<>();
}
public static void main(String[] args) {
MyClass myClass = new MyClass();
// 1. Not OK - The method add(Class<Object>) in the type List<Class<Object>> is not applicable for the arguments (Class<String>)
myClass.getList1().add(String.class);
// 2. …Run Code Online (Sandbox Code Playgroud) java ×10
generics ×6
list ×2
casting ×1
collections ×1
dictionary ×1
inheritance ×1
interface ×1
java-7 ×1
java-8 ×1
java-stream ×1
type-systems ×1