ArrayList and adding objects and Iterating over Collections


Declaring an ArrayList and adding objects

We can create an ArrayList (following the List interface):

List aListOfFruits = new ArrayList();
Version ≥ Java SE 5
List<String> aListOfFruits = new ArrayList<String>();
Version ≥ Java SE 7
List<String> aListOfFruits = new ArrayList<>();

Now, use the method add to add a String:

aListOfFruits.add("Melon");
aListOfFruits.add("Strawberry");

In the above example, the ArrayList will contain the String "Melon" at index 0 and the String "Strawberry" at index 1.

Also we can add multiple elements with addAll(Collection<? extends E> c) method

List<String> aListOfFruitsAndVeggies = new ArrayList<String>();
aListOfFruitsAndVeggies.add("Onion");
aListOfFruitsAndVeggies.addAll(aListOfFruits);

Now "Onion" is placed at 0 index in aListOfFruitsAndVeggies, "Melon" is at index 1 and "Strawberry" is at index 2.


Iterating over Collections

Iterating over List

List<String> names = new ArrayList<>(Arrays.asList("Tiya", "Riya", "Diya"));
//Version ≥ Java SE 8
names.forEach(System.out::println);

If we need parallelism use

names.parallelStream().forEach(System.out::println);
Version ≥ Java SE 5
for (String name : names) {
 System.out.println(name);
}
Version < Java SE 5
for (int i = 0; i < names.size(); i++) {
 System.out.println(names.get(i));
}
Version ≥ Java SE 1.2
//Creates ListIterator which supports both forward as well as backward traversel
ListIterator<String> listIterator = names.listIterator();
 
//Iterates list in forward direction
while(listIterator.hasNext()){
 System.out.println(listIterator.next());
}
 
//Iterates list in backward direction once reaches the last element from above iterator in forward
direction
while(listIterator.hasPrevious()){
 System.out.println(listIterator.previous());
}

Iterating over Set

Set<String> names = new HashSet<>(Arrays.asList("Clementine", "Duran", "Mike"));
Version ≥ Java SE 8
names.forEach(System.out::println);
Version ≥ Java SE 5
for (Iterator<String> iterator = names.iterator(); iterator.hasNext(); ) {
 System.out.println(iterator.next());
}
 
for (String name : names) {
 System.out.println(name);
}
 
//Version < Java SE 5
Iterator iterator = names.iterator();
while (iterator.hasNext()) {
 System.out.println(iterator.next());
}

Iterating over Map

Map<Integer, String> names = new HashMap<>();
names.put(1, "Clementine");
names.put(2, "Duran");
names.put(3, "Mike");
Version ≥ Java SE 8
names.forEach((key, value) -> System.out.println("Key: " + key + " Value: " + value));
Version ≥ Java SE 5
for (Map.Entry<Integer, String> entry : names.entrySet()) {
 System.out.println(entry.getKey());
 System.out.println(entry.getValue());
}
 
// Iterating over only keys
for (Integer key : names.keySet()) {
 System.out.println(key);
}
// Iterating over only values
for (String value : names.values()) {
 System.out.println(value);
}
//Version < Java SE 5
Iterator entries = names.entrySet().iterator();
while (entries.hasNext()) {
 Map.Entry entry = (Map.Entry) entries.next();
 System.out.println(entry.getKey());
 System.out.println(entry.getValue());
}

Basic Programs