0% found this document useful (0 votes)
29 views

Slides

The document discusses different ways to organize data in Java programs, including strings, arrays, array lists, and hash maps. It then introduces the Java Collections Framework, which provides classes for organizing data into sets, lists, and maps. Sets store unordered, unique elements; lists store ordered sequences; and maps store key-value pairs. The document provides examples using a HashSet to demonstrate basic set operations like adding, checking for existence, and removing elements.

Uploaded by

Lavan Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Slides

The document discusses different ways to organize data in Java programs, including strings, arrays, array lists, and hash maps. It then introduces the Java Collections Framework, which provides classes for organizing data into sets, lists, and maps. Sets store unordered, unique elements; lists store ordered sequences; and maps store key-value pairs. The document provides examples using a HashSet to demonstrate basic set operations like adding, checking for existence, and removing elements.

Uploaded by

Lavan Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 64

Collections

Let's Take a Quick Break


Organizing Data
● We have many ways of storing and
organizing data in our programs:
● Strings for holding sequences of characters.
● ArrayLists for holding sequences of general
objects.
● Arrays for holding fixed-sized sequences.
● HashMaps for associating data with one another.
● Are there other ways of organizing data?
● What do they look like?
The Collections Framework
● Java has a variety of collections classes
for holding groups of data.
The Collections Framework
● Java has a variety of collections classes
for holding groups of data.
● The three major ways of organizing data
are
● Sets, which store unordered data
The Collections Framework
● Java has a variety of collections classes
for holding groups of data.
● The three major ways of organizing data
are
● Sets, which store unordered data,
● Lists, which store sequences
The Collections Framework
● Java has a variety of collections classes
for holding groups of data.
● The three major ways of organizing data
are
● Sets, which store unordered data,
● Lists, which store sequences, and
● Maps, which store key/value pairs.
The Collections Framework
Java has a variety of collections classes
for holding groups of data.
The three major ways of organizing data
are
● Sets, which store unordered data,
Lists, which store sequences, and
Maps, which store key/value pairs.
What is a Set?
● A set is a collection of distinct elements.
● Similar to an ArrayList, but elements are
not stored in a sequence.
● Major operations are:
● Adding an element.
● Removing an element.
● Checking whether an element exists.
● Useful for answering questions of the
form “have I seen this before?”
HashSet<String> mySet = new HashSet<String>();
CS106A

HashSet<String> mySet = new HashSet<String>();


mySet.add("CS106A");

To add a value to a
HashSet, use the syntax
set.add(value)
CS106A

CS106A
Ibex

HashSet<String> mySet = new HashSet<String>();


mySet.add("CS106A");
mySet.add("Ibex");
CS106A

CS106A
Ibex

CS106A
137

HashSet<String> mySet = new HashSet<String>();


mySet.add("CS106A");
mySet.add("Ibex");
mySet.add("137");
CS106A

CS106A
Ibex

CS106A
137

HashSet<String> mySet = new HashSet<String>();


mySet.add("CS106A");
If you add a value
mySet.add("Ibex");
mySet.add("137");
mySet.add("CS106A");
pair where the value
exists, nothing
happens.
CS106A

CS106A
Ibex

CS106A
137

HashSet<String> mySet = new HashSet<String>();


mySet.add("CS106A");
mySet.add("Ibex");
mySet.add("137");
mySet.add("CS106A");

mySet.contains("Ibex");
CS106A

CS106A
Ibex

CS106A
137

HashSet<String> mySet = new HashSet<String>();


mySet.add("CS106A");
mySet.add("Ibex");
mySet.add("137"); To see if a value
mySet.add("CS106A");
exists:
mySet.contains("Ibex");
set.contains(value)
CS106A

CS106A
Ibex

CS106A
137

HashSet<String> mySet = new HashSet<String>();


mySet.add("CS106A");
mySet.add("Ibex");
mySet.add("137");
mySet.add("CS106A");

mySet.contains("Ibex");
mySet.contains("CS106A");
CS106A

CS106A
Ibex

CS106A
137

HashSet<String> mySet = new HashSet<String>();


mySet.add("CS106A");
mySet.add("Ibex");
mySet.add("137");
mySet.add("CS106A")

mySet.contains("Ibex");
mySet.contains("CS106A");
CS106A

CS106A
Ibex

CS106A
137

HashSet<String> mySet = new HashSet<String>();


mySet.add("CS106A");
mySet.add("Ibex");
mySet.add("137");
mySet.add("CS106A")

mySet.contains("Ibex");
mySet.contains("CS106A");
mySet.contains("<(^_^)>");
CS106A

CS106A
Ibex

CS106A
137

HashSet<String> mySet = new HashSet<String>();


mySet.add("CS106A");
mySet.add("Ibex");
mySet.add("137");
mySet.add("CS106A")

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 = /* … */

Iterator<Integer> iter = myList.iterator();


while (iter.hasNext()) {
Integer curr = iter.next();

/* … use curr … */
}
Java Iterators

ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();


while (iter.hasNext()) {
Integer curr = iter.next();

/* … use curr … */
}
Java Iterators

