Ed has created and taught college courses since 2002 and has a Doctorate of Computer Science and three MS degrees. He has authored several tech books.
Java Collections Framework: Methods & Algorithms
Java is a mature programming platform that includes a collections framework. This framework is a collection of Java language components that has a set of common behaviors, which is a mandate of Java collections. The Java collections framework contains interfaces, implementations, and algorithms.
The benefits of using collections include:
- More efficient programming
- Better performing code
- Enhanced code portability
We can achieve these outcomes in Java because of the common behavior mandate. These behaviors support searching, adding, removing, and retrieving objects as well as the ability to iterate through the collection. We will look at the List interface to demonstrate how this all works.
To unlock this lesson you must be a Study.com Member.
Create your account
The List interface extends the Collections framework as well as the Iterable interface.
![]() |
The List interface has over 2 dozen native methods as well as the methods it inherits from the Collection and Iterable interfaces.
Working with Lists
To show how to work with the List interface, we will start by creating an ArrayList and adding values to the first four positions (0, 1, 2, and 3) using the add() method:
List<String> myList = new ArrayList<>();
myList.add('This');
myList.add('makes');
myList.add('no');
myList.add('sense');
We can retrieve a single element from the ArrayList by using the get() method.
System.out.println(myList.get(1));
Next, we will replace one of the values and then iterate through the ArrayList to print it to the console.
myList.set(2, 'perfect');
for (String theValue : myList) {
System.out.print(theValue + ' ');
}
Executing this code results in the following output: This make perfect sense
Searching and Sorting Lists
Searching and sorting are common operations for Lists. To demonstrate, let's create an ArrayList to hold names:
List<String> listOfNames = new ArrayList<>();
listOfNames.add('jimmy');
listOfNames.add('sally');
listOfNames.add('cindy');
listOfNames.add('rhona');
listOfNames.add('john');
listOfNames.add('brenda');
listOfNames.add('lona');
Our list consists of 7 names. We will perform three operations on this list: search, sort, and change the names so their first letter is capitalized. One step at a time, let's search our list to see if we can find Brenda:
if (listOfNames.contains('brenda')) {
System.out.println('Record found!');
}
else {
System.out.println('Record not found!');
}
Next, we will sort our list and then iterate through it printing one name at a time. We will use the sort() method:
Collections.sort(listOfNames);
for (String theName : listOfNames) {
System.out.println(theName);
}
Now that we have a sorted list of names, we can iterate through it and change the first character to a capital letter. After the list of names is updated, we will then iterate through it one final time to output the updated values. Our iterative loop will make a call to the size() method to determine how many elements are in the ArrayList:
for (int i=0; i < listOfNames.size(); i++) {
String tempName = listOfNames.get(i);
listOfNames.set(i, tempName.substring(0, 1).toUpperCase() + tempName.substring(1));
}
for (String theName : listOfNames) {
System.out.println(theName);
}
Instead of printing each name on a new line as shown above, we can also print them with a single call to the println() method:
System.out.println(listOfNames);
This results in the following output:
[Brenda, Cindy, Jimmy, John, Lona, Rhona, Sally]
Algorithms for Lists
From the example above, you have already seen the sort algorithm in use. There are several other algorithms available in Java that can be used to modify the contents of a List. Here are the most commonly used algorithms for List modification:
Algorithm | Description |
---|---|
binarySearch | uses a binary search algorithm |
copy | allows for the duplication of Lists |
fill | flushes all elements, replacing them with a specified value |
replaceAll | permits global find and replace operations |
reverse | puts the List elements in reverse order |
shuffle | puts the List elements in random order |
sort | sorts a List |
swap | changes out List elements |
To unlock this lesson you must be a Study.com Member.
Create your account
Java affords us the opportunity to work on a collection of interfaces, implementations, and algorithms, which is referred to as a collections framework. These collections enable us to write more efficient code quickly and to promote code portability. The List interface extends the Collections framework with 10 implemented classes including ArrayList. In addition, the List interface includes over 2 dozen methods that allow us to perform add, update, remove, search, and other element manipulation operations.
To unlock this lesson you must be a Study.com Member.
Create your account
Register to view this lesson
Unlock Your Education
See for yourself why 30 million people use Study.com
Become a Study.com member and start learning now.
Become a MemberAlready a member? Log In
BackResources created by teachers for teachers
I would definitely recommend Study.com to my colleagues. It’s like a teacher waved a magic wand and did the work for me. I feel like it’s a lifeline.