Java Collections – Part 1

Java Collections Framework: learn about adding items, iterating over a collection and removing items.

“Success is not final, failure is not fatal: it is the courage to continue that counts.”
― Winston S. Churchill

Check out the first part of this Java Collections Series: Introduction.

1. Introduction

Let us 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

2. 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) {
    System.out.println(name);
}

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

names.forEach(System.out::println);

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

Iterator<String> iter = names.iterator();
while( iter.hasNext() ) {
    System.out.println(iter.next());
}

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;
}
iter.forEachRemaining(System.out::println);

3. 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.add("Mary");
names.add("Annie");
names.add("Anna");
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.addAll(remaining);
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.

4. 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.

4.1. 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

4.2. 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

4.3. 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

4.4. 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") ) names.remove(name);
}

// throws ConcurrentModificationException

4.5. 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.retainAll(keepEm);
names.forEach(System.out::println);

// prints:
Mary
Margaret
Helen

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

Summary

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

See Also