• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generic Type Verification

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I have a couple of questions about the code above.
1. Why line 4 gives compiler error, but line 3 does not. I notice a kind of similar between line 3 and line 4, see bold chars. Map<String, List<String> > map1 = new HashMap<String, List<String >>();
2. Map<String, List<String>> map1 = new HashMap<String, List<String>>(); How do I know List<String> is back by a an ArrayList, a LinkedList, or something else.


Thanks

 
Ranch Hand
Posts: 74
5
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1st question

In line 3 you instantiate a HashMap that is designed to store instances of List<String>. HashMap is a class (an implementation of the interface Map).

In line 4 you instantiate a List that is designed to store instances of String. List is an interface and cannot be instantiated directly. You must instantiate a class that implements this interface instead, such as an ArrayList or an anonymous inner class. While it may look that way, you don't instantiate a List in line 3, you only define the HashMap to store instances that implement List<String>. Instantiation takes place when you use the new keyword and this can't be followed by an interface alone.

2nd question

If that is necessary you can use the instanceof operator to check if an instance is of a specific type. Be aware that if one element in your list is of type ArrayList it does not mean that the others have to be. You should generally not have to check that. If that is necessary (and you don't just do that out of curiosity) you could probably refactor your code in such way that it's not necessary to check that. The whole idea of interfaces is that groups of instances are treated the same way no matter if they're ArrayLists or LinkedLists or something else.
 
Sheriff
Posts: 17652
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vu lee wrote:I notice a kind of similar between line 3 and line 4, see bold chars. Map<String, List<String> > map1 = new HashMap<String, List<String >>();


The only similarity is that it involves List<String>. The semantics of the usage of List<String> in Line 3 is totally different from that on Line 4.

Refer to the Syntax description here: https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-ClassOrInterfaceTypeToInstantiate

On Line 3, List<String> is a TypeArgument
On Line 4, List<String> is a ClassOrInterfaceTypeToInstantiate

The use of List<String> on line 3 is legal, whereas its use on line 4 is not because of the reason already mentioned by Christian, you can't just write new List<String>(); because List is an interface. You would have to provide an anonymous ClassBody that contains implementations of all the methods defined by the List interface: new List<String>() { ... // lots of methods to implement ... };
 
Are you okay? You look a little big. Maybe this tiny ad will help:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic