lecture 4 the java collections framework. java container classes

24
Lecture 4 The Java Collections Framework

Upload: isabella-ella-bridges

Post on 27-Dec-2015

261 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Lecture 4 The Java Collections Framework. Java Container Classes

Lecture 4The Java Collections Framework

Page 2: Lecture 4 The Java Collections Framework. Java Container Classes

Java Container Classes

Page 3: Lecture 4 The Java Collections Framework. Java Container Classes

The Collection Interface

Page 4: Lecture 4 The Java Collections Framework. Java Container Classes

LinkedList

Page 5: Lecture 4 The Java Collections Framework. Java Container Classes

data next

the Node

Page 6: Lecture 4 The Java Collections Framework. Java Container Classes

4 6 9 12 15 20 null

head curNode

11

newNode

Insertion Sort

Page 7: Lecture 4 The Java Collections Framework. Java Container Classes

4 6 9 12 15 20 null

head curNode

11

newNode

Insertion Sort

Page 8: Lecture 4 The Java Collections Framework. Java Container Classes

4 6 9 12 15 20 null

head curNode

11

newNode

Insertion Sort

Page 9: Lecture 4 The Java Collections Framework. Java Container Classes

4 6 9 12 15 20 null

head curNode

11

newNode

Insertion Sort

Page 10: Lecture 4 The Java Collections Framework. Java Container Classes

4 6 9 12 15 20 null

head curNode

11

newNode

Insertion Sort

Page 11: Lecture 4 The Java Collections Framework. Java Container Classes

4 6 9 12 15 20 null

head curNode

11

newNode

Insertion Sort

Page 12: Lecture 4 The Java Collections Framework. Java Container Classes

the interator

Page 13: Lecture 4 The Java Collections Framework. Java Container Classes

Set

Page 14: Lecture 4 The Java Collections Framework. Java Container Classes

HashSet

The HashSet class is a concrete class that implements Set. You can create an empty hash set using its no-arg constructor or create a hash set from an existing collection. By default, the initial capacity is 16 and load factor is 0.75. If you know the size of your set, you may specify the initial capacity and load factor in the constructor. Otherwise, use the default setting. Load factor is a value between 0.0 and 1.0.

The Load factor measures how full the set is allowed to be before its capacity is increased. When the number of elements exceeds the product of the capacity and load factor, the capacity is automatically doubled. For example, if the capacity is 16 and load factor is 0.75, the capacity will be doubled to 32 when the size reaches 12 (16 x 0.75 = 12).

Page 15: Lecture 4 The Java Collections Framework. Java Container Classes

LinkedHashSet extends HashSet with a linked-list implementation that supports an ordering of the elements in the set.

The elements in a HashSet are not ordered, but the elements in a LinkedHashSet can be retrieved in the order in which they were inserted into the set.

A LinkedHashSet can be created by using one of its four constructors. These constructors are similar to the constructors for HashSet.

LinkedHashSet

Page 16: Lecture 4 The Java Collections Framework. Java Container Classes

import java.util.*;public class TestLinkedHashSet { public static void main(String[] args) { // Create a hash set Set<String> set = new LinkedHashSet<String>(); // Add strings to the set set.add("London"); set.add("Paris"); set.add("New York"); set.add("San Francisco"); set.add("Beijing"); set.add("New York"); System.out.println(set); // Display the elements in the hash set for (Object element: set) System.out.print(element.toString().toLowerCase() + " "); }}

[London, Paris, New York, San Francisco, Beijing]london paris new york san francisco beijing

TestLinkedHashSet

Page 17: Lecture 4 The Java Collections Framework. Java Container Classes

Map

Page 18: Lecture 4 The Java Collections Framework. Java Container Classes

A Mapping of Keys to Values

Page 19: Lecture 4 The Java Collections Framework. Java Container Classes

Stack

Page 20: Lecture 4 The Java Collections Framework. Java Container Classes

Queue

Page 21: Lecture 4 The Java Collections Framework. Java Container Classes

public class TestQueue{ public static void main(String[] args) { java.util.Queue<String> queue = new java.util.LinkedList<String>(); queue.offer("Oklahoma"); queue.offer("Indiana"); queue.offer("Georgia"); queue.offer("Texas"); while (queue.size() > 0) System.out.print(queue.remove() + " "); }}

TestQueue

Oklahoma Indiana Georgia Texas

Page 22: Lecture 4 The Java Collections Framework. Java Container Classes

PriorityQueue

Page 23: Lecture 4 The Java Collections Framework. Java Container Classes

Backtracking

Page 24: Lecture 4 The Java Collections Framework. Java Container Classes

Shortest Route