Traversing Collections

Hello friends,  lets have a small recap from the last post about “Collections”.

The Collection Interface

What is Collection?

Collection is an interface. The Collection interface is used to manipulate collections of objects.

Collections of objects can be in the form of  List, Set , Array etc.

List list = new ArrayList(c);

The Collection interface also exposes methods Stream stream() and Stream parallelStream(), for obtaining sequential or parallel streams from the underlying collection.


How can we traverse collections?

Traversing Collections

There are three ways to traverse collections:

 (1) using aggregate operations
 (2) with the for-each construct and
 (3) by using Iterators.

Aggregate Operations

We obtain obtain a stream of objects from collection and perform aggregate operations on it.
Obtain a stream as shown below:

List items = new ArrayList();
items.add("Lancer");
items.add("Audi");
items.add("Toyota");

Stream stream = items.stream();