Javas Collection Framework 1 Javas Collection Framework w

  • Slides: 25
Download presentation
Java's Collection Framework 1

Java's Collection Framework 1

Java's Collection Framework w Collection framework — w Java's collection framework contains — —

Java's Collection Framework w Collection framework — w Java's collection framework contains — — — w Unified architecture for representing and manipulating collections Interfaces (ADTs): specification not implementation Concrete implementations as classes Polymorphic Algorithms to search, sort, find, shuffle, . . . The algorithms are polymorphic: — the same method can be used on many different implementations of the appropriate collection interface. In essence, algorithms are reusable functionality. 2

Collection interfaces in java. util Image from the Java Tutorial 3

Collection interfaces in java. util Image from the Java Tutorial 3

Abstract Data Type w Abstract data type (ADT) is a specification of the behaviour

Abstract Data Type w Abstract data type (ADT) is a specification of the behaviour (methods) of a type Specifies method names to add, remove, find — Specifies if elements are unique, indexed, accessible from only one location, mapped, . . . — An ADT shows no implementation — • no structure to store elements, no implementations w Which Java construct nicely specifies ADTs? 4

Collection Classes w A collection class the can be instantiated implements an interface as

Collection Classes w A collection class the can be instantiated implements an interface as a Java class — implements all methods of the interface — selects appropriate instance variables — w Java has many collection classes including: — — — Array. List<E> Linked. List<E> Linked. Blocking. Queue<E> Array. Blocking. Queue<E> Stack<E> Hash. Set<E> Tree. Set<E> Hash. Map<K, V> Tree. Map<K, V> 5

Common Functionality w Collection classes often have methods for Adding objects — Removing an

Common Functionality w Collection classes often have methods for Adding objects — Removing an object — Finding a reference to a particular object — • We can then send messages to the object still referenced by the collection 6

The Collection Interface w interface Collection abstractly describes a group of elements as a

The Collection Interface w interface Collection abstractly describes a group of elements as a bag or multi-set w Some classes implementing Collection allow duplicate elements; others do not — keeps elements ordered; others do not w Collection allows different types of — objects to be passed around — Allows generality, many concrete classes can be treated as a Collection 7

Collection method signatures w The methods of interface Collection http: //java. sun. com/javase/7/docs/api/java/util/Collection. html

Collection method signatures w The methods of interface Collection http: //java. sun. com/javase/7/docs/api/java/util/Collection. html boolean add(E e) boolean add. All(Collection<? extends E> c) void clear() boolean contains(Object o) boolean contains. All(Collection<? > c) boolean equals(Object o) int hash. Code() boolean is. Empty() Iterator<E> iterator() boolean remove(Object o) boolean remove. All(Collection<? > c) boolean retain. All(Collection<? > c) int size() Object[] to. Array() 8

One Interface Note: List extends Collection w List: a collection where indexes matter —

One Interface Note: List extends Collection w List: a collection where indexes matter — — extends Collection so it has all of the methods of Collection Can decide where in the List elements are inserted 9

List<E>, an Abstract Data Type (ADT) written as a Java interface w List<E>: a

List<E>, an Abstract Data Type (ADT) written as a Java interface w List<E>: a collection with a first element, a last element, distinct predecessors and successors — — The user of this interface has precise control over where in the list each element is inserted duplicates that "equals" each other are allowed 10

List<E> w Users of the interface List<E> — — — have precise control over

List<E> w Users of the interface List<E> — — — have precise control over where in the list each element is inserted. allows programmers to access elements by their integer index (position in the list) search for elements in the list Can have duplicate elements (equals) Methods include: add, add. All, index. Of, is. Empty, remove, to. Array w Some implementing classes in java. util: — Array. List<E>, Linked. List<E>, Stack<E>, Vector<E> 11

import java. util. *; // For List, Array. List, Linked. . . import static

import java. util. *; // For List, Array. List, Linked. . . import static org. junit. Assert. *; import org. junit. Test; public class Three. Classes. Implement. List { @Test public void show. Three. Implementations. Of. List() { // Interface name: List // Three classes that implement the List interface: List<String> a. List = new Array. List<String>(); List<String> el. List = new Linked. List<String>(); List<String> shared. List = new Vector<String>(); // All three have an add method a. List. add("in array list"); el. List. add("in linked list"); shared. List. add("in vector"); // All three have a get method assert. Equals("in array list", a. List. get(0)); assert. Equals("in linked list", el. List. get(0)); assert. Equals("in vector", shared. List. get(0)); } } 12

Polymorphism via interfaces w The previous slide shows three different types treated as type

Polymorphism via interfaces w The previous slide shows three different types treated as type List, all three — implement interface List — understand the same messages may have different methods with the same names execute — w This is an example of polymorphism via interfaces — All implementing classes have add, get, iterator 13

Algorithms w Java has polymorphic algorithms to provide functionality for different types of collections

Algorithms w Java has polymorphic algorithms to provide functionality for different types of collections — — — Sorting (e. g. sort) Shuffling (e. g. shuffle) Routine Data Manipulation (e. g. reverse, add. All) Searching (e. g. binary. Search) Finding Extreme Values (e. g. max) w Static methods in the Collections class w Demo a few of these with Array. List … 14

Add max, min, binary. Search, Switch to Linked. List<String> names = new Array. List<String>();

