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 will talk about the List Interface, which is the most used interface of collections. You can think of the list interface as a slightly more advanced version of the arrays we learned in the previous lesson. Elements you add to the list are stored in order of insertion. Also, the same element can be stored more than once. The Java platform contains two general purpose list implementations. ArrayList, which is usually the better performing implementation, and LinkedList, which offers better performance under certain circumstances. We will continue with ArrayList in our course. So, how to declare and initialize the ArrayList? ArrayLists declaration is like creating an object from a regular class. First, we specify the collection type i.e. list, ArrayList, set, or map. In this example, it's an ArrayList. Then we must specify which data type the list we'll create inside the diamond symbol will be. The list in our example is of type string. Then we specify the name of the list that we will create.
After the equal sign, we can create an object from the ArrayList class using the new keyword. Of course, if you notice, there is also a diamond symbol on the right side of the equal sign and it says String in between. Finally, there is a constructor parentheses. But for Java 7 and later, it's no longer necessary to specify the data type inside the diamond symbol. If you specify, you will not get any errors. Now let's see how we can add data to an ArrayList. For example, of type integer, of course, we are using the integer wrapper class here. We have an ArrayList called ageList, not primitive. We can add as many elements as we want using the add method of the ArrayList. Even if we specify the index number in the add method, we can add an element to the index we want. We also use the remove method to delete any element from the ArrayList. You can write the element name directly into the remove method, as well as the index number. Yes, now let's move on to Eclipse and get some practice.
First, I'll create a new project. I will click the file menu and select the New Java Project option. So, the project name can be Collections and the JRE will be Java 8 or Java 1.8, and I click the 'Finish' button. Now I will create a new class in this project. So, I right click on the source folder and select new class. The package name can be collectionsexample and the class name can be ArrayListDeclaration. And I will check the checkbox for the main method and click the 'Finish' button. Okay, first I will create an ArrayList. I write ArrayList, and as you can see, the ArrayList is under the java.util package. In fact, all collections are in java.util package. So, to use a collection, we need to import the java.util package. So, I will select this option and press 'Enter'. As you can see, it's imported. Also you can put a star instead of the Array class. So, thus all collections will be imported.
Okay, let's continue. Inside the diamond, we need to write the data type of this ArrayList. So, this can be the type of the object, and the name of this array can be list1. After the equal sign, I write new ArrayList<>(); and you can write the data type of this array inside the diamond again, or you can leave an empty. And last, put the constructor parentheses. Okay, the declaration of ArrayList is like this. Also, you can create it like that. ArrayList type can be string this time, name, list2; and semi colon. And in the next line you can initialize it. list2 = new ArrayList<>(); This is also valid. Now, let's create another ArrayList. ArrayList< integer > list3 = new ArrayList<>(); Of course, you should use the wrapper classes, not primitive. For example, let's delete this integer and write int instead. As you can see, we get a compilation error. So, we cannot use the primitive in the collections. Okay, now let's create another ArrayList. List< Double > list4 = new ArrayList<>(); This is also valid because the ArrayList class implements the list interface. Remember the hierarchy diagram of the collection framework?
Since the ArrayList implements the list, you can write like this. Also this was polymorphism, remember? Okay, now let's create another ArrayList. Collection. I will select the collection interface in the java.util package. Type can be string, name list5, and new ArrayList<>(); This is also valid, because the collection interface is the root interface. So, the list extends the collection interface and the ArrayList implements the list2. So, the ArrayList is a subclass of the list and collection. Okay, and last, I'll show you another thing. Let's create another ArrayList. ArrayList list6 = new ArrayList(). As you can see, this time I don't use the diamond symbol and data type. So, this is also valid, but this time the compiler accepts this as raw. So, if you move the cursor over the ArrayList, it says 'ArrayList is a raw type'. References to generic type ArrayList should be parameterized. So, if you leave it like that, the type of this array is object by default, and you can add string, integer, double, or any type to this array. Don't forget this. Of course, you must use the constructor parentheses here, otherwise you will get a compilation error. Okay, the declaration of the ArrayList is like this. Let's take a short break here. 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.