I don't know java and I don't understand what the below instruction means:
Map<String, Object> unpacked = new HashMap<>();
Why use Map
class and after the instance unpacked
use HashMap
?
What does <String, Object>
mean?
Thanks all
I don't know java and I don't understand what the below instruction means:
Map<String, Object> unpacked = new HashMap<>();
Why use Map
class and after the instance unpacked
use HashMap
?
What does <String, Object>
mean?
Thanks all
HashMap
is an implementation of the Map
interface. Check polymorphism in Java for more details, and also check the Collections API.
The <String, Object>
are Generics. Learn more about that. In this case, they specify what this map is for : the keys are strings, and the values are Objects. In a certain way, it maps strings -> objects.
This needs understanding of one of the programming concept called "Associative Array", which is nothing but a collection of a Key and its paired/associated Value.
What is represented by the line of code you mentioned is that, the collection represented by 'unpacked' variable is going to hold String as Key and Object as its paired value.
Map, HashMap are part of Java Collections framework.
Standard books like "Head First Java" or "Java Complete Refefence", could help further enhance your knowledge on this.
A Map is an object that maps keys to values. It cannot contain duplicate keys: Each key can map to at most one value. String? :The keys you are going to map will be entered in String datatype. Object? : This contains values which are to be mapped by the keys. And hasmap contains values which are based on the key. It implements the Map interface and extends AbstractMap class. It contains only unique elements. For better understanding you need to go through the theory as well. Do practice. Hope this helped you. :)
That a generics specification. unpacked
is declared as a map where the key type is a String
and the value type is an Object
.
Please go through Java fundamental books, Head First Java, Java Beginner's guide etc. Map is a key value pair data structure, Hashmap is an implementation of map and <String, Object>
is for Generics.