Set Interface
Start course
2h 16m

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


  • No prior knowledge is required about the Java programming language
  • Basic computer knowledge

Hi there. In this video, we'll talk about the set interface. The set interface is present in the java.util package and extends the collection interface in an unordered collection of objects in which duplicate values cannot be stored. It's an interface that implements the mathematical set. This interface adds a feature that restricts the insertion of duplicate elements. We use the set interface for lists where we want to have only one of the same elements. The HashSet and LinkedHashSet classes implement the set interface. The TreeSet class also implements the SortedSet interface, which extends the set interface. The objects that we insert into the HashSet do not guarantee to be inserted in the same order. The objects are inserted based on their hashCode. This class also allows the insertion of null elements. When the iteration order is needed to be maintained, this class is used. When iterating through a HashSet, the order is unpredictable. While a LinkedHashSet lets us iterate through the elements in the order in which they were inserted. Let's see how to create a set object using this class.

The syntax we use to create a set object is as you see in the slide. If you're ready, let's switch to Eclipse and get some practice. First, I will create a new class. I right click on the collections example package and select the 'New Class' options. The class name can be SetExample. And I will check the check box for the main method and click the 'Finish' button. First, let's create a set with the type String. Set<String> myCarSet = new HashSet. Remember, since the set is an interface, options cannot be created from the TypeSet. We always need a class that extends this list in order to create an object. Now, let's add some elements to this list. To fill this set, this time I will use the asList() method of the arrays class like that, myCarSet.addAll. Inside the parentheses, I write Arrays.asList, and in parentheses, we'll create a new array. So, I write new String[]. I'll write the elements. The first element can be Ferrari, the second can be Ford, the third can be BMW, the fourth can be Ford again, the fifth can be Opel, and the last element can be Hyundai. Now, let's get some practice with the methods of the set interface.

First, I will print the elements of the set, System.out.println("myCarSet: " + myCarSet). Second, I'll print the size of the set, System.out.println("size of myCarSet: " + myCarSet.size()). And now, I will remove the BMW from the set. myCarSet.remove("BMW"). And let's print the elements again. System.out.println("after removing BMW: " + myCarSet). And now, I will use the contains method. With the contains method, we can search any element in the set. System.out.println("Does myCarSet contains Mercedes? " After the plus sign, I write myCarSet.contains and inside the parentheses I write, Mercedes. Now, let's run the code and see the output. As you can see, the first output is the elements of the set. But notice that the Ford element is printed only once. This is actually the main purpose of the set interface. If you want to have only one of each element in your list, then the set class will make your job much easier. Also, the size of the set is five not six because the Ford element is duplicate and it only accepts one of the Ford elements. Okay. On the third print, we removed the BMW element, and it printed the last version of the set.

And on the last print, we used the contains method for Mercedes and it returned false because the Mercedes is not an element of this set. Yes, there were basic methods that we could use with the set class. We can also compare two different lists using the set class. Let's do an example about that right now. First, I will add a println method to separate the results from above. Okay. Now, I will create two sets with type integer. Set<Integer> mySet1 = new HashSet<>() and let's fill this set. mySet1.addAll(Arrays.asList), and in parentheses, we can write the name of the elements directly. Because note, that the asList method takes the Varargs as the parameter. So, we can add as many elements as we want.  So, I write 1, 3, 4, 8, 9, 7, and 5, and let's print this on the console. System.out.println("mySet1: " + mySet1). Now, let's create another integer set. Set<Integer> mySet2 = new HashSet<>(). And let's fill this set. mySet2.addAll(Arrays.asList). And in parentheses, I write 1, 2, 3, 6, 4, 10, and 4. And let's print this set on the console, System.out.println("mySet2: " + mySet2). Okay. The sets are ready. We can make three different comparisons.

First, we get a new list that is a combination of the elements in both lists. In this case, intersecting and repeating elements will be printed only once. This is known in the literature as the union operation. For this, we'll use the addAll() method. The other method is that we can detect the common, i.e., intersecting elements in both lists. This is also known as intersection. For this, we will use the retainAll() method. Finally, we can find the elements of one list that are different from the other list. For this, we'll use the removeAll() method. Okay. If you're ready, let's start. First, I will create a new set. Set<Integer> unionSet = new HashSet<>(). And I will give the mySet1 set as a constructor parameter to the HashSet class. Thus, the mySet1 set is assigned to the unionSet. Now, I will use the addAll() method. So, I write unionSet.addAll, and in parentheses, I write mySet2. Let's print the unionSet on the console. System.out.println("union set: " + unionSet). Let's run and see. These are the elements of the mySet1 and these are the elements of the mySet2. And after the addAll() method, the new set consists of the both elements of both sets. But notice that each element is printed only once.

Okay. Now, let's look at the intersection operation. I'll copy these codes and paste them here. The name of the set will be intersectionSet. And this method will be intersectionSet.retainAll. And this will be the intersection set. Okay. Let's run and see. As you can see, the common elements in both lists are printed. And lastly, let's look at the elements of mySet1 different from mySet2. I will copy these codes and paste them here. The name of this set will be differenceSet. And this method will be differenceSet.removeAll. And this will be different elements. And this will be the differentSet. Okay. Let's run and see. As you can see, the difference of mySet1 from mySet2 is printed on the console. Okay. Now, let's make an example with HashSet and LinkedHashSet and end the lesson. The peculiarity of HashSet is that it only considers one of the same elements but does not guarantee that the list elements are stored in the same order. So, the first element you add to the list may not be at the beginning of the list. Let's just do an example. First, I'll create a println method for the separating. I'll create an object from the HashSet class, HashSet<String> hs1 = new HashSet<>(). Now, let's add some elements to this set. hs1.add("Dog").

I'll copy this line and paste it below seven times. The second element can be cat, the third and fourth elements can be monkey, the fifth element can be cow, the sixth and seventh elements can be lion, and the last element can be snake. Now, let's print this HashSet. System.out.println("hs1: " + hs1), and let's run it. As you can see, the index number of the elements is changed. Also, the lion and the monkey were printed only once. The thing to keep in mind for the HashSet is that it stores elements in a Hash, not the order in which you add them. Now, let's look at the LinkedHashSet. LinkedHashSet class contains unique elements only like HashSet and it provides all optional set operation and permits null elements. The difference from the HashSet class, the LinkedHashSet class maintains insertion order. Let's do an example. I'll copy these codes and paste them here. This class will be LinkedHashSet and the name of the object can be lhs1. Also, I'll change these object names. And lastly, this will also be lhs1. Let's run and see. As you can see, this time the insertion order of the elements was not changed. Also, again, the duplicate elements are printed only once. Okay. Lastly, we'll talk about the TreeSet class, but let's take a short break here. I'll see you in the next lesson.


About the Author
Learning Paths

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