Over a million developers have joined DZone.

Java Collections (Part 1)

DZone's Guide to

Java Collections (Part 1)

As we kick off a series on Java Collections, let's start with a primer to the List and Collections interfaces and how to interact with them.

· Java Zone ·
Free Resource

Bring content to any platform with the open-source BloomReach CMS. Try for free.

Let's learn about Java Collections and the various operations applicable to collections in general. Since the Collection interface is implemented by most of the classes in the Collections Framework (except Map and friends), the methods described here apply to all those classes.

For reference, here is the Collection hierarchy.

Java Collections Part 1

Iterating Over a Collection

All members of the collection hierarchy support iteration using the enhanced for-each loop as well as an iterator. Use a collection with the enhanced for-each loop as follows:

Iterable<String> names = Arrays.asList("Mary", "Annie", "Anna", "Margaret", "Helen", "Elsie", "Lucy", "Dorothy", "Mary", "Margaret"); 
for (String name : names) { 

With Java 8, you can also use forEach() with a lambda expression.


Of course, you can always use the old-fashioned Iterator to loop over the elements:

Iterator<String> iter = names.iterator(); 
while( iter.hasNext() ) { 

Why not combine both the loop and the lambda? Use the iterator to skip some elements and the forEachRemaining() method to print the rest.

Iterator<String> iter = names.iterator(); 
while( iter.hasNext() ) { 
  String next = iter.next(); 
  if ( next.startsWith("H") ) break;

Adding Items to a Collection

Add individual elements to a collection using add(). In the following, we create a Set and add individual elements to it.

Set<String> names = new LinkedHashSet<>(); 
names.forEach(System.out::println); // prints: Mary Annie Anna

Add all elements from one collection to another using addAll(). How about adding all elements from a List to the above set? Note that even though the value “Mary” is added twice, only a single instance is stored since the target container is a Set.

List<String> remaining = Arrays.asList("Margaret", "Helen", "Elsie", "Mary"); 
names.forEach(System.out::println); // prints: Mary Annie Anna Margaret Helen Elsie

Also, since we used a LinkedHashSet, the iteration order is the same as the insertion order. If you do not need this behavior, use a HashSet for more efficiency.

Removing Items from a Collection

Let us now see how to remove items from a collection. Depending on your need, there are a bunch of methods.

Removing a Single Item

Remove a single object from a collection using remove().  This method uses the item’s equals() method to perform the comparison. It method returns true if the item was found and removed.

Collection<String> names = new ArrayList<>(); 
names.addAll(Arrays.asList("Mary", "Annie", "Anna", "Margaret")); 
if ( names.remove("Anna") ) System.out.println("==> Removed <=="); 
names.forEach(System.out::println); // prints ==> Removed <== Mary Annie Margaret

Note: The List returned by Arrays.asList() is an immutable list. Attempting to remove (or add) items to such a list results in an UnsupportedOperationException as shown below:

Collection<String> names = Arrays.asList("Mary", "Annie", "Anna", "Margaret"); 
if ( names.remove("Anna") ) System.out.println("==> Removed <=="); 
names.forEach(System.out::println); // throws: java.lang.UnsupportedOperationException: remove

Removing Multiple Items

Use removeAll() to remove all elements found in one collection from another. (Basically does a Set Difference operation.)

Collection<String> names = new ArrayList<>(); 
names.addAll(Arrays.asList("Mary", "Annie", "Anna", "Margaret")); 
names.removeAll(Arrays.asList("Annie", "Margaret")); 
names.forEach(System.out::println); // prints: Mary Anna

Removing Matching Items

Selective removal is performed by using the removeIf() method which accepts a predicate. Below, we pass a lambda expression to remove names containing the string “y”.

Collection<String> names = new LinkedHashSet<>(); 
names.addAll(Arrays.asList("Elsie", "Lucy", "Dorothy", "Mary", "Margaret")); 
names.removeIf(name -> name.contains("y")); 
names.forEach(System.out::println); // prints: Elsie Margaret

Removing in a Loop

The method removeIf() is a new addition to Java 8. Before this, we had to resort to this code for the same effect.

for (Iterator<String> iter = names.iterator() ; 
iter.hasNext() { 
String name = iter.next(); 
    if ( name.contains("y") ) iter.remove();

By the way, the following is the wrong way to go about removing items in a loop. Do not do this or you end up with a ConcurrentModificationException exception.

Collection<String> names = new ArrayList<>(); 
names.addAll(Arrays.asList("Mary", "Annie", "Anna", "Margaret", "Helen", "Elsie", "Lucy", "Margaret", "Dorothy")); 
for (String name : names) { 
  if ( name.contains("t") ) 
} // throws ConcurrentModificationException

Retain All Items

Method retainAll() retains all items found in both collections. Basically, you end up with the Intersection of both collections.

Collection<String> names = new ArrayList<>(); 
names.addAll(Arrays.asList("Mary", "Annie", "Anna", "Margaret", "Helen")); 
Collection<String> keepEm = Arrays.asList("Elsie", "Lucy", "Margaret", "Dorothy", "Mary", "Helen", "Margaret"); 
names.forEach(System.out::println); // prints: Mary Margaret Helen

Finally, method clear() empties the collection of all elements.


Java Collections support a wide variety of concrete implementation classes with a common interface. This article provided a gentle introduction to looping over the items in a collection and adding/removing from a collection.

BloomReach CMS: the API-first CMS of the future. Open-source & enterprise-grade. - As a Java developer, you will feel at home using Maven builds and your favorite IDE (e.g. Eclipse or IntelliJ) and continuous integration server (e.g. Jenkins). Manage your Java objects using Spring Framework, write your templates in JSP or Freemarker. Try for free.

java ,collection ,map ,list

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}