Working with ArrayList - Part 2

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 continue to work with ArrayList. If you're ready, let's start. I'll create a new class. I right click on the collections example package and select the new class. The class name can be ArrayListMethods2, and check the checkbox for the main method and click the 'Finish' button. So, let's create an ArrayList with the type string. ArrayList string languages = new ArrayList(); First, I will print the size of this ArrayList. System.out.println(languages.size()); Let's run and see. As you can see, it is printed zero because currently the list is empty. So, if we use the isEmpty method, it will return true. Let's try. System.out.println(languages.isEmpty()); and run. As you can see, the size is zero. 

And since it's empty, it printed true. Okay, now let's add some elements to this list. languages.add("Java"); languages.add("C#"); languages.add("Scala"); languages.add("Kotlin"); Okay, now let's print the size of the array and check it with the isEmpty method. I will copy and paste these print methods. Okay, now let's run the code. As you can see, now the size of the array is four, and since it is not empty, the isEmpty method returns false. Now, let's use the contains method. With the contains method, we find out if the element we are querying is in the list. Returns true if present, false otherwise. Let's use it.

System.out.println(languages.contains("C++")); This will return false because the C++ is not an element of the languages list. Now, let's search for another element. System.out.println(languages.contains("Kotlin")); This will return true because the Kotlin is an element of this list. Let's run and see. As you can see, the first result is false and the second is true. Okay, now let's use the clear method of the ArrayList class. The clear method deletes all elements of the list at once. So, I write languages.clear. And now let's create an if statement. If the languages list is empty, print the, "The clear method deleted all elements." Else, print the, "There is a problem." Okay, now let's run and test. As you can see, the clear method worked and deleted all elements from the list. And since the languages list is empty now, it returned true and the if condition is satisfied. 

So, the if block was executed and, "The clear method deleted all elements" message is printed on the console. Okay, lastly, let's look at the equals method of the ArrayList class. If you remember, we used the equals method for strings and wrapper classes and arrays. Now we will use it in the ArrayList. The equals method in ArrayList compares the elements in the list in order. It returns true if the contents of the elements are the same, otherwise it returns false. Let's just do an example. I'll create an array list, ArrayList object test1 = new arrayList<>(); And let's add three elements to this list. test1.add("Java"); test1.add("Kotlin"); test1.add("Scala"). And let's create another array. arrayList < string > test2 = new arrayList<>(). test2.add("Java"); test2.add("Kotlin"); test2.add("Scala"). Now, let's compare them with the equals method. test1.equals(test2). Before running the app, let's examine the code. The equals method compares the elements of the lists in order. So, let's look at the first element. The first element is Java for both lists and the second element is Kotlin for both lists again, and the last element is Scala for both lists. So, all three elements are the same. For this reason, the result will be true. Notice that I purposely created the data types of these lists differently. 

The first list is the object and the second is string. However, the equals method will return true because the elements are the same. Let's run the code and see the result. As you can see, the result is true. Now, I will change the order of the elements for the second list. The first element will be Scala and the last element will be Java. Okay, this time it will return false because the equals method compares the elements in order. First, it looks to the first element in each list. If they are the same, this time it looks at the second element. If not, it returns false. So, this time the first elements are not the same, so the return will be false. Let's run and see. As you can see, the result is false this time. Now let's take a short break. I'll see you in the next video.


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