//TestTreeSet.java

import java.util.ArrayList;
import java.util.Iterator;
import java.util.TreeSet;

public class TestTreeSet
{
    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> myArrayList = new ArrayList<>();
        for (char c : sChars)
        {
            myArrayList.add(c);
        }
        for (char c : myArrayList)
        {
            System.out.print(c);
        }
        System.out.println();
        System.out.println(myArrayList);
        System.out.println(myArrayList.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==============================");
        TreeSet<Character> myTreeSet = new TreeSet<Character>(myArrayList);
        for (char c : myTreeSet)
        {
            System.out.print(c);
        }
        System.out.println();
        System.out.println(myTreeSet);
        System.out.println(myTreeSet.size());

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

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

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

