//TestHashSet.java

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;

public class TestHashSet
{
    public static void main(String[] args)
    {
        //Create an array of char to contain the characters
        //of "Hello, world!" and then display them.
        System.out.println("1==============================");
        String s = "Hello, world!";
        char[] sChars = s.toCharArray();
        for (char c : sChars)
        {
            System.out.print(c);
        }
        System.out.println();
        System.out.println(sChars);
        System.out.println(sChars.length);

        //Create an ArrayList of Character to contain the
        //characters of "Hello, world!" and then display them.
        System.out.println("2==============================");
        ArrayList<Character> arrayList = new ArrayList<>();
        for (char c : sChars)
        {
            arrayList.add(c);
        }
        for (char c : arrayList)
        {
            System.out.print(c);
        }
        System.out.println();
        System.out.println(arrayList);
        System.out.println(arrayList.size());

        //Can't use sChars in place of arrayList in the following
        //HashSet constructor because it's not a "container of objects".
        System.out.println("3==============================");
        HashSet<Character> myHashSet = new HashSet<Character>(arrayList);
        for (char c : myHashSet)
        {
            System.out.print(c);
        }
        System.out.println();
        System.out.println(myHashSet);
        System.out.println(myHashSet.size());

        //Display the HashSet using an Iterator.
        System.out.println("4==============================");
        Iterator<Character> cIter = myHashSet.iterator();
        while (cIter.hasNext())
        {
            System.out.print(cIter.next());
        }
        System.out.println();
        System.out.println(myHashSet.size());

        //Display the HashSet using a Stream and a lambda functions.
        System.out.println("5==============================");
        myHashSet.stream().forEach(c -> System.out.print(c));
        System.out.println();
        System.out.println(myHashSet.size());

        //Display the HashSet using a Stream and a method reference.
        System.out.println("6==============================");
        myHashSet.stream().forEach(System.out::print);
        System.out.println();
        System.out.println(myHashSet.size());
    }
}
/*  Output:
    1==============================
    Hello, world!
    Hello, world!
    13
    2==============================
    Hello, world!
    [H, e, l, l, o, ,,  , w, o, r, l, d, !]
    13
    3==============================
    !deHl,orw
    [ , !, d, e, H, l, ,, o, r, w]
    10
    4==============================
    !deHl,orw
    10
    5==============================
    !deHl,orw
    10
    6==============================
    !deHl,orw
    10
*/

