首页 \ 问答 \ Java泛型澄清(Java Generics clarification)

Java泛型澄清(Java Generics clarification)

在那里,我虽然理解Java泛型,但在我尝试编写以下内容之前:

class A{}

class B{
    A a;

    <T extends A> T getA(){ 
       return a; // does not compile
    }
}

我收到编译错误,说明类型不兼容:需要T,找到A.

  1. 为什么我收到错误?
  2. 我很乐意参考一篇描述这种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.

  1. Why am I getting the error?
  2. 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.

相关问答

更多
  • 我知道我有点晚了,但我会分享我做的,以防其他人: 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; } ...
  • 正如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) ,以帮 ...
  • 假设一个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 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。