Source of TestArrayToLinkedList.java


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

  7: import java.util.LinkedList;
  8: import java.util.Arrays;
  9: import java.util.stream.Collectors;
 10: import java.util.stream.Stream;

 12: public class TestArrayToLinkedList
 13: {
 14:     public static void main(String[] args)
 15:     {
 16:         System.out.println();

 18:         //First we create an array of integers, display it, put it into
 19:         //an LinkedList of Integer, and then display the values again.
 20:         //Here's one way to create an array of integers:
 21:         Integer[] integers1 = new Integer[]
 22:         {
 23:             4, 1, 5, 3, 2
 24:         };
 25:         System.out.print("integers1: ");
 26:         for (Integer i : integers1)
 27:         {
 28:             System.out.print(i + " ");
 29:         }
 30:         System.out.println();

 32:         //Here's another way (that creates the same array as above):
 33:         Integer[] integers2 =
 34:         {
 35:             4, 1, 5, 3, 2
 36:         };
 37:         System.out.print("integers2: ");
 38:         for (int i : integers2)
 39:         {
 40:             System.out.print(i + " ");
 41:         }
 42:         System.out.println();

 44:         //And now we create an LinkedList from an array, using
 45:         //the static method Arrays.asList() from the Arrays class:
 46:         LinkedList<Integer> integerLinkedList =
 47:             new LinkedList<Integer>(Arrays.asList(integers1));
 48:         System.out.print("integerLinkedList: ");
 49:         for (int i : integerLinkedList)
 50:         {
 51:             System.out.print(i + " ");
 52:         }
 53:         System.out.println("\n");

 55:         //================================================================
 56:         //Now we repeat what we did in the above three code segments, but
 57:         //this time with strings instead of integers:
 58:         String strings1[] = new String[]
 59:         {
 60:             "klm", "abc", "xyz", "pqr"
 61:         };
 62:         System.out.print("strings1: ");
 63:         for (String s : strings1)
 64:         {
 65:             System.out.print(s + " ");
 66:         }
 67:         System.out.println();

 69:         String strings2[] =
 70:         {
 71:             "klm", "abc", "xyz", "pqr"
 72:         };
 73:         System.out.print("strings2: ");
 74:         for (String s : strings2)
 75:         {
 76:             System.out.print(s + " ");
 77:         }
 78:         System.out.println();

 80:         LinkedList<String> stringLinkedList =
 81:             new LinkedList<>(Arrays.asList(strings1));
 82:         System.out.print("stringLinkedList: ");
 83:         for (String s : stringLinkedList)
 84:         {
 85:             System.out.print(s + " ");
 86:         }
 87:         System.out.println("\n");

 89:         //================================================================
 90:         //Now we sort each LinkedList according to the "natural order" of
 91:         //its elements:
 92:         integerLinkedList.sort(null); //sort according to "natural order"
 93:         System.out.print("integerLinkedList sorted: ");
 94:         for (int i : integerLinkedList)
 95:         {
 96:             System.out.print(i + " ");
 97:         }
 98:         System.out.println();

100:         stringLinkedList.sort(null); //sort according to "natural order"
101:         System.out.print("stringLinkedList sorted: ");
102:         for (String s : stringLinkedList)
103:         {
104:             System.out.print(s + " ");
105:         }
106:         System.out.println();

108:         System.out.println();

110:         String[] array =
111:         {
112:             "Hello", "World"
113:         };

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

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

123:         //In Java 9 you can use
124:         //List<String> list = List.of("Hello", "World");
125:         //System.out.println(list);
126:         //Initialization of an LinkedList in one line
127:         LinkedList<String> list2 = new LinkedList<String>()
128:         {
129:             {
130:                 add("Hello");
131:                 add("World");
132:             }
133:         };
134:         System.out.println(list2);

136:     }
137: }
138: /*  Output:

140:     integers1: 4 1 5 3 2
141:     integers2: 4 1 5 3 2
142:     integerLinkedList: 4 1 5 3 2

144:     strings1: klm abc xyz pqr
145:     strings2: klm abc xyz pqr
146:     stringLinkedList: klm abc xyz pqr

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

151:     [Hello, World]
152:     [Hello, World]
153:     [Hello, World]
154: */