CS 46B - Lecture 13

Cover page image

Pre-class reading

Collections

Collections

. .

Collections

Maps

.

Common Methods

Collection<String> coll = new ArrayList<>(); The ArrayList class implements the Collection
interface.

coll = new TreeSet<String>();

The TreeSet class also implements the Collection interface.

int n = coll.size();

Gets the size of the collection. n is now 0.

coll.add("Harry");
coll.add("Sally");

Adds elements to the collection.

String s = coll.toString();

Returns a string with all elements in the collection. s is now "[Harry, Sally]"

System.out.println(coll);

Invokes the toString method and prints [Harry, Sally].

coll.remove("Harry");
boolean b = coll.remove("Tom");

Checks whether this collection contains a given element. b is now true.

for (String s : coll)
   System.out.println(s);
You can use the “for each” loop with any collection. This loop prints the elements on separate lines.

Iterator<> iter = coll.iterator();

You use an iterator for visiting the elements in
the collection

Lecture 13 Clicker Question 1

You are taking a social science class and you are assigned lots of books and articles as required reading. Which data structure is best for organizing them?

  1. A set
  2. A queue
  3. A stack
  4. A map

Linked Lists

Linked Lists

Working With Linked Lists

LinkedList<String> list = new LinkedList<>(); An empty list.
list.addLast("Harry"); Adds an element to the end of the list. Same as add.
list.addFirst("Sally"); Adds an element to the beginning of the list. list is now [Sally, Harry].
list.getFirst(); Gets the element stored at the beginning of the list; here "Sally".
list.getLast(); Gets the element stored at the end of the list; here "Harry".
String removed = list.removeFirst(); Removes the first element of the list and returns it. removed is "Sally" and list is [Harry]. Use removeLast to remove the last element.
ListIterator<> iter=list.listIterator() Provides an iterator for visiting all list elements

List Iterators

List Iterators

String s = iter.next(); Assume that iter points to the beginning of the list [Sally] before calling next. After the call, s is "Sally" and the iterator points to the end.
iter.previous(); iter.set("Juliet"); The set method updates the last element returned by next or previous. The list is now [Juliet].
iter.hasNext() Returns false because the iterator is at the end of the collection.
if (iter.hasPrevious()) { s = iter.previous(); } hasPrevious returns true because the iterator is not at the beginning of the list. previous and hasPrevious are ListIterator methods.
iter.add("Diana"); Adds an element before the iterator position (ListIterator only). The list is now [Diana, Juliet].
iter.next(); iter.remove(); remove removes the last element returned by next or previous. The list is now [Diana].
while (iterator.hasNext())
{
   String name = iterator.next();
   if (condition is fulfilled for name)
   {
      iterator.remove();
   }
}
this loop removes all names that fulfill a certain condition:

Lecture 13 Clicker Question 2

What is the contents of letters after this sequence of instructions?

List<String> letters = new LinkedList<>();
letters.add("F");
letters.add("R");
letters.add("E");
letters.add("D");
ListIterator<String> iter = letters.iterator();
iter.next();
iter.next();
iter.remove();
iter.next();
iter.add("X");
  1. [F, R, E, X]
  2. [F, E, X, D]
  3. [F, E, R, X]
  4. Something else