Slides
Slides
To add a value to a
HashSet, use the syntax
set.add(value)
CS106A
CS106A
Ibex
CS106A
Ibex
CS106A
137
CS106A
Ibex
CS106A
137
CS106A
Ibex
CS106A
137
mySet.contains("Ibex");
CS106A
CS106A
Ibex
CS106A
137
CS106A
Ibex
CS106A
137
mySet.contains("Ibex");
mySet.contains("CS106A");
CS106A
CS106A
Ibex
CS106A
137
mySet.contains("Ibex");
mySet.contains("CS106A");
CS106A
CS106A
Ibex
CS106A
137
mySet.contains("Ibex");
mySet.contains("CS106A");
mySet.contains("<(^_^)>");
CS106A
CS106A
Ibex
CS106A
137
mySet.contains("Ibex");
mySet.contains("CS106A");
mySet.contains("<(^_^)>");
Basic Set Operations
● To insert an element:
set.add(value)
● To check whether a value exists:
set.contains(value)
● To remove an element:
set.remove(value)
Word Walks
CODE
DESIRE
REWRITE
TEMPERATE
TEATIME
MEMENTO
TORRENT
Word Skips
CARROT
TOMATO
OKRA
ASPARAGUS
SQUASH
HORSERADISH
Word Skips
● Begin with any word you'd like.
● Choose a word whose first letter is the
same as the last letter of your current
word.
● Repeat until you get bored.
Iterators
● To visit every element of a collection, you can use the
“for each” loop:
for (ElemType elem: collection) {
…
}
● Alternatively, you can use an iterator, an object whose
job is to walk over the elements of a collection.
● The iterator has two commands:
● hasNext(), which returns whether there are any more
elements to visit, and
● next(), which returns the next element and moves the
iterator to the next position.
Java Iterators
ArrayList<Integer> myList = /* … */
/* … use curr … */
}
Java Iterators
ArrayList<Integer> myList = /* … */
/* … use curr … */
}
Java Iterators
137 42 2718
ArrayList<Integer> myList = /* … */
/* … use curr … */
}
Java Iterators
137 42 2718
ArrayList<Integer> myList = /* … */
/* … use curr … */
}
Java Iterators
iter
137 42 2718
ArrayList<Integer> myList = /* … */
/* … use curr … */
}
Java Iterators
iter
137 42 2718
ArrayList<Integer> myList = /* … */
/* … use curr … */
}
Java Iterators
iter hasNext()?
137 42 2718
ArrayList<Integer> myList = /* … */
/* … use curr … */
}
Java Iterators
iter
137 42 2718
ArrayList<Integer> myList = /* … */
/* … use curr … */
}
Java Iterators
iter
next()!
137 42 2718
ArrayList<Integer> myList = /* … */
/* … use curr … */
}
Java Iterators
iter
next()!
137 42 2718
ArrayList<Integer> myList = /* … */
/* … use curr … */
}
Java Iterators
iter
137 42 2718
ArrayList<Integer> myList = /* … */
/* … use curr … */
}
Java Iterators
iter
137 42 2718
ArrayList<Integer> myList = /* … */
/* … use curr … */
}
Java Iterators
hasNext()?
iter
137 42 2718
ArrayList<Integer> myList = /* … */
/* … use curr … */
}
Java Iterators
iter
137 42 2718
ArrayList<Integer> myList = /* … */
/* … use curr … */
}
Java Iterators
next()!
iter
137 42 2718
ArrayList<Integer> myList = /* … */
/* … use curr … */
}
Java Iterators
next()!
iter
137 42 2718
ArrayList<Integer> myList = /* … */
/* … use curr … */
}
Java Iterators
iter
137 42 2718
ArrayList<Integer> myList = /* … */
/* … use curr … */
}
Java Iterators
iter
137 42 2718
ArrayList<Integer> myList = /* … */
/* … use curr … */
}
Java Iterators
hasNext()?
iter
137 42 2718
ArrayList<Integer> myList = /* … */
/* … use curr … */
}
Java Iterators
iter
137 42 2718
ArrayList<Integer> myList = /* … */
/* … use curr … */
}
Java Iterators
next()!
iter
137 42 2718
ArrayList<Integer> myList = /* … */
/* … use curr … */
}
Java Iterators
next()!
iter
137 42 2718
ArrayList<Integer> myList = /* … */
/* … use curr … */
}
Java Iterators
iter
137 42 2718
ArrayList<Integer> myList = /* … */
/* … use curr … */
}
Java Iterators
iter
137 42 2718
ArrayList<Integer> myList = /* … */
/* … use curr … */
}
Java Iterators
hasNext()?
iter
137 42 2718
ArrayList<Integer> myList = /* … */
/* … use curr … */
}
Java Iterators
Done!
iter
137 42 2718
ArrayList<Integer> myList = /* … */
/* … use curr … */
}
Java Iterators
iter
137 42 2718
ArrayList<Integer> myList = /* … */
/* … use curr … */
}
A Use Case for Iterators
● Because all collections have iterators, a
method can return an iterator to indicate
“here is some data to look at.”
● Internally, that data can be stored in any
format.
● Separates the implementation (how the
class works) from the interface (how the
class is used).
A Word of Warning
● The following will loop forever on a nonempty collection:
while (collection.iterator().hasNext()) {
/* … */
}
● Every time that you call .iterator(), you get back a
new iterator to the start of the collection.
Ibex Kitty
A Word of Warning
● The following will loop forever on a nonempty collection:
while (collection.iterator().hasNext()) {
/* … */
}
● Every time that you call .iterator(), you get back a
new iterator to the start of the collection.
Ibex Kitty
The Collections Framework
Collection Map
ArrayList HashSet
The Collections Framework
Collection Map
Collection Map
www.data.gov