Search code examples
hashmapunreachable-code

Why HashMap<String,String> is Unreachable code. HashMap in Java 8


I'm declaring a HashMap in Java 8, this:

HashMap<String, String> nameMap = new HashMap<>();
OR
HashMap<String, String> nameMap = new HashMap<String, String>();

The compiler show this error: unreachable code. When I put HashMap<String, Object> nameMap = new HashMap<>() this error disapears.


Solution

  • Not possible only. 1) Check ur compiler . Should be using correct version 1.8 even if u added jdk 1.8. I think for u this is set as 1.5 coz diamond operator (<>) was introduced in 1.7 I think. enter image description here

    enter image description here