Source of TestLinkedListOperations.java


  1: //TestLinkedListOperations.java
  2: //Illustrates most of what are (probably) the most useful and
  3: //frequently used LinkedList methods. As you experiment with this
  4: //program, you should check the official LinkedList documentation
  5: //and compare what you see there with what you see here.
  6: //See TestLinkedList_toArray.java for ilustrations of toArray().
  7: //But note that you must always decide carefully which kind of
  8: //implentation you should use for a given application.

 10: import java.util.LinkedList;
 11: import java.util.Arrays;

 13: public class TestLinkedListOperations
 14: {
 15:     public static void main(String[] args)
 16:     {
 17:         //Illustrates the default constructor and methods add(value),
 18:         //and size(), as well as indexOf(value) and lastIndexOf(value).
 19:         //Creates a new (empty) LinkedList of Integer and displays its size.
 20:         //Then adds some values and displays the size again.
 21:         //Note that autoboxing happens as the values are added.
 22:         //Then displays the first and last index locations of the value 2.
 23:         //Finally, displays all of the values.
 24:         System.out.println("=====1==============================");
 25:         LinkedList<Integer> linkedList1 = new LinkedList<>();
 26:         System.out.println("Size = " + linkedList1.size());
 27:         linkedList1.add(5);
 28:         linkedList1.add(2);
 29:         linkedList1.add(9);
 30:         linkedList1.add(4);
 31:         linkedList1.add(7);
 32:         linkedList1.add(2);
 33:         System.out.println("Size = " + linkedList1.size());
 34:         System.out.println("First index of 2 = " + linkedList1.indexOf(2));
 35:         System.out.println("Last index of 2 = " + linkedList1.lastIndexOf(2));
 36:         for (int i : linkedList1)
 37:         {
 38:             System.out.print(i + " ");
 39:         }
 40:         System.out.println();

 42:         //Illustrates methods add(index, value), contains(value)
 43:         //remove(index) and remove(value), removeIf(condition),
 44:         //as well as how to remove a range of values, which involves
 45:         //the use of subList(start, end).
 46:         System.out.println("=====2==============================");
 47:         System.out.println("Is 6 present? " + linkedList1.contains(6));
 48:         linkedList1.add(3, 6);
 49:         for (int i : linkedList1)
 50:         {
 51:             System.out.print(i + " ");
 52:         }
 53:         System.out.println();
 54:         System.out.println("Is 6 present? " + linkedList1.contains(6));
 55:         linkedList1.remove(5); //Note carefully. Compare with remove() below.
 56:         for (int i : linkedList1)
 57:         {
 58:             System.out.print(i + " ");
 59:         }
 60:         System.out.println();
 61:         Integer iObj = 5;
 62:         linkedList1.remove(iObj); //Note carefully. Compare with remove() above.
 63:         for (int i : linkedList1)
 64:         {
 65:             System.out.print(i + " ");
 66:         }
 67:         System.out.println();
 68:         linkedList1.removeIf(i -> i % 2 == 1);
 69:         for (int i : linkedList1)
 70:         {
 71:             System.out.print(i + " ");
 72:         }
 73:         System.out.println();
 74:         //linkedList1.removeRange(1, 3);
 75:         //Somewhat surprisingly, the above line will not compile.
 76:         //The reason is simple: it's a protected method. The reason
 77:         //it's protected is a little more complicated and we do not
 78:         //discuss it here. Google will fill you in ...
 79:         //Here's the alternative:
 80:         linkedList1.subList(1, 3).clear();
 81:         for (int i : linkedList1)
 82:         {
 83:             System.out.print(i + " ");
 84:         }
 85:         System.out.println();

 87:         //Illustrates LinkedList constructor that takes a container as input.
 88:         //Uses the same values as for the first container.
 89:         System.out.println("=====3==============================");
 90:         LinkedList<Integer> linkedList2 =
 91:             new LinkedList<>(Arrays.asList(5, 2, 9, 4, 7, 2));
 92:         for (int i : linkedList2)
 93:         {
 94:             System.out.print(i + " ");
 95:         }
 96:         System.out.println();

 98:         //Illustrate clear() and isEmpty().
 99:         System.out.println("=====4==============================");
100:         linkedList2.clear();
101:         for (int i : linkedList2)
102:         {
103:             System.out.print(i + " ");
104:         }
105:         System.out.println("\nAbove line contains values after clear().");
106:         if (linkedList2.isEmpty())
107:         {
108:             System.out.println("The LinkedList is now empty.");
109:         }

111:         //Illustrate sort(), the default version as well as two
112:         //different lambda functions for determining how to sort.
113:         LinkedList<Integer> linkedList3 =
114:             new LinkedList<>(Arrays.asList(5, 2, 9, 4, 7, 2));
115:         linkedList3.sort(null); //Sorts using the "natural order"
116:         for (int i : linkedList3)
117:         {
118:             System.out.print(i + " ");
119:         }
120:         System.out.println();
121:         //The following two lines both sort in reverse,
122:         //but the second is the preferred option.
123:         linkedList3.sort((i1, i2) -> i2 - i1);
124:         linkedList3.sort((i1, i2) -> Integer.compare(i2, i1));
125:         for (int i : linkedList3)
126:         {
127:             System.out.print(i + " ");
128:         }
129:         System.out.println();
130:     }
131: }
132: /*  Output:
133:     =====1==============================
134:     Size = 0
135:     Size = 6
136:     First index of 2 = 1
137:     Last index of 2 = 5
138:     5 2 9 4 7 2
139:     =====2==============================
140:     Is 6 present? false
141:     5 2 9 6 4 7 2
142:     Is 6 present? true
143:     5 2 9 6 4 2
144:     2 9 6 4 2
145:     2 6 4 2
146:     2 2
147:     =====3==============================
148:     5 2 9 4 7 2
149:     =====4==============================

151:     Above line contains values after clear().
152:     The LinkedList is now empty.
153:     2 2 4 5 7 9
154:     9 7 5 4 2 2
155: */

157: /*
158:     Just a note in passing ... below is another way to create
159:     an LinkedList (of strings), which would require these imports:
160:     import java.util.Collections;
161:     import java.util.stream.Stream;

163:     Stream.of("xyz", "abc").collect(Collectors.toList());
164:     You can ignore this approach for the time being.
165: */