1. Collections Framework Data Structures
Let's talk about one of the most used set of classes in Java: the Collections Framework.
2. What's a framework
A framework is a set of reusable types that provide general functionality. We'll explore types shortly, but for now, know that classes and interfaces are examples of types. Frameworks simplify application development by offering well-tested components, so we don't have to build everything from scratch. Frameworks are delivered as Java packages and come with guidelines to help us use them effectively.
3. Java Types
We said a framework is a set of reusable types. What is a type in Java? There are two types in Java: primitives (we already know the eight primitive types) and reference types.
Reference types are everything outside primitives, including classes, interfaces, enums, and arrays. Because we cannot create new primitive types, we mean reference type when this course refers to a type. Reference types get their name because variables refer to objects of the specified type.
4. The Collections Framework
The Collections Framework is a built-in Java framework providing generic data structures for managing groups (or collections) of objects. It is part of the `java.util` package and works with objects only. Since it is in `java.util`, not `java.lang`, we need to import any types we use from the framework.
5. What about arrays?
The Collections Framework data structures are the array alternative. While arrays and collections have their differences, both are useful in different situations. Arrays are not resizable. Collections are. Arrays can store primitives or objects. Collections store only objects. Array elements must have the same type. Collections can hold different types of objects. Arrays have a special notation for declaring, initializing, and accessing elements. Collections have no special syntax and use methods to access elements.
6. Collections Framework Types
The framework has two main interfaces: `Collection` and `Map` both in `java.util`. These interfaces are implemented by several classes, which we explore later. Use import java.util.* to use any type in the framework.
7. Collection
Collection includes data structure types like `List`, which is an ordered list of objects, `Set`, an unordered collection with unique objects, and `Queue`, which behaves like the first-in-first-out line of people at a ticket booth.
Key methods for Collection objects are the `.add()` method to add objects and the `.remove()` method to remove objects.
8. Map
`Map` is a lookup-based data structure where we use keys to find values. It works like a phone book: a key (like a name) maps to a value (like a phone number). Both keys and values are objects.
Important methods for `Map` objects include the `.put()` method to add a key-value pair and the `.remove()` to remove one. There are many implementations of `Map`. We look at `HashMap` later.
9. Generics
All `Collection` and `Map` types let us define the type of objects they store using generics. Special syntax allows us to specify what type of objects a collection should hold. This concept is called parameterization.
For example, `ArrayList` is a type of `Collection` we'll see later.
We use generics to say the `ArrayList` will only hold `String` data.
10. Generics and type safety
Generics restricts what objects can go in a collection - making our code safer by catching errors when adding the wrong type (`Integer` here) into a collection.
11. Not using generics
While legal, not using generics is considered poor practice. It allows any type to be added.
12. Variable declaration and assignment
Collections variable declaration and assignment can be done separately.
13. Let's practice!
Let's dive into Collections and Maps!