Start course
Overview
Difficulty
Beginner
Duration
2h 16m
Students
26
Description

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
Transcript

Hi there. In our previous lesson, we talked about the Map interface, and the HashMap and LinkedHashMap classes that implement this interface. In this lesson, we'll talk about the TreeMap class which implements to the map interface. The main feature that distinguishes the TreeMap class from the HashMap and LinkedHashMap classes is that it implements the Map interface as well as they navigate over Map interface, which inherits from the SortedMap interface. Therefore, in addition to the methods in the Map interface, we can also use the descendingKeySet( ) in descending map methods in the NavigableMap interface. 

The map is sorted according to the natural ordering of its keys or by a comparator provided at map creation time, depending on which constructor is used. Therefore, the elements we create with the Tree set are sorted according to the keys in ascending order or alphabetically. This distinguishes the TreeMap class from the HashMap and LinkedHashMap classes. After this brief information, let's move on to Eclipse and get some practice. I'll create a new class. Right click on the 'mapexample' package and select the new 'Class' option. The class name can be TreeMap example. And I will check the checkbox for the main method and click the 'Finish' button to create this class. Okay. Let's create an object from the TreeMap class. TreeMap. The type of key can be integer. And the type of value can be string. I'll store the students' roll number as the key and their names as the value. So, the name of the object can be 'studentsMap'. After the equal sign, I write new TreeMap. 

Okay. Also, I'll show you something. If you press the 'Ctrl' key on the keyboard and click the 'TreeMap' class, it opens the TreeMap class where it is defined. And here, you can see that this class implements the NavigableMap interface. If you press the 'Ctrl' key and click on the 'NavigableMap' interface, this time, you open the place where the NavigableMap class is defined. Notice that, this interface also extends the SortedMap interface. That is, it inherits from this interface. Finally, if you click on the 'SortedMap' interface with 'Ctrl', you will open the place where the SortedMap interface is defined. And notice that, this also inherits from the Map interface. So, this is how you can understand the hierarchy between classes and interfaces in the collection framework in the Map interface. Yes. Now, let's close these tabs and move on to TreeMap. 

I will add some data to this map using the 'put( )' method again. 'studentsMap.put( )'. The key can be 120. The value can be 'David'. And I'll copy this line and paste it below four times. The second key can be 101, and its value can be 'Alex'. The third key can be 105, and its value can be 'Marry'. The fourth key can be 103, and its value can be 'Ronaldo'. The fifth key can be 110, and its value can be 'James'. Okay. Now, let's print this map on the console. System.out.printIn ("studentsMap: " + studentsMap). Let's run and see. As you can see, the data is printed on the console. But notice that the data is stored by key value from least to greatest. This is the main difference between the TreeMap class and other Map classes. Now, let's use the descendingKeySet( ) method that comes from the NavigableMap interface. I'll copy and paste this line. 

This will be descendingKeySet( ). And this will be studentsMap.descendingKeySet( ). This method only prints the keys and the order of the keys is from largest to smallest. Let's run and see. As you can see, only the keys are printed on the console and the order of the keys is from largest to smallest. Now, let's use the descending map method. I will copy this line and paste it below. This can be descendingMap, and this method will be the descendingMap( ). This method prints the keys along with their values and the ordering of the keys is, again, from largest to smallest. Let's run and see. As you can see, the keys and their values are printed on the console and the order of the keys is from largest to smallest. We can also remove an element from the map or change the value of an element, again, using the key value of the elements. 

I'll convert these two lines to the comment line. Okay. Now, let's remove 'Ronaldo' from the map. To do this, we will use the remove( ) method again; studentsMap.remove( ) in parentheses. I'll write the key of the value which I want to remove. So, it is the 103. Also, I'll change the name of 'James'; 'studentsMap.replace( )'. The key is 110, and the new name can be 'Clark'. Okay. And this time, let's print the last version of the map using the for-each loop; for(int sKey: studentsMap.KeySet). And in the loop, I write System.out.printIn ("Name: " + studentsMap.get(sKey). Remember, we can get the value using the GET method. And plus again, "Roll number: " + sKey. Also, we can use the \t before the roll number to leave a tab of space. Okay. Let's run and see. As you can see, 'Ronaldo' is deleted from the map and the name 'James' is changed to 'Clark'. And we printed the last version of the map on the console using the for-each loop. Also, you can use the Entry interface again to print the elements on the console. We learned how to use the Entry interface in our previous lesson. I leave this part to you. Yes, the TreeMap class is like that. Let's take a short break here. See you in the next lesson.

 

About the Author
Students
409
Courses
35
Learning Paths
2

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.

Covered Topics