package me.illuminatiproductions;
import java.util.*;
public class Main {
public static void main(String[] args) {
//Iterators
ArrayList<String> names = new ArrayList<>();
names.add("Noah");
names.add("Kody");
names.add("Damascus");
names.add("Lars");
names.add("Nadie");
//Loop through a collection with an iterator
Iterator<String> nameIterator = names.iterator();
System.out.println("Here's all of the names: ");
while(nameIterator.hasNext()){ //loop while there is another element ahead
System.out.println(nameIterator.next()); //returns and moves to next element
}
//Use a listIterator when working with list type collections
ListIterator<String> nameListIterator = names.listIterator();
System.out.println("Names: ");
while(nameListIterator.hasNext()){
System.out.println(nameListIterator.next());
}
//Now, make it go backwards
System.out.println("Names backwards: ");
while(nameListIterator.hasPrevious()){
System.out.println(nameListIterator.previous());
}
//iterator with hashmaps
HashMap<String, String> map = new HashMap<>();
map.put("key1", "value1");
map.put("key2", "value2");
Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> pair = it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
}
}
}