137 42 2718
ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();


while (iter.hasNext()) {
Integer curr = iter.next();

/* … use curr … */
}
Java Iterators

137 42 2718
ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();


while (iter.hasNext()) {
Integer curr = iter.next();

/* … use curr … */
}
Java Iterators
iter

137 42 2718
ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();


while (iter.hasNext()) {
Integer curr = iter.next();

/* … use curr … */
}
Java Iterators
iter

137 42 2718
ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();


while (iter.hasNext()) {
Integer curr = iter.next();

/* … use curr … */
}
Java Iterators
iter hasNext()?

137 42 2718
ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();


while (iter.hasNext()) {
Integer curr = iter.next();

/* … use curr … */
}
Java Iterators
iter

137 42 2718
ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();


while (iter.hasNext()) {
Integer curr = iter.next();

/* … use curr … */
}
Java Iterators
iter
next()!

137 42 2718
ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();


while (iter.hasNext()) {
Integer curr = iter.next();

/* … use curr … */
}
Java Iterators
iter
next()!

137 42 2718
ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();


while (iter.hasNext()) {
Integer curr = iter.next();

/* … use curr … */
}
Java Iterators
iter

137 42 2718
ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();


while (iter.hasNext()) {
Integer curr = iter.next();

/* … use curr … */
}
Java Iterators
iter

137 42 2718
ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();


while (iter.hasNext()) {
Integer curr = iter.next();

/* … use curr … */
}
Java Iterators
hasNext()?
iter

137 42 2718
ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();


while (iter.hasNext()) {
Integer curr = iter.next();

/* … use curr … */
}
Java Iterators
iter

137 42 2718
ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();


while (iter.hasNext()) {
Integer curr = iter.next();

/* … use curr … */
}
Java Iterators
next()!
iter

137 42 2718
ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();


while (iter.hasNext()) {
Integer curr = iter.next();

/* … use curr … */
}
Java Iterators
next()!
iter

137 42 2718
ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();


while (iter.hasNext()) {
Integer curr = iter.next();

/* … use curr … */
}
Java Iterators
iter

137 42 2718
ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();


while (iter.hasNext()) {
Integer curr = iter.next();

/* … use curr … */
}
Java Iterators
iter

137 42 2718
ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();


while (iter.hasNext()) {
Integer curr = iter.next();

/* … use curr … */
}
Java Iterators
hasNext()?
iter

137 42 2718
ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();


while (iter.hasNext()) {
Integer curr = iter.next();

/* … use curr … */
}
Java Iterators
iter

137 42 2718
ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();


while (iter.hasNext()) {
Integer curr = iter.next();

/* … use curr … */
}
Java Iterators
next()!
iter

137 42 2718
ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();


while (iter.hasNext()) {
Integer curr = iter.next();

/* … use curr … */
}
Java Iterators
next()!
iter

137 42 2718
ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();


while (iter.hasNext()) {
Integer curr = iter.next();

/* … use curr … */
}
Java Iterators
iter

137 42 2718
ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();


while (iter.hasNext()) {
Integer curr = iter.next();

/* … use curr … */
}
Java Iterators
iter

137 42 2718
ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();


while (iter.hasNext()) {
Integer curr = iter.next();

/* … use curr … */
}
Java Iterators
hasNext()?
iter

137 42 2718
ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();


while (iter.hasNext()) {
Integer curr = iter.next();

/* … use curr … */
}
Java Iterators
Done!
iter

137 42 2718
ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();


while (iter.hasNext()) {
Integer curr = iter.next();

/* … use curr … */
}
Java Iterators
iter

137 42 2718
ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();


while (iter.hasNext()) {
Integer curr = iter.next();

/* … 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

List Set HashMap

ArrayList HashSet
The Collections Framework

Collection Map

List Set HashMap TreeMap

ArrayList LinkedList HashSet TreeSet


Plus a lot
more!
The Collections Framework

Collection Map

List Set HashMap TreeMap

ArrayList LinkedList HashSet TreeSet


Plus a lot
more!
TreeSet
● TreeSet is similar to HashSet, except
that the values in a TreeSet are stored in
sorted order.
● Iterating over a TreeSet guarantees that
the elements are visited in ascending
order.
● TreeSet is a bit slower than HashSet, so
it's best used only when you really need
things in sorted order.
Levels of Specificity
● To create a map, set, or list, you must
choose a specific implementation (i.e.
ArrayList, HashMap, etc.)
● You can store maps, sets, or lists in
variables of type Map, Set, or List.
● Similar to GObject versus GOval, GRect, etc.
● Lets you say “I just need key/value pairs”
rather than “I need key/value pairs
specifically stored as a HashMap”
TreeMap
● TreeMap is similar to HashMap, except that
the keys in a TreeMap are stored in sorted
order.
● Like TreeSet, iteration over the keys visits
the keys in sorted order.
● The TreeMap has several impressive methods
that don't exist on the normal HashMap.
● There is slight performance cost to using
TreeMap.
Data With Stakes
Just how bad was the
2008 financial meltdown?
An Amazing Website

www.data.gov

You might also like