1. Lists
Let's now look at Lists.
2. List interface
A `List` is an ordered group of objects that allows duplicates. While there are several `List` implementations, such as `ArrayList` and `LinkedList`, their behavior and methods are similar. However, their internal structure differs, which affects performance and determines the best use case for each.
3. List implementations
`ArrayList` functions similarly to an array but for objects. It's an indexed, ordered collection that allows adding, removing, and modifying objects.
`LinkedList` consists of linked objects. There are links, or pointers, connecting the objects in the list. Methods to add, remove, change or even clear the lists are the same.
4. ArrayList
We construct an `ArrayList` with `new` keyword and use generic type syntax to tell Java the types of objects to be stored in the `ArrayList`. There are methods to add, get, or change objects in the `ArrayList` as shown. We remove an element at a certain index, counting from zero. There are also methods to clear the list or get its size. Don't forget to import any Collections Framework type used.
5. Objects and primitives
Collections Framework data structures store objects - not primitives. However, wrapper classes allow primitives to be added to `ArrayList`. If you add a primitive, e.g., `int`, to a collection, Java automatically converts it into its corresponding wrapper object. This process is called `autoboxing`. It ensures that primitives can be used seamlessly within Java's object-based collections.
6. Heterogeneous collections
Collections Framework data structures can be heterogenous, meaning they can hold objects of different types. When the type of objects is not going to be the same, we specify `Object` as the generic type.
7. Looping a List
Similarly to arrays, we use the for-each syntax to loop through objects in any `List` like `ArrayList`.
8. List println
We use the `println` statement to display the contents of any `List`.
9. LinkedList
The construction of a `LinkedList` and its methods to add, remove, change objects are the same as `ArrayList`.
10. Additional LinkedList methods
Some additional methods exist to add and remove objects to the beginning and end of the LinkedList.
11. Collection type similarities
ArrayList and LinkedList may look similar in methods and functionality, and that's intentional. Both types share the same `List` interface, allowing them to provide the same method names while working differently under the hood.
This is an example of polymorphism - a core concept in OOP. As we learned previously, polymorphism allows different types to use the same method names but implement them in unique ways, making code more flexible and reusable.
12. ArrayList vs LinkedList
As `ArrayList` and `LinkedList` look very similar, which is the best? Which should we use? The choice depends on how we use the data structure.
`ArrayList` objects are better at random access of the objects in the data structure. But they are not so fast at adding and removing objects since it requires moving a lot of objects in the `ArrayList` when an object is added or removed.
13. ArrayList vs LinkedList
`LinkedList` objects allow much faster addition/removal since the objects in the list don't have to be moved. Just a couple of pointers change when an object is added or removed. `LinkedList` are slower at random access of objects. Which data structure we use depends on whether we plan to add/remove more objects or just access objects in it.
14. Let's practice!
Let's create, populate, and use a few `List` objects.