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

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

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

        //First we create an array of integers, display it, put it into
        //an LinkedList 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 LinkedList from an array, using
        //the static method Arrays.asList() from the Arrays class:
        LinkedList<Integer> integerLinkedList =
            new LinkedList<Integer>(Arrays.asList(integers1));
        System.out.print("integerLinkedList: ");
        for (int i : integerLinkedList)
        {
            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();

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

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

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

        System.out.println();

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

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

        //In Java 8 you can use
        list = Stream.of(array).collect(Collectors.toCollection(LinkedList::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 LinkedList in one line
        LinkedList<String> list2 = new LinkedList<String>()
        {
            {
                add("Hello");
                add("World");
            }
        };
        System.out.println(list2);

    }
}
/*  Output:

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

    strings1: klm abc xyz pqr
    strings2: klm abc xyz pqr
    stringLinkedList: klm abc xyz pqr

    integerLinkedList sorted: 1 2 3 4 5
    stringLinkedList sorted: abc klm pqr xyz

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