Add max, min, binary. Search, Switch to Linked. List<String> names = new Array. List<String>(); names. add("Kim"); names. add("Chris"); names. add("Dakota"); names. add("Jamie"); System. out. println(names); Collections. sort(names); System. out. println(names); [Kim, Chris, Dakota, Jamie] Collections. reverse(names); [Chris, Dakota, Jamie, Kim] System. out. println(names); [Kim, Jamie, Dakota, Chris] [Jamie, Chris, Dakota, Kim] Collections. shuffle(names); System. out. println(names); 15

Stack<E> void push(E e) Adds e to the top of the stack boolean is.

Stack<E> void push(E e) Adds e to the top of the stack boolean is. Empty() Return true if the has no elements E peek() Retrieves, but does not remove, the element at the top of this queue E pop() Retrieves and removes the element at the top of this stack 16

Queue<E> boolean add(E e) Inserts e into this queue E element() Retrieves, but does

Queue<E> boolean add(E e) Inserts e into this queue E element() Retrieves, but does not remove, the head of this queue boolean offer(E e)Inserts e into this queue E peek() Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty E poll() Retrieves and removes the head of this queue, or returns null if this queue is empty E remove() Retrieves and removes the head of this queue 17

Array. Blocking. Queue<E> a FIFO queue Array. Blocking. Queue<Double> number. Q = new Array.

Array. Blocking. Queue<E> a FIFO queue Array. Blocking. Queue<Double> number. Q = new Array. Blocking. Queue<Double>(40); number. Q. add(3. 3); number. Q. add(2. 2); number. Q. add(5. 5); number. Q. add(4. 4); number. Q. add(7. 7); assert. Equals(3. 3, number. Q. peek(), 0. 1); assert. Equals(3. 3, number. Q. remove(), 0. 1); assert. Equals(2. 2, number. Q. remove(), 0. 1); assert. Equals(5. 5, number. Q. peek(), 0. 1); assert. Equals(3, number. Q. size()); 18

Tree. Set implements Set w Set<E> An interface for collections with no duplicates. More

Tree. Set implements Set w Set<E> An interface for collections with no duplicates. More formally, sets contain no pair of elements e 1 and e 2 such that e 1. equals(e 2) w Tree. Set<E>: This class implements the Set interface, backed by a balanced binary search tree. This class guarantees that the sorted set will be in ascending element order, sorted according to the natural order of the elements as defined by Comparable<T> 19

Set and Sorted. Set w The Set<E> interface methods: — add, add. All, remove,

Set and Sorted. Set w The Set<E> interface methods: — add, add. All, remove, size, but no get! w Two classes that implement Set<E> Tree. Set: values stored in order, O(log n) — Hash. Set: values in a hash table, no order, O(1) — w Sorted. Set extends Set by adding methods E first() E last() Sorted. Set<E> tail. Set(E from. Element), Sorted. Set<E> head. Set(E from. Element) Sorted. Set<E> sub. Set(E from. Element, E to. Element) 20

Tree. Set elements are in order Sorted. Set<Integer> set = new Tree. Set<Integer>(); set.

Tree. Set elements are in order Sorted. Set<Integer> set = new Tree. Set<Integer>(); set. add(7); set. add(2); set. add(7); set. add(8); set. add(9); System. out. println(set. tail. Set(8)); System. out. println(set. head. Set(8)); System. out. println(set. sub. Set(1, 9)); [2, 7, 8, 9] [2, 7] [2, 7, 8] 21

Tree. Set elements are in order Set<String> names = new Tree. Set<String>(); names. add("Sandeep");

Tree. Set elements are in order Set<String> names = new Tree. Set<String>(); names. add("Sandeep"); names. add("Chris"); names. add("Kim"); names. add("Chris"); // not added names. add("Devon"); for (String name : names) System. out. println(name); w Output? w Change to Hash. Set 22

The Map Interface (ADT) w Map describes a type that stores a collection of

The Map Interface (ADT) w Map describes a type that stores a collection of elements that consists of a key and a value w A Map associates (maps) a key the it's value w The keys must be unique the values need not be unique — put destroys one with same key — w Concrete classes in java. util — Hash. Map<K, V> and Tree. Map<K, V> 23

Map Operations w java. util. Hash. Map<K, V> — — — public V put(K

Map Operations w java. util. Hash. Map<K, V> — — — public V put(K key, V value) • associates key to value and stores mapping public V get(K key) • associates the value to which key is mapped or null public boolean contains. Key(K key) • returns true if the Map already uses the key public V remove(K key) public Collection<V> values() • get a collection you can iterate over public Collection<V> key. Set() • get a collection you can iterate over 24

Using a Map Tree. Map<K, V> is like our Ordered. Map<K, V> Map<String, Bank.

Using a Map Tree. Map<K, V> is like our Ordered. Map<K, V> Map<String, Bank. Account> a. Map = new Tree. Map<String, Bank. Account>(); Bank. Account acct 1 = new Bank. Account("Jessica", 500. 00); Bank. Account acct 2 = new Bank. Account("Alex", 400. 00); Bank. Account acct 3 = new Bank. Account("Anthony", 300. 00); Bank. Account acct 4 = new Bank. Account("Danny", 200. 00); Bank. Account acct 5 = new Bank. Account("Patrick", 100. 00); a. Map. put(acct 1. get. ID(), acct 1); a. Map. put(acct 2. get. ID(), acct 2); a. Map. put(acct 3. get. ID(), acct 3); a. Map. put(acct 4. get. ID(), acct 4); a. Map. put(acct 5. get. ID(), acct 5); // Check out the keys and the value in a. Map Collection<String> keys = a. Map. key. Set(); System. out. println(keys); Collection<Bank. Account> values = a. Map. values(); System. out. println(values); 25