Why the HashMap implements the Map interface twice

See the type hierarchy below:
HashMap<K,V>
&#9475;
&#9507;AbstractMap<K,V>
&#9475;&#9475;
&#9475;&#9495;Map<K,V>
&#9495;Map<K,V>
The HashMap extends AbstractMap, which implements Map. So the HashMap has already implements Map, and has the Map type. But the HashMap's declaration still contains "implements Map".
So why the HashMap implements the Map interface twice?
Thanks in advance.

georgemc wrote:
Maybe the guy who wrote HashMap wanted to make it clear that HashMap implemented Map, rather than rely on you looking further up the hierarchy to find outYes, it does make the Javadoc clearer. If they hadn't done that then there would have been legions of newbies who couldn't tell that HashMap implemented Map and thousands of "I have a doubt, does HashMap implement Map" questions here.

Similar Messages

  • Why the ArrayList implements the List interface

    Why the ArrayList implements the List interface even though it extends the AbstractList class. Is it only for clarity of interface implemented?

    899440 wrote:
    Why the ArrayList implements the List interface even though it extends the AbstractList class. Is it only for clarity of interface implemented?I dont think there's any specific reason for that. In my opinion its redundant. May be its added for clarity so that from the documentation its obivous that ArrayList implements List.
    May be there's something which I am missing regarding this?

  • Correctness of the HashMap implementation w.r.t. its specifications/docs

    According to the JavaDoc of HashMap, containsKey is defined as :
    public boolean containsKey(Object key)
         Returns true if this map contains a mapping for the specified key.
         Specified by:
              containsKey in interface Map
         Overrides:
              containsKey in class AbstractMap
         Parameters:
              key - The key whose presence in this map is to be tested
         Returns:
              true if this map contains a mapping for the specified key.
    Again, according to the JavaDoc of Map, containsKey is defined as :
    public boolean containsKey(Object key)
    Returns true if this map contains a mapping for the specified key. More formally, returns true if and only if this map contains at a mapping for a key k such that (key==null ? k==null : key.equals(k)). (There can be at most one such mapping.)
    In a project, I use a HashMap to store objects and my key class is something like that :
    public class MyKey {
    private String s1 = null;
    private String s2 = null;
    public MyKey(String s1, String s2) {
    this.s1 = s1;
    this.s2 = s2;
    public boolean equals(Object o) {
    if (o instanceof MyKey) {
    MyKey oKey = (MyKey) o;
    returns this.s1.equals(o.s1) && this.s2.equals(o.s2);
    returns false;
    So according to the JavaDoc, the following should be ok :
    MyKey key1 = new MyKey("hello","world");
    MyKey key2 = new MyKey("hello","world");
    HashMap map = new HashMap();
    map.put(key1, new Object());
    map.containsKey(key1); // returns true
    map.containsKey(key2); // returns false BUT should return true
    After reading the code of containsKey, it actually does a AND test on a hashcode and the equals method, which does not correspond to the spec.
    map.containsKey(key2) returns false because although key1.equals(key2) is true, key1.hashCode() != key2.hashCode().
    Is that behaviour normal ? Am I missing something in my understanding of the keys comparison ?
    This behaviour also stands for the Set Interface, despite what's said in the JavaDoc.
    Regards.
    Jerome

    It's not so clear exactly how the documentation should be ammended though. HashMap.containsKey is required to conform to Map.containsKey, and putting the hashCode referencing stuff into Map would force its use in every Map implementation, even if they weren't in fact hash maps.
    I think the view that the designers have taken is that the key is assumed to be well-formed, which means that all its methods obey their contracts. If this isn't true, then the behaviour is undefined.
    Do we want such a well-formed requirement to be expressly stated everywhere that an Object is used?
    Sylvia.

  • How does the servlet implement the multi-thread feature?

    There is only one instance of every servlet in one wep application.
    When several clients invoke the doPost() method of the same servlet,the servlet can process their request respectively.
    So there much multi threads of one servelt.
    But the Servlet doesn't implement the Runnable interface of extends the Thread class.
    I wan't to know,how does the servlet/servlet container implement the multi-thread feature?

    Hi johnnylzb
    There is only one servlet instance existing.
    (assuming u have <load-on-startup>1</load-on-startup>)
    The server creates a thread for every request ..
    and all the threads access the same servlet instance.
    For this its not necessary for the Servlet to extend Thread or Runnable.
    hope this helps

  • Sorting the values in the HashMap maintaing the key-value relationship

    Hi,
    I want to sort the HashMap based on the values. I have tried several other ways but the closest I can get is sort the values but have to loose the keys. I wanted to sort the collection based on the values but at the same time maintain the key-value relationship. Is there any way I can get this functionality either by using any collection API or by some other way?
    Thanks and appreciate your help....
    --coolers                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

    I believe that Commons Collections from Jakarta has something like that.
    http://jakarta.apache.org/commons/collections/
    But I'm not sure.

  • Mapping Interface

    Hi All
    Can we create the any kind of Mapping Interface that allows for defining metadata that is then used to perform dynamic SQL calls. the SQL is created dynamically at execution time. Mapping involves a)Target Field Identification b)Source Identification c)Transformation d)Formatting e)Record Selection
    Regards
    Anurag Saroha

    Hi Anurag Saroha,
    dynamic sql is not possible with owb. I do not recommend it in general, it is difficult to maintain.
    You may use OMB*Plus to generate mappings. Have a look at the documentation or search in this forum.
    Regards,
    Carsten.

  • How to delete the index for the business object BUS0033

    Hi to all experts,
    I'm applying note 1349496 the error here is no records with F4 help for the funds center .
    solution from the note
    Implement the attached program corrections. Then, in the transaction, delete the index for the business object BUS0033, reactivate it, and start the indexing in the indexing mode "Full". The system then displays the data correctly in the F4 search help.
    how to do the second part i have already applied the note .

    any help

  • How to reset the data when the session lost?

    When I use response.sendRedirect("http://localhost:82/main.jsp")
    the session lost, because the URL before redirect is using SSL,
    "https://localhost:81/index.jsp", they use the different port,
    so the session lost, what should i do to prevent this? And how
    to reset the data?

    Create a HashMap and store it as an attribute of the servlet context. In the first servlet assign the user a key and store all session data in the HashMap under that key (use a vector, collection or user defined class).
    In the response.sendRedirect call add the key to the url:
    response.sendRedirect(url + "?key=" + userKey);
    In the second servlet get the key (request.getParameter("key")), retrieve the session data from the HashMap in the servletcontext and store in an httpsession.
    Remember to delete the entry from the HashMap so it doesn't become overly large.

  • Why extends HashMap and implements Map?

    Hi,
    I browsed the source of HashMap in JDK 5.0.
    The declaration of the class puzzled me.
    Declaration of HashMap:public class HashMap<K,V>
        extends AbstractMap<K,V>
        implements Map<K,V>, Cloneable, SerializableDeclaration of AbstractMap:public abstract class AbstractMap<K,V> implements Map<K,V>Puzzle:
    Now that AbstractMap has been declared to implement Map interface, and HashMap extends AbstractMap,
    why HashMap is declared to implement Map interface again?
    I don't think that's necessary.
    a cup of Java, cheers!
    Sha Jiang

    I don't think it makes any difference that this code is part of the standard libraryAbsolutely, I agree on the view.
    I think it was declared as such for clarity only.I just be curious with the little topic.
    "clarity" may be the answer.
    Thanks!

  • Use of generics in the Map interface

    After using Java 1.5 for a while, I've gotten pretty comfortable using generics and the collections framework. Very often, I depend on the new generics-related compiler warnings to guide me when I've made changes to my code. Recently, I missed a fairly subtle bug because of the following issue. Why isn't the get() method in the java.util.Map interface declared as:
    V get(K key);Is there some subtle reason why this method isn't declared to enforce the generic type K for the key value? Likewise for containsKey() and remove() as well.
    Thanks!

    So where is the problem to use K in the first
    place??The cast to K is removed during complilation. It's
    not going to do anything at runtime.
    Just because a reference is of type Object doesn't
    mean it's not a K.
    If I get an Object of unknown type, why should I have
    to down-cast it just so that I can check to see
    whether it is in the Map?
    To state the point I wrote this simple code:
            Map<String,Integer> testMap = new TreeMap<String,Integer>();
            testMap.put("test", Integer.valueOf(100));
            // test the get method
            Integer value = testMap.get("test");
            System.out.println("First value: " + value);
            // test the method with differing parameter
            value = testMap.get(Float.valueOf(2));
            System.out.println("Second value: " + value);The code compiles without any error (so far so good) but the execution of this code results in Exception in thread "main" java.lang.ClassCastException: java.lang.String
         at java.lang.Float.compareTo(Unknown Source)
         at java.util.TreeMap.compare(Unknown Source)
         at java.util.TreeMap.getEntry(Unknown Source)
         at java.util.TreeMap.get(Unknown Source)
         at MapTest.main(MapTest.java:27)So when the Object parameter is allowed (I'm okay with that) why the ClassCastException. A similar approach to the implementations of Lists would be necessary. The current implementation is not satisfactory.

  • A java List that implements the Stream interface?

    Hello,
    I just took some time to start looking into the java-8 buzz about streams and lambdas.
    And have a couple of questions...
    The first thing that surprised me is that you cannot apply the streams operations,
    like .map(), .filter() directly on a java.util.List.
    First question:
    Is there a technical reason why the java.util.List interface was not extended with
    default-implementations of these streams operations? (I guess there is...?)
    Googling a bit, I see lots of examples of people coding along the pattern of:
        List<String> list = someExpression;
        List<String> anotherList = list.stream().map(x -> f(x)).collect(Collectors.toList());
    which becomes very clumsy, if you have a lot of these stream-operations in your code.
    Since .stream() and .collect() are completely irrelevant to what you want to express,
    you would rather like to say:
        List<String> list = someExpression;
        List<String> anotherList = list.map(x -> f(x));
    What I first did as a workaround, was to implement (see code below)
    a utility interface FList, and a utility class FArrayList (extending ArrayList with .map() and .filter()).
    (The "F" prefixes stand for "functional")
    Using these utilities, you now have two options to create less clumsy code:
        List<String> list = someExpression;
        List<String> anotherList = FList.map(list, x -> f(x));
        List<String> thirdList = FList.filter(list, somePredicate);
    or better:
        FList<String> list = new FArrayList<String>(someExpression);
        FList<String> anotherList = list.map(x -> someFunction(x));
        FList<String> thirdList = list.filter(somePredicate);
    My second question:
    What I would really like to do is to have FArrayList implement the
      java.util.stream.Stream interface, to fully support the java-8 functional model.
    Since that involves implementing some 40 different methods, I would just like to know,
    if you know somebody has already done this kind of work, and the code is
    available somewhere as a public jar-file or open-source?
        public interface FList<T> extends List<T> {
            public static <A, B> List<B> map(List<A> list, Function<A, B> f) {
                return list.stream().map(f).collect(Collectors.toList());
            public static <A> List<A> filter(List<A> list, Predicate<A> f) {
                return list.stream().filter(f).collect(Collectors.toList());
            default <R> FList<R> map(Function<T, R> f) {
                FList<R> result = new FArrayList<R>();
                for (T item : this) {
                    result.add(f.apply(item));
                return result;
            default FList<T> filter(Predicate<T> p) {
                FList<T> result = new FArrayList<T>();
                for (T item : this) {
                    if (p.test(item)) {
                        result.add(item);
                return result;
        public class FArrayList<T> extends ArrayList<T> implements List<T>, FList<T> {
            private static final long serialVersionUID = 1L;
            public FArrayList() {
                super();
            public FArrayList(List<T> list) {
                super();
                this.addAll(list);

    I believe SSH and telnet are used for interactive command line sessions, don't know how you want to use them in a program.

  • Implements the method defined in interface

    I defined a interface such as
    interface IAuthorizer{
    boolean checkPermission(Set<IAttribute> subj, Set<IAttribute> Permission);
    In java 5, why does compiler not accept the method checkPermission signature of the following class
    class Auth1 implements IAuthorizer{
    boolean checkPermission(Set<IAttribute> subj, Set<IAttribute> Permission){
    Iclass Auth1 implements IAuthorizer{
    boolean checkPermission(Set subj, Set Permission){
    nstead, the compiler only accepts

    <If you compile the interface and the class in the same time (eg not having the interface in a jar file against which to compile the class), the compiler should accept your code.>
    The interface and class is even in the same package. The compiler does not accept method signature as
    boolean checkPermission(Set<IAttribute> subj, Set<IAttribute> perms)
    <If your interface is part of a library jar file, could it be that
    - the library is obfuscated using an obfuscater that doesn't know about generics?
    - the class is written against JDK 1.4 API (in which Set is not generic)?
    - the class is compiled using java 1.4 compiler?
    >
    no library is involved in the issue.
    class is written in JDK 1.5 API and use JDK 1.5 compiler.

  • Concrete classes implement abstract class and implements the interface

    I have one query..
    In java collection framework, concrete classes extend the abstract classes and implement the interface. What is the reason behind extending the class and implementing the interface when the abstract class actually claims to implement that interface?
    For example :
    Class Vector extends AbstractList and implements List ,....
    But the abstract class AbstractList implements List.. So the class Vector need not explicitly implement interface List.
    So, what is the reason behind this explicit definition...?
    If anybody knows please let me know..
    Thanx
    Rajendra.

    Why do you post this question again? You already asked this once in another thread and it has been extensively debated in that thread: http://forum.java.sun.com/thread.jsp?forum=31&thread=347682

  • Reg Implementing the interfaces in JDBC

    Hi,
    When we do a program that deals with database connectivity,we are not implementing the Interfaces like Statement,ResultSet.But we are using those
    interfaces inside our program.
    Can anybody justify how it is possible.
    Thanks in advance

    The DB vendors are responsible for providing implementation classes for these interfaces. That's why we need to keep DB specific jar files in CLASSPATH when we do DB activities..
    Gautam

  • Implementing the Serializable interface

    Hi guys,
    I have a conceptual question about implementing the Serializable interface - since it's only a marker interface (i.e. - it doesn't have any abstract methods) why does Java demand an object to implement this interface in order to be written into a file? What I'm asking is why wouldn't they make the writeObject method's signature
    public void writeObject(*Object* o)
    so any object can get in? What is the point of forcing a class to implement an interface in order to enable it to be written to a file?

    So that serialization requires the explicit consent of the class or superclass/superinterface.

Maybe you are looking for