我有2个不同类型的不同地图,这两个地图都是BaseClass.
我希望能够使用相同的逻辑将两个案例中的项目"保存"到地图中,使用Generics.
假设getId是一种方法BaseClass.
我需要强迫
item是的子类BaseClassmap值与该类型相同item我怎样才能做到这一点?这是一次失败的尝试,描述了我需要做的事情:
private <T><?extends BaseClass> void save(T item, Map<Long, T> map)
{
if (item.getId() == null)
{
long max = -1;
for (Long key : map.keySet())
max = Math.max(max, key);
item.setId(max + 1);
}
map.put(item.getId(), item);
}
Run Code Online (Sandbox Code Playgroud) 是什么区别List<Integer>和List<? super Integer>。
哪个是好的做法或什么时候应该使用什么?
我正在尝试创建一个hashmap值的hashmap,其中包含自定义类的不同子类的hashsets,如下所示:
HashMap<String, Hashmap<String, HashSet<? extends AttackCard>>> superMap
Run Code Online (Sandbox Code Playgroud)
AttackCard有子类,如:Mage,Assassin,Fighter.superMap中的每个HashMap只会包含一个包含单个子类的HashSets AttackCard.
当我尝试放一个
HashMap<String, HashSet<Assassin>>
Run Code Online (Sandbox Code Playgroud)
下面是发生错误的代码:
public class CardPool {
private HashMap<String, HashMap<String, HashSet<? extends AttackCard>>> attackPool =
new HashMap<>();
private ArrayList<AuxiliaryCard> auxiliaryPool;
public CardPool() {
(line 24)this.attackPool.put("assassins", new AssassinPool().get());
/* this.attackPool.put("fighters", new Fighter().getPool());
this.attackPool.put("mages", new Mage().getPool());
this.attackPool.put("marksmen", new Marksman().getPool());
this.attackPool.put("supports", new Support().getPool());
this.attackPool.put("tanks", new Tank().getPool());
*/
this.auxiliaryPool = new ArrayList<>(new AuxiliaryCard().getPool());
}
Run Code Online (Sandbox Code Playgroud)
这里有一个AssassinPool get方法的片段:
private HashMap<String, HashSet<Assassin>> pool = new HashMap<>();
public HashMap<String, …Run Code Online (Sandbox Code Playgroud) 有几个人告诉我,Java 允许协变数组子类型化,换句话说,如果 A 是 B 的子类型,则 A[] 是 B[] 的子类型,但这是一个糟糕的特性,因为它会导致运行时错误。有人能给我一个具体的例子来说明它如何导致运行时错误以及 Java 是否/如何解决这个问题?
我发现我不能调用通配符类型的泛型方法而且不明白为什么?
public class GenericsTry2 {
public static class Element {
private Container<? extends Element> container;
public Container<? extends Element> getContainer() {
return container;
}
public void setContainer(Container<? extends Element> container) {
this.container = container;
}
public void doStuff() {
getContainer().doStuff(this); // how to call this?
}
}
public static class SomeSubElement extends Element {
}
public static class SomeSubElement2 extends Element {
}
public static class Container<E extends Element> {
public void doStuff(E element) {
}
}
public static void …Run Code Online (Sandbox Code Playgroud) 我正在尝试这个 -
List<? extends Integer> l = new ArrayList<Integer>();
l.add(10);
Run Code Online (Sandbox Code Playgroud)
和编译器说 -
The method add(int, capture#1-of ? extends Integer) in the type List<capture#1-of ? extends Integer> is not applicable for the arguments (int)
Run Code Online (Sandbox Code Playgroud)
为什么我无法在整数列表中添加整数,为什么编译器在第一行本身没有抱怨如果我无法添加整数?
本文档的图表显示了使用上限和下限通配符声明的几个List类之间的关系.关系如下图所示:
在右侧层次结构中,List<? super Number>是子类型List<? super Integer>.这不是很困惑吗?
据我解释,List<? super Number>可以通过任何被表示List<type>,其中type或者是Number或超类的Number.同样的逻辑List<? super Integer>也适用.那怎么可能List<? super Number>是一个子类型List<? super Integer>?
以下代码抛出编译时异常
类型不匹配:无法从Integer转换为K.
我理解的是K应该处理任何扩展的价值Number.
public <K extends Number> K getValue(){
Integer a = new Integer(1);
return a;//Type mismatch: cannot convert from Integer to K
}
Run Code Online (Sandbox Code Playgroud)
(注意:这只是一个测试代码,可以提出我的问题,而不会给出我实际情况的不相关细节.)
在 Spring Batch 的课程中,我很惊讶地看到? extends String. 由于 String 是最终类,这如何正确?
public void write(List<? extends String> items) throws Exception {
System.out.println(String.format("Received list of size: %s", items.size()));
items.forEach(System.out::println);
}
Run Code Online (Sandbox Code Playgroud) 这是我的代码:
Set<Class<Event>> s = new HashSet<>();
Set<Class<? extends Event>> s2 = new HashSet<>();
Event e = new Event();
s.add(e.getClass()); // #1
s2.add(e.getClass()); // #2
class Event {
// ...
}
Run Code Online (Sandbox Code Playgroud)
为什么编译器会在语句中引发错误#1?
我正在使用Java 7.
java ×10
generics ×8
arrays ×1
collections ×1
covariance ×1
covariant ×1
hashmap ×1
wildcard ×1