Java Collections: The List and Collections Interfaces
To learn more about Java Collections, let's look at the List and Collections interfaces and how to interact with them.
Join the DZone community and get the full member experience.
Join For FreeLet'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.
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 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);
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.
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") )
names.remove(name);
} // 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.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 a common interface. This article provided a gentle introduction to looping over the items in a collection and adding/removing from a collection.
If you enjoyed this article and want to learn more about Java Collections, check out this collection of tutorials and articles on all things Java Collections.
Published at DZone with permission of Jay Sridhar, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Incident Response Guide
-
Using Render Log Streams to Log to Papertrail
-
Demystifying SPF Record Limitations
-
Best Practices for Securing Infrastructure as Code (Iac) In the DevOps SDLC
Comments