Source of CreatingLinkedLists.java


  1: //CreatingLinkedLists.java
  2: //Illustrates various ways to create a LinkedList.

  4: import java.util.LinkedList;
  5: import java.util.Arrays;
  6: import java.util.List;
  7: import java.util.stream.Collectors;
  8: import java.util.stream.Stream;

 10: public class CreatingLinkedLists
 11: {
 12:     public static void main(String[] args)
 13:     {
 14:         //First we create an array of integers for use in some of
 15:         //the following examples, and then display its values.
 16:         Integer[] arrayOfInteger =
 17:         {
 18:             4, 1, 5, 3, 2
 19:         };
 20:         System.out.print("arrayOfInteger: ");
 21:         for (int i : arrayOfInteger)
 22:         {
 23:             System.out.print(i + " ");
 24:         }
 25:         System.out.println();

 27:         //1Now we create a LinkedList from the above array using
 28:         //the static method Arrays.asList() from the Arrays class:
 29:         LinkedList<Integer> linkedListOfInteger1 =
 30:             new LinkedList<>(Arrays.asList(arrayOfInteger));
 31:         System.out.println("linkedListOfInteger1: " + linkedListOfInteger1);

 33:         //2Next we create a LinkedList from the above array using
 34:         //the Java 8 Stream "collector" approach:
 35:         LinkedList<Integer> linkedListOfInteger2 =
 36:             Stream
 37:             .of(arrayOfInteger)
 38:             .collect(Collectors.toCollection(LinkedList::new));
 39:         System.out.println("linkedListOfInteger2: " + linkedListOfInteger2);

 41:         //3In this code segment the last line "proves" that if we use an
 42:         //interface as the type rather than the class, we still get the
 43:         //class object we expect.
 44:         List<Integer> linkedListOfInteger3 =
 45:             Stream
 46:             .of(arrayOfInteger)
 47:             .collect(Collectors.toCollection(LinkedList::new));
 48:         System.out.println("linkedListOfInteger3: " + linkedListOfInteger3);
 49:         System.out.println(linkedListOfInteger3.getClass().getName());

 51:         //4With Java 9 we can shorten the above approach to this:
 52:         List<Integer> linkedListOfInteger4 = List.of(4, 1, 5, 3, 2);
 53:         System.out.println("linkedListOfInteger4: " + linkedListOfInteger4);
 54:         System.out.println(linkedListOfInteger4.getClass().getName());
 55:         //But note that we get a somewhat different class ...

 57:         //5We can now also intialize an LinkedList directly with a code block:
 58:         LinkedList<Integer> linkedListOfInteger5 = new LinkedList<Integer>()
 59:         {
 60:             {
 61:                 add(4);
 62:                 add(1);
 63:                 add(5);
 64:                 add(3);
 65:                 add(2);
 66:             }
 67:         };
 68:         System.out.println("linkedListOfInteger5: " + linkedListOfInteger5);

 70:         System.out.println();

 72:         //========================================================
 73:         //Exercise:
 74:         //Repeat what we did in the above five code segments, but
 75:         //with strings instead of integers, and here is a starting
 76:         //array of strings to use:
 77:         String arrayOfString[] =
 78:         {
 79:             "klm", "abc", "xyz", "pqr"
 80:         };
 81:         System.out.print("arrayOfString: ");
 82:         for (String s : arrayOfString)
 83:         {
 84:             System.out.print(s + " ");
 85:         }
 86:         System.out.println();
 87:     }
 88: }
 89: /*  Output:
 90:     arrayOfInteger: 4 1 5 3 2
 91:     linkedListOfInteger1: [4, 1, 5, 3, 2]
 92:     linkedListOfInteger2: [4, 1, 5, 3, 2]
 93:     linkedListOfInteger3: [4, 1, 5, 3, 2]
 94:     java.util.LinkedList
 95:     linkedListOfInteger4: [4, 1, 5, 3, 2]
 96:     java.util.ImmutableCollections$ListN
 97:     linkedListOfInteger5: [4, 1, 5, 3, 2]

 99:     arrayOfString: klm abc xyz pqr
100: */