Skip to content

Instantly share code, notes, and snippets.

@Bachmann1234
Created December 18, 2014 20:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bachmann1234/3fc0efd8a245e95bb438 to your computer and use it in GitHub Desktop.
Save Bachmann1234/3fc0efd8a245e95bb438 to your computer and use it in GitHub Desktop.
Statically initialized map
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[] args) {
Map<Integer, String> tens = new HashMap<Integer, String>(){{
put(10, "ten");
put(20, "twenty");
put(30, "thirty");
put(40, "fourty");
put(50, "fifty");
put(60, "sixty");
}};
System.out.println(tens.get(10));
}
}
@jstnlef
Copy link

jstnlef commented Dec 18, 2014

Technically you don't need the java.util.Map; import since you're not using this HashMap as a Map elsewhere. ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment