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 the previous video, we talked about the set interface and Hashset and LinkedHashSet classes. In this video, we'll talk about the TreeSet class. So, the TreeSet class implements the sorted interface which extends to the set interface. The main feature of the TreeSet class is that it sorts the added elements from smallest to largest or alphabetical. This is the difference from other classes. Also, the tree set class doesn't store duplicate values like other set classes. Now let's move on to the Eclipse and get some practice. I'll continue through the previous class named SetExample. Now, I will create a TreeSet object. TreeSetStringmyCitiesSet = new TreeSet. Now, let's add some elements to this list, myCitiesSet.add("Paris"). And I will copy and paste this line below nine times. 

The second element can be London, the third element can be Rome, the fourth element can be Washington, the fifth element can be Washington again, the sixth element can be New York, the seventh element can be Berlin, the eighth element can be Tokyo, the ninth element can be Tokyo again, the last element can be Amsterdam. Okay, the TreeSet is ready. Now let's use the useful methods of the TreeSet. First, I'll print the elements in descending order. System.out.println("Descending") + myCitiesSet.descendingSet. Now, let's print the elements of the myCitySet, System.out.println("myCitiesSet: "+ myCitiesSet. Now let's print the elements before the New York, System.out.println("HeadSet") + myCitiesSet.headSet. There are two types of the headset method. 

The method with one parameter, returns the group of elements that are less than the specified element. And the method with two parameter, returns the group of elements that are less than or equal to, if inclusive is true, the specified element. I want to print the elements before the New York. So I write "New York" in parentheses. Or, you can add the second Boolean parameter as false, if you set the second parameter true, this time, the element New York will also be printed. Now, let's print the elements from London to Tokyo. System.out.println("SubSet" + myCitiesSet.subSet). The subset method returns a set of elements that lie between the given range, which includes from element and excludes to element. 

I want to print the elements from London to Tokyo. So I write, "London", true, "Washington", false. Or you can write it like that. London and Washington. If you write it like that, it includes London but not Washington. Okay, let's continue. Now I'll print the elements from New York to the last element. System.out.println("TailSet: " + myCitiesSet.tailSet "New York") If I write the second parameter is false, this time, New York will not be printed on the console. Also, we can print the first and last elements directly on the console. System.out.println("First element: " + myCitiesSet.first()) System.out.println("Last element: " + myCitiesSet.last()). Also, we can use the poll first and poll last methods for retrieving and removing the first and last elements. System.outprintln("Retrieve and remove the first element: ". + myCitiesSet.pollFirst()) System.out.println("Retrieve and remove the last element:" + myCitiesSet.pollLast()), and lastly, let's print the final version of the myCitiesSet. 

Okay, let's run and see. As you can see, the elements are printed first in descending alphabetical order, because we used the descending method first, then we printed the TreeSet object itself, and as you can see, it's printed in ascending alphabetical order this time. Notice, that we are not doing any sorting here, TreeSet did this automatically. It also printed the same element only once like other set classes. Then, we printed the elements before New York with the HeadSet method. Next, we printed the elements from London to Tokyo with a SubSet method. Then, with the TailSet method, we printed the elements from New York to the last element on the console. Then, with the first and last methods, we printed the current first and last element. 

Finally, we printed the first and last elements in the set with the poll first and poll last methods. Also, the first and last elements are deleted from the set. So if we look at the final version of the myCitySet, we can see the first and last elements are removed. Yes, so we used the set interface and the classes that implement this interface. 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