Chapter 7 - Java for Beginners Course

Core Collection Interfaces

In the next sections we’ll introduce some of the common data structures offered by Java as part of the Java Collections Framework.

This framework offers a core set of interfaces that defines the contract available to users of the different types of collections.

Basic classes and interfaces in the collections framework

The diagram above shows the hierarchy of the interfaces (in gray background) and classes (in white background), with each subinterface or subclass providing a more specific implementation of a data structure. The diagram only covers the classes and interfaces we’ll cover in this section.

It’s important to note that all these interfaces and classes are generic, with the type parameter representing the type of element they are storing, for example a list of integer numbers can be represented as List<Integer>.

Below is a quick summary from top-to-bottom of the interfaces in the diagram:

  • Collection: A collection is a group of elements of the same type. The collection interface provides no further details about how the objects are stored, but it provides the common behaviors across all collection types. For example, the Collection contract specifies methods that allow users to add/remove objects and methods to iterate over the objects in the collection. Sub-interfaces start adding more details about the properties of the data structures, like List and Set.

  • List: In a List, elements are ordered, in a similar fashion as we saw for arrays in the previous section. This means that the objects stored in the list are in a sequence, and we have control over the position/index at which elements are inserted. We can also access objects by their index in the List. Lastly, lists allow duplicates, meaning you can store the same object more than once.

  • Set: Sets in Java are modelled after the concept of a set in mathematics. They’re a collection of objects that don’t allow duplicate objects. This interface doesn’t provide a notion of an index for each object.

  • Map: Maps are data structures that allow us to efficiently map keys to value objects. For example, think about a dictionary, where each word is mapped to one or more meanings, where the words are the keys and the meanings are the values associated with each key. The Map interface isn’t a type of Collection as they’re data structures with different purposes.

We’ll go into more detail using the concrete implementations shown in the diagram in the next sections.

It’s important to read the documentation of the actual implementation classes before using them.
Code in GitHub

Get the code for this tutorial using the links below.

Project Repo
Download code for this step
Dependencies

This is a list of recommended tutorials or courses that might be useful before starting this one.

Contents
Welcome to the Course!
Course Introduction
Chapter 1 - Building Blocks
Quick introduction to Java Variables Classes And Objects Class Example - Defining a class Object Examples - Creating instances Java Application Example - Running our first app Accessing class members - The dot operator Packages - Organizing the code
Chapter 2 - Primitives and Operators
Primitives Arithmetic Operators Assignment Operator Unary Operators Equality and Relational Operators Conditional Operators
Chapter 3 - Statements and Control Flow
Expressions Statements If-Then Statement If-Then-Else Statement More If Statements Switch Statement While and Do-While Statements For Statement Branching Statements Exception Handling
Chapter 4 - Code Example
Example Project - A Simple Vending Machine Adding money Delivering Items Giving Change
Chapter 5 - Classes and Interfaces
Introduction Access Level Modifiers Class Declaration - Class, Methods and Fields Class Declaration - Constructors Inheritance Basics Inheritance - Constructors Inheritance - Methods and Fields Polymorphism Abstract Classes and Methods Interfaces Static Class Members Class Composition Final Classes and Class Members Generic Classes
Chapter 6 - Base Object Behaviors
Introduction Type Comparison Type Casting Object Equality - The Contract Object Equality - Common Pitfalls Object String Representation Garbage Collection Object Comparison Primitive Wrappers and Autoboxing
Chapter 7 - Data Structures
Introduction Arrays - Declaration and Creation Arrays - Basic Operations Core Collection Interfaces List and ArrayList - Basic Operations ArrayList Internals Introduction to Hash Tables Map and HashMap - Basic Operations Set and HashSet - Basic Operations
Chapter 8 - Anonymous classes and lambdas
Introduction Filtering a List Anonymous Classes Lambdas Built-in Functional Interfaces
Chapter 9 - Streams
Introduction Creating Streams Intermediate Operations Terminal Operations