Skip to main content
Refactor | Explicit Type Argument is redundant
Source Link

How would you initialise a static Map in Java?

Method one: static initialiser
Method two: instance initialiser (anonymous subclass) or some other method?

What are the pros and cons of each?

Here is an example illustrating the two methods:

import java.util.HashMap; import java.util.Map; public class Test { private static final Map<Integer, String> myMap = new HashMap<Integer, String>HashMap<>(); static { myMap.put(1, "one"); myMap.put(2, "two"); } private static final Map<Integer, String> myMap2 = new HashMap<Integer, String>HashMap<>(){ { put(1, "one"); put(2, "two"); } }; } 

How would you initialise a static Map in Java?

Method one: static initialiser
Method two: instance initialiser (anonymous subclass) or some other method?

What are the pros and cons of each?

Here is an example illustrating the two methods:

import java.util.HashMap; import java.util.Map; public class Test { private static final Map<Integer, String> myMap = new HashMap<Integer, String>(); static { myMap.put(1, "one"); myMap.put(2, "two"); } private static final Map<Integer, String> myMap2 = new HashMap<Integer, String>(){ { put(1, "one"); put(2, "two"); } }; } 

How would you initialise a static Map in Java?

Method one: static initialiser
Method two: instance initialiser (anonymous subclass) or some other method?

What are the pros and cons of each?

Here is an example illustrating the two methods:

import java.util.HashMap; import java.util.Map; public class Test { private static final Map<Integer, String> myMap = new HashMap<>(); static { myMap.put(1, "one"); myMap.put(2, "two"); } private static final Map<Integer, String> myMap2 = new HashMap<>(){ { put(1, "one"); put(2, "two"); } }; } 
applies formatting & wording
Source Link
Gerold Broser
  • 15.1k
  • 6
  • 56
  • 124

How would you initialise a static MapMap in Java?

Method one: static initialiser
Method two: instance initialiser (anonymous subclass) or some other method?

What are the pros and cons of each?

Here is an example illustrating the two methods:

import java.util.HashMap; import java.util.Map; public class Test { private static final Map<Integer, String> myMap = new HashMap<Integer, String>(); static { myMap.put(1, "one"); myMap.put(2, "two"); } private static final Map<Integer, String> myMap2 = new HashMap<Integer, String>(){ { put(1, "one"); put(2, "two"); } }; } 

How would you initialise a static Map in Java?

Method one: static initialiser
Method two: instance initialiser (anonymous subclass) or some other method?

What are the pros and cons of each?

Here is an example illustrating two methods:

import java.util.HashMap; import java.util.Map; public class Test { private static final Map<Integer, String> myMap = new HashMap<Integer, String>(); static { myMap.put(1, "one"); myMap.put(2, "two"); } private static final Map<Integer, String> myMap2 = new HashMap<Integer, String>(){ { put(1, "one"); put(2, "two"); } }; } 

How would you initialise a static Map in Java?

Method one: static initialiser
Method two: instance initialiser (anonymous subclass) or some other method?

What are the pros and cons of each?

Here is an example illustrating the two methods:

import java.util.HashMap; import java.util.Map; public class Test { private static final Map<Integer, String> myMap = new HashMap<Integer, String>(); static { myMap.put(1, "one"); myMap.put(2, "two"); } private static final Map<Integer, String> myMap2 = new HashMap<Integer, String>(){ { put(1, "one"); put(2, "two"); } }; } 
Question Protected by GhostCat
Question Unprotected by GhostCat
Remove odd capitalisation of "Initialise". Also revert to OP's British spelling. Please read this: http://meta.stackexchange.com/questions/23869/what-should-the-standard-spelling-be-british-or-us
Source Link
Jonik
  • 82.1k
  • 77
  • 272
  • 386

How can I Initializeinitialise a static Map?

How would you initialise a static Map in Java?

Method one: Static initializerstatic initialiser
Method two: instance initialiser (anonymous subclass) or some other method?

What are the pros and cons of each?

Here is an example illustrating two methods:

import java.util.HashMap; import java.util.Map; public class Test { private static final Map<Integer, String> myMap = new HashMap<Integer, String>(); static { myMap.put(1, "one"); myMap.put(2, "two"); } private static final Map<Integer, String> myMap2 = new HashMap<Integer, String>(){ { put(1, "one"); put(2, "two"); } }; } 

How can I Initialize a static Map?

How would you initialise a static Map in Java?

Method one: Static initializer
Method two: instance initialiser (anonymous subclass) or some other method?

What are the pros and cons of each?

Here is an example illustrating two methods:

import java.util.HashMap; import java.util.Map; public class Test { private static final Map<Integer, String> myMap = new HashMap<Integer, String>(); static { myMap.put(1, "one"); myMap.put(2, "two"); } private static final Map<Integer, String> myMap2 = new HashMap<Integer, String>(){ { put(1, "one"); put(2, "two"); } }; } 

How can I initialise a static Map?

How would you initialise a static Map in Java?

Method one: static initialiser
Method two: instance initialiser (anonymous subclass) or some other method?

What are the pros and cons of each?

Here is an example illustrating two methods:

import java.util.HashMap; import java.util.Map; public class Test { private static final Map<Integer, String> myMap = new HashMap<Integer, String>(); static { myMap.put(1, "one"); myMap.put(2, "two"); } private static final Map<Integer, String> myMap2 = new HashMap<Integer, String>(){ { put(1, "one"); put(2, "two"); } }; } 
edited tags
Link
Ardalan Shahgholi
  • 12.7k
  • 25
  • 116
  • 155
Loading
edited title
Link
bjb568
  • 11.5k
  • 11
  • 53
  • 73
Loading
Rollback to Revision 4
Source Link
Erwin Bolwidt
  • 31.3k
  • 15
  • 59
  • 81
Loading
Loading
Question Protected by Andrew Barber
added 5 characters in body
Source Link
zellus
  • 9.6k
  • 5
  • 41
  • 58
Loading
edited tags
Link
Jonik
  • 82.1k
  • 77
  • 272
  • 386
Loading
edited tags
Link
starblue
  • 57.1k
  • 14
  • 101
  • 153
Loading
Source Link
dogbane
  • 276.3k
  • 77
  • 407
  • 415
Loading