Java泛型澄清(Java Generics clarification)
在那里,我虽然理解Java泛型,但在我尝试编写以下内容之前:
class A{} class B{ A a; <T extends A> T getA(){ return a; // does not compile } }
我收到编译错误,说明类型不兼容:需要T,找到A.
- 为什么我收到错误?
- 我很乐意参考一篇描述这种java泛型陷阱的文章。
谢谢!
There I though I understood Java generics until I tried to write the following:
class A{} class B{ A a; <T extends A> T getA(){ return a; // does not compile } }
I get a compilation error saying the types are incompatible: required T, found A.
- Why am I getting the error?
- I would be happy to get a reference to an article that describes this kind of java generics pitfalls.
Thank you!
原文:https://stackoverflow.com/questions/11816653
更新时间:2023-07-07 06:07
最满意答案
这个怎么样 :
public class multivalueHashmap { private Map< Integer, List<Float> > map = new HashMap< Integer, List<Float> >(); public void add(Integer id, Float value){ if(!map.containsKey(id)){ map.put(id, new ArrayList<Float>()); } map.get(id).add(value); } public void delete(Integer id, Float value){ if(!map.containsKey(id)){ return; } map.get(id).remove(value); } }
这样您就可以使用这些方法轻松添加和删除项目。
How about this :
public class multivalueHashmap { private Map< Integer, List<Float> > map = new HashMap< Integer, List<Float> >(); public void add(Integer id, Float value){ if(!map.containsKey(id)){ map.put(id, new ArrayList<Float>()); } map.get(id).add(value); } public void delete(Integer id, Float value){ if(!map.containsKey(id)){ return; } map.get(id).remove(value); } }
This way you can use the methods to easily add and remove items.
相关问答
更多-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
我知道我有点晚了,但我会分享我做的,以防其他人: HashMap
selects = new HashMap (); for(Map.Entry entry : selects.entrySet()) { String key = entry.getKey(); HashMap value = entry.getValue(); // do what you have to do ... -
您可以通过get获取该值,并使用remove从List中remove相关元素: hm.get("1").remove("two"); 当然,你必须针对get返回null的情况进行自我辩护: hm.computeIfPresent("1",(k,v)->{v.remove("two");return v;}); 这与以下内容类似: if (hm.get("1") != null) { hm.get("1").remove("two"); } 另外请注意,您放入Map中的List (由Arrays ...
-
您有效地拥有两个集合,因此如果您希望删除"Class"元素,则需要在两者中执行此操作。 如果总是在两个集合中删除tmp的最后一个元素是你关心的,并且假设header是List
,你应该这样做: String keyToRemove = header.remove(header.size()-1); map.remove(keyToRemove); You have two collections effectively, so if you want the "Class" element ... -
如果您需要使用HashMap ,那么没有比迭代条目集更好(更有效)的解决方案,并且一次删除一个条目。 这将是O(N)操作。 您需要访问/测试地图中的所有条目。 正如您正确观察到的那样,您可以从TreeMap更整齐,更有效地批量删除条目。 这将是一个O(logN)操作。 但缺点是插入和删除是O(logN)而不是O(1) 。 LinkedHashMap可以在某些用例中提供帮助,但不能在这个用例中提供帮助。 (它根据插入顺序对条目进行排序,而不是按键的值。) If you are required to use ...
-
这个怎么样 : public class multivalueHashmap { private Map< Integer, List
> map = new HashMap< Integer, List >(); public void add(Integer id, Float value){ if(!map.containsKey(id)){ map.put(id, new ArrayList ()); ... -
用于创建TreeSet的New_Comparison比较器的compare方法永远不会返回0,因此就TreeSet而言,这意味着没有两个Trial元素相等。 这就是为什么set.remove(t)不会删除任何东西。 将其更改为: public int compare(Trial n1, Trial n2) { if(n1.c < n2.c) return 1; else if (n1.c > n2.c) return -1; return 0; } ...
-
从hashmap中删除逗号(Remove commas from hashmap)[2023-10-11]
正如noisessmith指出的那样,这两者之间没有(程序化)差异,因为, clojure源代码中有空格: user=> (= {:a "A", :b "B"} {:a "A" :b "B"}) true user=> (map type [{:a "A", :b "B"} {:a "A" :b "B"}]) (clojure.lang.PersistentArrayMap clojure.lang.PersistentArrayMap) PS:这取决于底层漂亮的打印机(打印的默认clojure) ,以帮 ... -
HashMap删除无法正常工作(HashMap Remove is not working)[2023-10-09]
假设一个Item被添加到大小为16的HashMap中。它的hashCode = 20。 Android的HashMap在桶4中创建一个条目,存储实际的散列(20)和对Item本身的引用。 现在让我们说Item被修改,以便hashCode更改为36。 如果我们现在使用相同的Item引用运行containsKey(),Android的HashMap将查看相同的存储桶,4。它在那里找到基于identity(==)的键。 它是相同的对象,但此时它并不关心哈希码。 然而,如果我们使用相同的Item引用运行remove ... -
每次用户点击购物车时,您都会创建cartList。 也许,如果你删除字符串cartList = new ArrayList
>(); 从OnClick重写示例代码的第一个字符串: public static ArrayList > cartList = new ArrayList >() ? You create cartList each time when user ...