Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
School of Informatics, University of Edinburgh Computer Science 1 Ah CS1Ah Lecture Note 22 Lists, sets and iterators In this lecture note we discuss the linked-list and set data structures provided in Java’s java.util package. 22.1 Lists and arrays Lists differ from the arrays which we have previously seen because arrays provide efficient random access to any part of the array (accessing the thousandth element in an array is as efficient as accessing the first) whereas lists are designed for serial access (accessing the first element is the most efficient operation, whereas to access the thousandth element in a list we must access all of the nine hundred and ninetynine elements which precede it). This fact has a profound impact on the algorithms which we design for processing lists. For example, if we wanted to sum the integers in a list we would access the integers in the order from the first through to the last. We would obtain the same answer if we added them in random order but the program would be much less efficient. If we wish to add a new element at the start of a list then the time taken to perform this operation is independent of the length of the list. However, this is not true if we use arrays: in order to add a new element at the start of an array of a thousand elements we must shuffle a thousand entries along one place in order to provide a spare slot at the start for the new element. 22.2 Linked lists in the Java library The java.util package provides an implementation of doubly linked lists in its LinkedList class. This class provides implementations of the following methods (and others: see the Java documentation for full details). addFirst(), addLast() These methods allow us to add an object at the beginning or the end of the list. getFirst(), getLast() These methods allow us to inspect the object at the beginning or the end of the list. 1 School of Informatics, University of Edinburgh Computer Science 1 Ah removeFirst(), removeLast() These methods allow us to delete an object at the beginning or the end of the list. There are add(), get() and remove() methods which are parameterised by an integer allowing us to specify other positions in the list which are neither the first nor the last. There is a set() method which allows us to set the contents of the cell at a particular position. As with arrays, numbering is from zero. 22.3 List Iterators The Java LinkedList has a method iterator() for creating an iterator object on which hasNext() and next() methods can be invoked, just as with the iterators from Lecture note 21 on linked lists. The LinkedList class also has a method listIterator() which generates iterators supporting additional methods. For example: hasPrevious(), previous() since a doubly-linked list can as easily be navigated backward as forward. add(), remove(), set() which all modify the list in the region of the list iterator. Calling add() insert at the iterator and leaves the iterator just beyond the newly inserted element. Calling remove() removes the element most recently passed over with a next() or previous() call and leaves the iterator at the closed gap left by the removed element. Calling set() updates the value of the most recently crossed element. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 import java.util.*; class ListsExample { public static void main (String[] args) { LinkedList l = new LinkedList(); l.addLast(new Integer(3)); l.addLast(new Integer(5)); System.out.println(l); // prints [3, 5] ListIterator i = l.listIterator(); System.out.println(i.next()); // prints 3 i.add(new Integer(7)); System.out.println(l); // prints [3, 7, 5] System.out.println(i.next()); // prints 5 i.set(new Integer(11)); // changes 5, since last crossed System.out.println(l); // prints [3, 7, 11] System.out.println(i.previous()); // prints 11 i.remove(); // removes 11, since last crossed System.out.println(l); // prints [3, 7] } } 2 School of Informatics, University of Edinburgh 22.4 Computer Science 1 Ah Sets and arrays Lists allow us to assemble collections of objects which are stored in some order. When we add new objects to a list we must specify whether we want them to be added in first position or in last position. For some applications all we need is a collection of objects so that we may ask if the collection contains a particular element or not. A collection where order is not important is called a set. Like arrays, sets provide efficient access to every element. Sets differ from arrays in that access is provided by content, not by position. The type of questions which we pose with a set are whether or not an element is included. With lists and arrays we could ask where an element appears or how many times an element appears but these questions are not meaningful for sets. Set elements are either in or they are out. 22.5 Implementations of sets The java.util package provides two implementations of sets, HashSet and TreeSet. These provide implementations of the following methods (and others: see the Java documentation for full details). add(), addAll() These methods allow us to add one or several objects to the set. contains(), containsAll() These methods allow us to test whether one or several objects are in the set. remove(), removeAll() These methods allow us to delete one or several objects. The following program shows some of these methods in use with a TreeSet. A tree set maintains the elements of the set in sorted order because this makes adding and removing elements and testing for membership more efficient. A HashSet maintains the elements in a different order. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 import java.util.*; class SetExample { public static void main (String[] args) { TreeSet t = new TreeSet(); t.add("def"); t.add("abc"); t.add("ghi"); // next statement prints [abc, def, ghi] System.out.println(t); // next statement prints "true" System.out.println(t.contains("def")); // remove "def" from the set t t.remove("def"); // next statement prints "false" System.out.println(t.contains("def")); } } 3 School of Informatics, University of Edinburgh Computer Science 1 Ah The implementations of tree sets and hash sets depend on efficient data structures called (respectively) Red-Black trees and hash tables. Red-Black trees arrange the elements of the set by content (for example, strings are stored in lexicographic order). Hash tables map each object onto a numeric code (a hash number) and store the elements in hash number order. Hash tables are more efficient than Red-Black trees so they are to be preferred for modelling large sets. Precise analysis of the relative efficiency of data structures such as these appears in later years of this degree course. 22.6 Operations on sets In some applications we make new collections of objects by combining existing collections. There are three main operations for combining sets: Set union: The union of s and t contains all of the elements in either s or t (or both). Java provides the method addAll() for this. Set intersection: The intersection of s and t contains all of the elements which are in both s and t. Java provides the method retainAll() for this. Set difference: The difference of s and t contains all of the elements which are in s but not in t. Java provides the method removeAll() for this. The program below illustrates these methods in use combining two sets, s and t. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 import java.util.*; class SetOperations { public static void main (String[] args) { TreeSet s = new TreeSet(); s.add("mno"); s.add("jkl"); s.add("ghi"); TreeSet t = new TreeSet(); t.add("def"); t.add("abc"); t.add("ghi"); // Add all of the elements of t to s (set union) s.addAll(t); // next statement prints [abc, def, ghi, jkl, mno] System.out.println(s); // Remove all elements of t from s (set difference) s.removeAll(t); // next statement prints [jkl, mno] System.out.println(s); } } Authored by Stephen Gilmore and Paul Jackson. Paul Jackson, November 26th, 2002. 4