How can you create a map collection in Java?
In Java, a Map collection can be created using the following method:
- Create an instance of a HashMap using the HashMap class.
Map<String, Integer> map = new HashMap<>();
- Create an instance of LinkedHashMap class using the LinkedHashMap constructor (to maintain insertion order).
Map<String, Integer> map = new LinkedHashMap<>();
- Create a TreeMap instance using the TreeMap class (sorted according to the natural order of keys).
Map<String, Integer> map = new TreeMap<>();
- Create an instance of Hashtable class to create a Hashtable (a thread-safe Map implementation).
Map<String, Integer> map = new Hashtable<>();
- Create an instance of ConcurrentHashMap class, which is an efficient thread-safe Map implementation, using ConcurrentHashMap class.
Map<String, Integer> map = new ConcurrentHashMap<>();
The above are common ways to create a Map collection, choose the appropriate Map implementation class based on specific needs.