"Hi, Amigo."

"Hey, Rishi."

"Ellie told me you wanted more examples of collections. I'll give you a few. Here is a list of collections and interfaces:"

Interface Class/implementation Description
List  ArrayList  List
 LinkedList  List
 Vector  Vector
 Stack  Stack
 Set    HashSet  Set
 TreeSet  Set
 SortedSet  Sorted set
Map  HashMap Map/dictionary
 TreeMap  Map/dictionary
 SortedMap  Sorted dictionary
 Hashtable  Hash-table

"Hmm. That's quite a lot. Four lists, three sets, and four maps."

"Yes, they are all different implementations of the List, Set and Map interfaces."

" What's the difference between these implementations?"

"That's exactly what we're going to talk about today. Just be patient."

"Do you have any other questions?"

" I know how to display a list on the screen. How do I display a Set or Map?"

"The elements of a List have a set order, so you can just use an index to display them. For a Set or Map, there is no specific order. In fact, the order of their elements can change as items are deleted or new items are added."

"Amazing."

"This is why special objects, called iterators, were invented to work with collection elements. They let you to go through all the elements in a collection, even if they have only names instead of indices (Map), or neither names nor indices (Set)."

"Here are some examples:"

Display elements of a Set
 public static void main(String[] args)
{
    Set&ltString> set = new HashSet&ltString>();
    set.add("Rain");
    set.add("In");
    set.add("Spain");

     // Get an iterator for the set
     Iterator&ltString> iterator = set.iterator();

    while (iterator.hasNext())        // Check if there is another element
    {
       // Get the current element and move to the next one
       String text = iterator.next();

        System.out.println(text);
    }
}
 
undefined
8
Task
New Java Syntax, level 8, lesson 2
Locked
Where does a Person come from?
n the Person class, declare the following variables: String name, int age, int weight, int money. In the main method, create a Person object and store a reference to it in the variable person. Hint: Use the following construct to create a Person object and assign a reference to that object to the va
Display elements of a List
public static void main(String[] args)
{
    List&ltString> list = new ArrayList&ltString>();
    list.add("Rain");
    list.add("In");
    list.add("Spain");

    Iterator&ltString> iterator = list.iterator();// Get an iterator for the list

    while (iterator.hasNext())      // Check if there is another element   
    {
        // Get the current element and move to the next one
        String text = iterator.next();

        System.out.println(text);
    }
}
Display elements of a Map
public static void main(String[] args)
{
    // All elements are stored in pairs
    Map<String, String> map = new HashMap<String, String>();
    map.put("first", "Rain");
    map.put("second", "In");
    map.put("third", "Spain");

    Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();

   while (iterator.hasNext())
    {
        // Get a key-value pair
        Map.Entry<String, String> pair = iterator.next();
        String key = pair.getKey();            // Key
        String value = pair.getValue();        // Value
        System.out.println(key + ":" + value);
    }
}

"Wow. I wonder what all that means."

"It's actually quite simple. First, we get a special object, an iterator, from the collection. The iterator has only two methods.

1. The next() method returns the next element in the collection.

2. The hasNext() method checks whether there are still elements that have not been returned by next()."

"OK. I think it's getting clearer now. Let me try to repeat back to you what I understood."

"So... First, we need to call the iterator() method on a collection to get this magic iterator object."

"Then we get elements one by one as long as there are any left to get. We get the next element in the collection by calling next(), and we check whether there are still elements in the collection by calling hasNext() on the iterator. Is that correct?"

"Yes, more or less. But wait for the good part."

"Java has shorthand notation for working with iterators. Following the pattern of while and for, one more special statement has been added: for each. It is also indicated using the keyword for."

"The for-each statement is only used for working with collections and containers. It uses an iterator implicitly, but we only see the returned element."

"Let me show you the longhand and shorthand ways to work with an iterator:"

Longhand
public static void main(String[] args)
{
  Set&ltString> set = new HashSet&ltString>();
    set.add("Rain");
    set.add("In");
    set.add("Spain");

    Iterator&ltString> iterator = set.iterator();
  while (iterator.hasNext())
  {
    String text = iterator.next();
    System.out.println(text);
  }
}
Shorthand
public static void main(String[] args)
{
    Set&ltString> set = new HashSet&ltString>();
    set.add("Rain");
    set.add("In");
    set.add("Spain");

   for (String text : set)   
    {
        System.out.println(text);
    }
}


"Note that the words highlighted in red or green are absent in the right part. In fact, three lines are replaced by one:"

Longhand
Iterator&ltString> iterator = set.iterator();
while (iterator.hasNext())
{
    String text = iterator.next();
Shorthand

for (String text : set)


"This looks gorgeous. I like it much better this way."

"Let's look at the shorthand version of the examples above:"

Display elements of a Set
public static void main(String[] args)
{
    Set&ltString> set = new HashSet&ltString>();
    set.add("Rain");
    set.add("In");
    set.add("Spain");

    for (String text : set)   
    {
        System.out.println(text);
    }
}
Display elements of a List
public static void main(String[] args)
{
    List&ltString> list = new ArrayList&ltString>();
    list.add("Rain");
    list.add("In");
    list.add("Spain");

     for (String text : list)        
    {
        System.out.println(text);
    }
}
Display elements of a Map
public static void main(String[] args)
{
    Map<String, String> map = new HashMap<String, String>(); 
    map.put("first", "Rain");
    map.put("second", "In");
    map.put("third", "Spain");

    for (Map.Entry<String, String> pair : map.entrySet())
    {
        String key = pair.getKey();                      // Key
        String value = pair.getValue();                  // Value
        System.out.println(key + ":" + value);
    }
}

"Now you're talking!"

"I'm glad you liked it."

undefined
8
Task
New Java Syntax, level 8, lesson 2
Locked
Favorite board games
Create a list of your top 5 favorite board games.