//TestArrayToArrayList.java
//Illustrates the conversion of an array to an ArrayList.
//Also illustrates some variations on how to create arrays
//and how to process an ArrayList once it has been created
//from an array.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class TestArrayToArrayList
{
    public static void main(String[] args)
    {
        System.out.println();

        //First we create an array of integers, display it, put it into
        //an ArrayList of Integer, and then display the values again.
        //Here's one way to create an array of integers:
        Integer[] integers1 = new Integer[]
        {
            4, 1, 5, 3, 2
        };
        System.out.print("integers1: ");
        for (Integer i : integers1)
        {
            System.out.print(i + " ");
        }
        System.out.println();

        //Here's another way (that creates the same array as above):
        Integer[] integers2 =
        {
            4, 1, 5, 3, 2
        };
        System.out.print("integers2: ");
        for (int i : integers2)
        {
            System.out.print(i + " ");
        }
        System.out.println();

        //And now we create an ArrayList from an array, using
        //the static method Arrays.asList() from the Arrays class:
        ArrayList<Integer> integerArrayList
            = new ArrayList<Integer>(Arrays.asList(integers1));
        System.out.print("integerArrayList: ");
        for (int i : integerArrayList)
        {
            System.out.print(i + " ");
        }
        System.out.println("\n");

        //================================================================
        //Now we repeat what we did in the above three code segments, but
        //this time with strings instead of integers:
        String strings1[] = new String[]
        {
            "klm", "abc", "xyz", "pqr"
        };
        System.out.print("strings1: ");
        for (String s : strings1)
        {
            System.out.print(s + " ");
        }
        System.out.println();

        String strings2[] =
        {
            "klm", "abc", "xyz", "pqr"
        };
        System.out.print("strings2: ");
        for (String s : strings2)
        {
            System.out.print(s + " ");
        }
        System.out.println();

        ArrayList<String> stringArrayList
            = new ArrayList<>(Arrays.asList(strings1));
        System.out.print("stringArrayList: ");
        for (String s : stringArrayList)
        {
            System.out.print(s + " ");
        }
        System.out.println("\n");

        //================================================================
        //Now we sort each ArrayList according to the "natural order" of
        //its elements:
        integerArrayList.sort(null); //sort according to "natural order"
        System.out.print("integerArrayList sorted: ");
        for (int i : integerArrayList)
        {
            System.out.print(i + " ");
        }
        System.out.println();

        stringArrayList.sort(null); //sort according to "natural order"
        System.out.print("stringArrayList sorted: ");
        for (String s : stringArrayList)
        {
            System.out.print(s + " ");
        }
        System.out.println();

        System.out.println();

        String[] array =
        {
            "Hello", "World"
        };

        //Create ArrayList from array
        ArrayList<String> list = new ArrayList<>(Arrays.asList(array));
        System.out.println(list);

        //In Java 8 you can use
        list = Stream.of(array).collect(Collectors.toCollection(ArrayList::new));
        System.out.println(list);

        //In Java 9 you can use
        //List<String> list = List.of("Hello", "World");
        //System.out.println(list);
        //Initialization of an ArrayList in one line
        ArrayList<String> list2 = new ArrayList<String>()
        {
            {
                add("Hello");
                add("World");
            }
        };
        System.out.println(list2);

    }
}
/*  Output:

    integers1: 4 1 5 3 2
    integers2: 4 1 5 3 2
    integerArrayList: 4 1 5 3 2

    strings1: klm abc xyz pqr
    strings2: klm abc xyz pqr
    stringArrayList: klm abc xyz pqr

    integerArrayList sorted: 1 2 3 4 5
    stringArrayList sorted: abc klm pqr xyz

    [Hello, World]
    [Hello, World]
    [Hello, World]
*/
