Results 1 to 5 of 5
  1. #1
    guerlando is offline Member
    Join Date
    Mar 2016
    Posts
    11
    Rep Power
    0

    Default Map teste = new HashMap(), why not Map teste = new Map();

    Hi, I was studying java and the following question arose:

    Java Code:
    Map teste = new HashMap();
    why not
    Java Code:
    Map teste = new Map();
    or even

    Java Code:
    HashMap teste = new HashMap();
    ?

    I know that Map is an interface, but why people don't create an object of type HashMap instead of Map? What's the diffence?


    The same for this line of code:

    Java Code:
    Collection<SendDiscoveryThread> threads = new ArrayList<SendDiscoveryThread>();
    Why not

    Java Code:
    ArrayList<SendDiscoveryThread> threads = new ArrayList<SendDiscoveryThread>();
    ?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Map teste = new HashMap(), why not Map teste = new Map();

    It's simply because you can't instantiate an interface, only classes can be instantiated. There is nothing more to it ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    guerlando is offline Member
    Join Date
    Mar 2016
    Posts
    11
    Rep Power
    0

    Default Re: Map teste = new HashMap(), why not Map teste = new Map();

    ok, but there's still the case of HashMap myHashMap = new HashMap().

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Map teste = new HashMap(), why not Map teste = new Map();

    A HashMap is a class and you're instantiating one. There's not much of a case here ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Map teste = new HashMap(), why not Map teste = new Map();

    It is best practice to use an interface type as a reference to an interface implementation.

    The syntax,
    Java Code:
    Map<SomeType> mymap = new HashMap<>();
    Is preferred. HashMap is a particular implementation of the Map interface. It is usually best to use the interface whenever possible (in instantiations and in method signatures - i.e. methods that take a map as an argument). This way, if you want to change the implementation from HashMap to SomeOtherMapImpl, you only need to do it in one location.

    The exception is that if the implementation provides additional functionality that you require (but not defined in the interface). Then you must use the implementation type.

    e.g.
    Java Code:
    SomeMapImplementation<MyType> foo = new SomeMapImplementation<>();
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Help with HashMap?
    By sim18 in forum New To Java
    Replies: 7
    Last Post: 05-03-2012, 09:36 AM
  2. final HashMap hm=new HashMap();
    By sangramkeshari.jena in forum New To Java
    Replies: 4
    Last Post: 07-21-2011, 10:44 PM
  3. HashMap help
    By BobswYourUcle in forum New To Java
    Replies: 4
    Last Post: 05-06-2011, 10:05 PM
  4. Should i use a hashmap?
    By 6Sloth9 in forum New To Java
    Replies: 7
    Last Post: 05-02-2011, 03:38 AM
  5. Replies: 7
    Last Post: 12-08-2009, 08:17 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •