In this course, we'll learn the Collection framework and the Map Interface.
Learning Objectives
- Collection Framework vs Array
- Hierarchy of Collection Framework
- List interface - ArrayList Class
- Iterator Interface
- Set Interface
- Queue and Dequeue Interfaces
- Map Interface
Intended Audience
- Anyone looking to get Oracle Java Certification
- Those who want to learn the Java Programming language from scratch
- Java developers who want to increase their knowledge
- Beginners with no previous coding experience in Java programming
- Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams
Prerequisites
- No prior knowledge is required about the Java programming language
- Basic computer knowledge
Hi there, in this lesson, we'll learn the Map Interface. The map interface is out of the collection. A map is not a true collection. The map interface is present in the Java.util package, and represents a mapping between a key and a value. The feature that distinguishes map from other collections is that we can store the elements in the lists as key value pairs. There are three classes that implement the map interface: HashMap, TreeMap, and LinkedHashMap. Now, let's look at the declaration and initialization of a map. Let's examine our example on the screen. Since map is an interface, objects cannot be created of the type map. We always need a class that extends this map in order to create an object. In this example, we use the HashMap class. Between the diamond sign we need to write two different data types. The first data type will be the type of key that represents the element we will add to the list. The second data type will be the type of element we will add to the list.
In our example here, the keys type is string and the elements type is integer. To add data to the map, we use the put method. The put method takes two parameters. The first parameter is the key, and the second parameter is the value. In this example, we have two elements. The key of the first element is David, and its value is 20. The key of the second element is Ronaldo and its value is 25. Now let's see how to print these elements of a map to the console. To do that, we use the get method of the map interface. The get method takes the key as the parameter. In our example, to print the value of 20 to the console, we need to use the David key. Similarly, to print the value 25 to the screen, we need to use the Ronaldo key. After this information about map, let's open the Eclipse and do some practice.
First, I'll create a new project. I'll click the file 'Menu' and select the new Java project options. The project name can be Map Interface, and I click the 'Finish' button. Now, I will create a new class in this project. I'll right click on the source folder, and select the new class options. The package name can be mapexample and the name can be MapExample. And I will check the check box for the main method, and click the 'Finish' button. First, let's create a map. The key can be string and the value can be integer. The name of the reference can be agesMap. After the equal sign, I write new HashMap. Now, let's add some elements to this map, agesMap.put. The put method takes two parameters. The first one is the key, so I write David.
And the second parameter is the value, so I write the age of David, 20. I'll copy this line and paste it four times. The second element can be Ronaldo, and his age is 25. The third element can be Alex, and his age is 30. The fourth element will be David again, and this time his age will be 35. And lastly, this name can be Mary and her age is 40. Okay, the map is ready. First, I will print the elements of this map on the console. S out "agesMap" plus agesMap, and I will print the age of David. S out "age of David" plus agesMap.get. So, to learn the value of a specific key, we can use the get method of the map interface. It takes the key as the parameter. So, I write David because the key is David. Now, let's run the code.
As you can see in the first line, the elements of the map are printed, and it stores the data by mapping the key value. Each key corresponds to a value. I also tried to add two values using the David key, but it only added the second value. In other words, the map class contains only one key value. And since we are using HashMap, we can also observe that the order in which the data is added is not preserved. Now, let's use the for loop to print all elements of the map. For, string name: agesMap.key set. Thus, in each iteration, the keys will assign the name object. So, we get the values using the key, S out "Age of" plus name plus "is" plus agesMap.get name. Let's run the code. As you can see using the for loop, we can access and print the keys and values of the map.
Also, we can access the keys and values using the entry interface. Let's use it, for map.entry. The type of the key will be string. The type of the value will be integer. Name of the object can be mapEntry:ages.entrySet. Now, I can access the keys and values using the map entry object. I will copy this print method and paste it into the loop. Also, I'll convert this loop to the comment line. To get the key, I write mapEntry.getKey and to get value, I write mapEntry.getValue. Let's run and see. As you can see, the keys and values are printed on the console. The
usage of the map interface and the HashMap is like that. Lastly, let's remove an element from the map. I will remove the Mary, agesMap.removeMary. Also, you can change the value of a key. For this, we can use the replace method. For example, let's change the age of Alex, agesMap.replaceAlex 50. Let's run and see. As you can see, the Mary is removed from the map, and the age of Alex is updated. Now let's do an example using the LinkedHashMap. The feature that distinguishes LinkedHashMap from HashMap is that it preserves the intersection order of the added elements.
Let's create a LinkedHashMap. LinkedHashMap, string, string, countryCodes, equal new linked HashMap. Now, let's add some elements to this map. countryCodes.put DE Germany. countryCodes.put FR France. country Codes.put GB United Kingdom. countryCodes.put IT Italy. Now let's print the elements on the console. S out "countryCodes." Let's run and test. As you can see this time, the order in which the elements are added has not changed. Other methods and usage are the same as hash set. Finally, we'll make an example of the TreeSet class, but let's take a short break here, we'll continue in the next lesson. See you in the next lesson.
OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.