Heterogeneous Container with two element of same type

前端 未结 2 978
既然无缘
既然无缘 2020-12-21 17:43

I am reading Effective Java - Item 29. It talks about Heterogeneous container, in the example:

private Map, Objec         


        
2条回答
  •  礼貌的吻别
    2020-12-21 18:39

    If you put two strings the second will override the first one. So it is useful only if this behaviour is desired. If you want to store more objects under the same key you can use other containers, for example :

    Map, List>
    
    
    

    Or you can use MultiMap from Guava : http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Multimap.html

    Or you can use MultiMap from apache commons : http://commons.apache.org/proper/commons-collections/javadocs/api-3.2.1/org/apache/commons/collections/MultiMap.html

    提交回复
    热议问题