2016-08-09 10:21:47 -06:00
|
|
|
// understandingcollections/ListOps.java
|
2015-12-15 11:47:04 -08:00
|
|
|
// (c)2016 MindView LLC: see Copyright.txt
|
2015-11-15 15:51:35 -08:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2016-01-25 18:05:55 -08:00
|
|
|
// Things you can do with Lists
|
2015-06-15 17:47:35 -07:00
|
|
|
import java.util.*;
|
2016-11-01 11:43:22 -07:00
|
|
|
import onjava.HTMLColors;
|
2015-06-15 17:47:35 -07:00
|
|
|
|
2016-08-09 10:21:47 -06:00
|
|
|
public class ListOps {
|
2016-11-01 11:43:22 -07:00
|
|
|
// Create a short list for testing:
|
|
|
|
static final List<String> LIST =
|
|
|
|
HTMLColors.LIST.subList(0, 10);
|
2015-06-15 17:47:35 -07:00
|
|
|
private static boolean b;
|
|
|
|
private static String s;
|
|
|
|
private static int i;
|
|
|
|
private static Iterator<String> it;
|
|
|
|
private static ListIterator<String> lit;
|
|
|
|
public static void basicTest(List<String> a) {
|
|
|
|
a.add(1, "x"); // Add at location 1
|
|
|
|
a.add("x"); // Add at end
|
|
|
|
// Add a collection:
|
2016-11-01 11:43:22 -07:00
|
|
|
a.addAll(LIST);
|
2015-06-15 17:47:35 -07:00
|
|
|
// Add a collection starting at location 3:
|
2016-11-01 11:43:22 -07:00
|
|
|
a.addAll(3, LIST);
|
2015-06-15 17:47:35 -07:00
|
|
|
b = a.contains("1"); // Is it in there?
|
|
|
|
// Is the entire collection in there?
|
2016-11-01 11:43:22 -07:00
|
|
|
b = a.containsAll(LIST);
|
2015-06-15 17:47:35 -07:00
|
|
|
// Lists allow random access, which is cheap
|
|
|
|
// for ArrayList, expensive for LinkedList:
|
|
|
|
s = a.get(1); // Get (typed) object at location 1
|
|
|
|
i = a.indexOf("1"); // Tell index of object
|
|
|
|
b = a.isEmpty(); // Any elements inside?
|
|
|
|
it = a.iterator(); // Ordinary Iterator
|
|
|
|
lit = a.listIterator(); // ListIterator
|
|
|
|
lit = a.listIterator(3); // Start at loc 3
|
|
|
|
i = a.lastIndexOf("1"); // Last match
|
|
|
|
a.remove(1); // Remove location 1
|
|
|
|
a.remove("3"); // Remove this object
|
|
|
|
a.set(1, "y"); // Set location 1 to "y"
|
|
|
|
// Keep everything that's in the argument
|
|
|
|
// (the intersection of the two sets):
|
2016-11-01 11:43:22 -07:00
|
|
|
a.retainAll(LIST);
|
2015-06-15 17:47:35 -07:00
|
|
|
// Remove everything that's in the argument:
|
2016-11-01 11:43:22 -07:00
|
|
|
a.removeAll(LIST);
|
2015-06-15 17:47:35 -07:00
|
|
|
i = a.size(); // How big is it?
|
|
|
|
a.clear(); // Remove all elements
|
|
|
|
}
|
|
|
|
public static void iterMotion(List<String> a) {
|
|
|
|
ListIterator<String> it = a.listIterator();
|
|
|
|
b = it.hasNext();
|
|
|
|
b = it.hasPrevious();
|
|
|
|
s = it.next();
|
|
|
|
i = it.nextIndex();
|
|
|
|
s = it.previous();
|
|
|
|
i = it.previousIndex();
|
|
|
|
}
|
|
|
|
public static void iterManipulation(List<String> a) {
|
|
|
|
ListIterator<String> it = a.listIterator();
|
|
|
|
it.add("47");
|
|
|
|
// Must move to an element after add():
|
|
|
|
it.next();
|
|
|
|
// Remove the element after the newly produced one:
|
|
|
|
it.remove();
|
|
|
|
// Must move to an element after remove():
|
|
|
|
it.next();
|
|
|
|
// Change the element after the deleted one:
|
|
|
|
it.set("47");
|
|
|
|
}
|
|
|
|
public static void testVisual(List<String> a) {
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println(a);
|
2016-11-01 11:43:22 -07:00
|
|
|
List<String> b = LIST;
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println("b = " + b);
|
2015-06-15 17:47:35 -07:00
|
|
|
a.addAll(b);
|
|
|
|
a.addAll(b);
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println(a);
|
2015-06-15 17:47:35 -07:00
|
|
|
// Insert, remove, and replace elements
|
|
|
|
// using a ListIterator:
|
|
|
|
ListIterator<String> x = a.listIterator(a.size()/2);
|
|
|
|
x.add("one");
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println(a);
|
|
|
|
System.out.println(x.next());
|
2015-06-15 17:47:35 -07:00
|
|
|
x.remove();
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println(x.next());
|
2015-06-15 17:47:35 -07:00
|
|
|
x.set("47");
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println(a);
|
2015-06-15 17:47:35 -07:00
|
|
|
// Traverse the list backwards:
|
|
|
|
x = a.listIterator(a.size());
|
|
|
|
while(x.hasPrevious())
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.print(x.previous() + " ");
|
|
|
|
System.out.println();
|
|
|
|
System.out.println("testVisual finished");
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
// There are some things that only LinkedLists can do:
|
|
|
|
public static void testLinkedList() {
|
|
|
|
LinkedList<String> ll = new LinkedList<>();
|
2016-11-01 11:43:22 -07:00
|
|
|
ll.addAll(LIST);
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println(ll);
|
2015-06-15 17:47:35 -07:00
|
|
|
// Treat it like a stack, pushing:
|
|
|
|
ll.addFirst("one");
|
|
|
|
ll.addFirst("two");
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println(ll);
|
2015-06-15 17:47:35 -07:00
|
|
|
// Like "peeking" at the top of a stack:
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println(ll.getFirst());
|
2015-06-15 17:47:35 -07:00
|
|
|
// Like popping a stack:
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println(ll.removeFirst());
|
|
|
|
System.out.println(ll.removeFirst());
|
2015-06-15 17:47:35 -07:00
|
|
|
// Treat it like a queue, pulling elements
|
|
|
|
// off the tail end:
|
2015-11-03 12:00:44 -08:00
|
|
|
System.out.println(ll.removeLast());
|
|
|
|
System.out.println(ll);
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
|
|
// Make and fill a new list each time:
|
2016-11-01 11:43:22 -07:00
|
|
|
basicTest(new LinkedList<>(LIST));
|
|
|
|
basicTest(new ArrayList<>(LIST));
|
|
|
|
iterMotion(new LinkedList<>(LIST));
|
|
|
|
iterMotion(new ArrayList<>(LIST));
|
|
|
|
iterManipulation(new LinkedList<>(LIST));
|
|
|
|
iterManipulation(new ArrayList<>(LIST));
|
|
|
|
testVisual(new LinkedList<>(LIST));
|
2015-06-15 17:47:35 -07:00
|
|
|
testLinkedList();
|
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
}
|
2016-11-01 11:43:22 -07:00
|
|
|
/* Output:
|
2015-09-07 11:44:36 -06:00
|
|
|
*/
|