09a: Java Collections

ICS211, Spring 2013
Dr. Zach

(switch view)

Status check

Data structure / ADT libaries

Java Collections

Collection interface (ADT)

List interface (ADT)

subList Examples

From Tutorial:

List<?> list = ...
list.subList(fromIndex, toIndex).clear();

int i = list.subList(fromIndex, toIndex).indexOf(o);
int j = list.subList(fromIndex, toIndex).lastIndexOf(o);
public static <E> List<E> dealHand(List<E> deck, int n) {
    int deckSize = deck.size();
    List<E> handView = deck.subList(deckSize - n, deckSize);
    List<E> hand = new ArrayList<E>(handView);
    handView.clear();
    return hand;
}

Queue interface (ADT)

Deque interface (ADT)

Other ADTs

Also covered in Tutorial:

More stuff in java.util.concurrent too, but generally not necessary since we're not using threads

Implementations

Implementations

Others:

Using Implementations

Old Implementations

Generally don't use these. synchronized, so slight overhead to them (not as efficient)

Collections class

java.util.Arrays

Implementation Help

Other useful classes: Comparator

The inefficiency of Strings

String str = "";
for (int i = 0; i < 5; i++) {
  str += i + " ";
}
System.out.println(str); //prints: 0 1 2 3 4 

StringBuilder

StringBuilder str = new StringBuilder();
for (int i = 0; i < 5; i++) {
  str.append(i);
  str.append(" ");
}
System.out.println(str.toString()); //prints: 0 1 2 3 4

For next time