Java: Using Lists
USING THE LIST INTERFACE
Sarah Holderness
PLURALSIGHT AUTHOR
@dr_holderness
Assumptions
Basic Java knowledge about using arrays
and inheritance.
– The Collection and List Interfaces
– List operations
What this course
– The problem with plain arrays
will focus on
– Practical example analyzing Tweet
data
What is a Collection?
A Java List inherits from a Collection, which is a very
general container for any Java Objects
COLLECTION
The Collection Interface
A Collection is an Interface, like a Blueprint, that says you
MUST implement these methods to be a Collection:
COLLECTION
•add()
•remove()
•size()
•contains()
•clear()
•isEmpty()
•… and more!
Types of Collections
COLLECTION INTERFACE
LIST SET
An ordered group A group of unique values
of values that are without an index
indexed
numerically
List and Set are two subtypes of
Collection and there are many more!
Types of Collections
COLLECTION INTERFACE
LIST SET
An ordered group A group of unique values
of values that are without an index
indexed
numerically
In this course we’ll cover List!
Types of Collections - Lists
LIST
An ordered group of values that are indexed numerically
0 1 2 3 4 5 6 7 8
A List is ordered
List and Collection Methods
COLLECTION
Collection methods:
•add() •remove() The methods a Collection
must implement.
•clear() •size()
•contains() •toArray()
•equals() •… and more!
LIST
List is also an interface.
List methods: A List must also implement
•add() at index •set() these methods.
•remove() at index •subList()
•indexOf() •… and more!
Can We Initialize a List Yet?
'List' is abstract;
List<String> languages = new List<String>(); cannot be instantiated
Since List is ALSO an Interface, we need a non-abstract
(non-interface) implementation of a List to initialize.
•Build our own?
•ArrayList
•LinkedList
•Vector
•Stack
Can We Initialize a List Yet?
'List' is abstract;
List<String> languages = new List<String>(); cannot be instantiated
Since List is ALSO an Interface, we need a non-abstract
(non-interface) implementation of a List to initialize.
•Build our own?
Let’s start with ArrayList -
•ArrayList one of the most common Lists used
•LinkedList
•Vector
•Stack
Initializing a List
Our List will store the names of some programming languages as
Strings.
List<String> languages = new ArrayList<>();
Initializing a List
Our List will store the names of some programming languages as
Strings.
List<String>
List String languages = new ArrayList<>();
ArrayList<>
General List What’s in
Specific Type
Leave String out
Interface the List of List here, it’s inferred
Initializing a List
Why not make the variable an ArrayList on the left instead of List?
ArrayList<String> languages = new ArrayList<>
ArrayList ArrayList<>();
This is not wrong…
… but using a general List on the left lets
us switch out the type of List we use on
the right without changing any other code!
Using a general
List<String> languages = new LinkedList
List LinkedList<>(); List on the left is
more flexible
List<String> languages = new MyList
List MyList<>();
Lists Can Hold any Type of Object
List of Integers
List<Integer> numbers = new ArrayList<>();
List of class MyProduct objects
List<MyProduct> products = new LinkedList<>();
List of any type of Object
List<Object> objects = new ArrayList<>();
Adding Elements to a List
List<String> languages = new ArrayList<>();
languages.add("Java");
languages.add("Python");
languages.add("JavaScript");
languages.add(“Go");
System.out.println(languages.size());
Console output
> 4
Iterating through Elements in a List
List<String> languages = new ArrayList<>();
languages.add("Java");
languages.add("Python");
languages.add("JavaScript");
languages.add(“Go");
for (String language : languages)
System.out.println(language);
Console output
> Java
Python
JavaScript
Go
Iterating through Elements in a List
List<String> languages = new ArrayList<>();
languages.add("Java");
languages.add("Python");
languages.add("JavaScript");
languages.add(“Go");
Create a variable the Followed by a :
same type as the base and the List’s name
type of the List
for (String language : languages) Inside the loop body you can use
language to reference each
System.out.println(language); individual String in the List.
Accessing Elements in a List
List<String> languages = new ArrayList<>();
languages.add("Java");
languages.add("Python");
languages.add("JavaScript"); Java Python JavaScript Go
languages.add(“Go");
0 1 2 3
System.out.println(languages.indexOf("Python"));
System.out.println(languages.get(2));
Console output
> 1
JavaScript
Removing Elements from a List
List<String> languages = new ArrayList<>();
languages.add("Java");
languages.add("Python");
languages.add("JavaScript");
languages.add(“Go"); Java Python JavaScript Go
languages.remove("Python"); Java JavaScript Go
languages.remove(2); Java JavaScript
Why can't we just use a
regular Array?
The Problem with Arrays
String[] languages = {"Java", "C#", "Go"}; Java C# Go
0 1 2
languages[3] = "JavaScript";
languages[4] = “Python"; We originally created an So indices 3 and 4
array of size 3 and arrays don’t exist.
can’t be resized.
Console output
> Exception in thread "main"
java.lang.ArrayIndexOutOfBoundsException: 3
at Main.main(Main.java:10)
Creating a Larger Array
String[] languages = new String[8];
languages[0] = "Java";
languages[1] = “C#";
languages[2] = "Go";
languages[3] = “Python”;
Java C# Go Python null null null null
Empty cells
Size is 8.
are null.
That doesn’t indicate there
are 4 elements in the array.
Removing Elements from an Array
Java C# Go Python null null null null
To remove C# can
we just set that
value to null?
Java Go Python null null null null
How do we know where the
empty cells are?
Removing Elements from an Array
Java C# Go Python null null null null
To remove C# we have to
shift all of the following
elements over.
Java C# Go Python null null null null
Java Go Python null null null null
C# is removed and
Java Go Python null null null null null
the gap is filled.
– Array length is immutable
– Resizing an array requires creating a
larger array and copying each item
Summary:
– We need to keep track of how many
The problems with items are in the array
plain old arrays – We need to check for nulls
– If we remove an item we need to shift
the elements over to fill the empty space
The good news is the List
interface solves these
problems!
Converting a List to an Array
List<String> languages = new ArrayList<String>();
languages.add("Java");
languages.add("C#");
languages.add("Python");
languages.add("JS");
languages.add("Go");
toArray() returns an Array of
Object[] langsArray = languages.toArray(); general Objects by default…
So passing the type
String[] langsArray = languages.toArray(new String[0]); as a parameter in
this way is preferred.
Getting Part of a List with subList()
List<String> languages = new ArrayList<String>();
languages.add("Java");
languages.add("C#"); Java C# Python JS Go
languages.add("Python");
languages.add("JS");
languages.add("Go");
Java C# Python
List<String> someLangs = languages.subList(0, 3);
(from index - inclusive,
to index - exclusive)
subList() Returns a View of the List
List<String> languages = new ArrayList<String>();
languages.add("Java");
languages.add("C#"); Java C# Groovy JS Go
languages.add("Python");
languages.add("JS");
languages.add("Go");
Java C# Groovy
List<String> someLangs = languages.subList(0, 3);
someLangs.set(2, "Groovy");
Changing a value in the subList will also
change the original List. Because subList()
returns a view of the original List.