Interfaces in Javas Collection Framework Rick Mercer 1

  • Slides: 21
Download presentation
Interfaces in Java’s Collection Framework Rick Mercer 1

Interfaces in Java’s Collection Framework Rick Mercer 1

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

Java's Collection Framework w Java's Collection Framework — w 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, . . . 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

Core Collection interfaces in java. util Image from the Java Tutorial w Set, List,

Core Collection interfaces in java. util Image from the Java Tutorial w Set, List, and Queue extend the Collection interface — If you implement Set, in addition to all of Set’s methods, all Collection methods must also be implemented w BTW: Collection<E> extends Iterable<E> 3

interface Collection Bags (multi-sets) should implement Collection directly boolean add(E e) boolean add. All(Collection<?

interface Collection Bags (multi-sets) should implement Collection directly 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() <T> T[] to. Array(T[] a) 4

List<E> an ADT written as a Java interface w List<E>: a collection with a

List<E> an 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 w The List interface is implemented by these three collection classes — — — Array. List<E> Linked. List<E> Vector<E> 5

Can treat all three (and more) as a List // Interface name: List //

Can treat all three (and more) as a List // Interface name: List // Three classes that implement the List interface: List<String> big. List = new Array. List<String>(); List<String> little. List = new Linked. List<String>(); List<String> shared. List = new Vector<String>(); // All three have an add method big. List. add("in array list"); little. List. add("in linked list"); shared. List. add("in vector"); // All three have a get method assert. Equals("in array list", big. List. get(0)); assert. Equals("in linked list", little. List. get(0)); assert. Equals("in vector", shared. List. get(0)); 6

interface Iterator w Iterators provide a general way to traverse all elements in a

interface Iterator w Iterators provide a general way to traverse all elements in a collection Array. List<String> list = new Array. List<String>(); list. add("1 -Fi. Rs. T"); list. add("2 -Se. Co. ND"); list. add("3 -Th. Ir. D"); Iterator<String> itr = list. iterator(); while (itr. has. Next()) { System. out. println(itr. next(). to. Lower. Case()); } Output 1 -first 2 -second 3 -third 7

The actual interface public interface Iterator<E> { // Returns true if the iteration has

The actual interface public interface Iterator<E> { // Returns true if the iteration has more elements. boolean has. Next(); //Returns the next element in the iteration. E next(); // Removes from the underlying collection the last element // returned by the iterator (optional operation). void remove(); } 8

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 w Methods use interfaces to Sort any List — — — Sorting (e. g. sort) Shuffling (e. g. shuffle) Routine Data Manipulation (e. g. reverse, add. All) Searching (e. g. binary. Search) Composition (e. g. frequency) Finding Extreme Values (e. g. max) 9

Before looking at Collections. java. some Additional Generics w To specify additional interfaces that

Before looking at Collections. java. some Additional Generics w To specify additional interfaces that must be implemented, use the & character, as in: <U extends Number & My. Interface> w In generics, an unknown type is represented by the wildcard character “? ” contains. All(Collection<? > c) w The receiver must be a superclass (or the same type) of the type of elements being added http: //docs. oracle. com/javase/6/docs/api/java/util/Collections. html 10

Can add <String> to <Objects> or to <String> w final class String extends Object

Can add <String> to <Objects> or to <String> w final class String extends Object List<String> a = new Array. List<String>(); a. add("Z"); a. add("A"); List<Object> b = new Array. List<Object>(); b. add("Y"); b. add("B"); b. add. All(a); System. out. println(b); // a. add. All(a) is okay, // but this is an error: a. add. All(b); 11

class Collections has many static methods public static <T extends. Object & Comparable<? super

class Collections has many static methods public static <T extends. Object & Comparable<? super T>> T min(Collection<? extends T> coll) // Ugly … List<String> a = new Array. List<String>(); a. add("Z"); a. add("A"); a. add("M"); a. add("T"); a. add("G"); [Z, A, M, T, G] System. out. println(a); A System. out. println(Collections. min(a)); System. out. println(Collections. max(a)); Z Collections. sort(a); [A, G, M, T, Z] System. out. println(a); Collections. shuffle(a); [G, M, T, Z, A] System. out. println(a); 12

Tree. Set implements Set w Set<E> collections with no duplicates — — More formally,

Tree. Set implements Set w Set<E> collections with no duplicates — — More formally, sets contain no pair of elements e 1 and e 2 such that e 1. equals(e 2) Has the same methods as Collection, but intended to have different meanings public interface Set<E> extends Collection<E> { boolean add(E o); // Do not allow duplicates 13

Set and Sorted. Set w The Set<E> interface extends Collection w Two classes that

Set and Sorted. Set w The Set<E> interface extends Collection 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(), Sorted. Set<E> tail. Set(E from. Element), Sorted. Set<E> head. Set(E from. Element), E last(), Sorted. Set<E> sub. Set(E from. Element, E to. Element) 14

Set or Sorted. Set? Set<String> names = new Tree. Set<String>(); names. add("Sandeep"); names. add("Chris");

Set or Sorted. Set? 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 Change to Hash. Set 15

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 — 16

Map Operations w Java's Hash. Map<K, V> — public V put(K key, V value)

Map Operations w Java's Hash. Map<K, V> — public V put(K key, V value) • associates key to value and stores mapping — public V get(Object key) • associates the value to which key is mapped or null — public boolean contains. Key(Object key) • returns true if the Map already uses the key — public V remove(Object key) • Returns previous value associated with specified key, or null if there was no mapping for key. — Collection<V> values() • get a collection you can iterate over 17

Sample Code Map<Integer, String> map = new Hash. Map<Integer, String>(); map. put(2, "Ali"); map.

Sample Code Map<Integer, String> map = new Hash. Map<Integer, String>(); map. put(2, "Ali"); map. put(1, "Jaime"); map. put(3, "Alex"); map. put(1, "Dylan"); // Wipes out old System. out. println(map); System. out. println(map. key. Set()); System. out. println(map. values()); 18

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 19

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()); 20

Another use of interfaces There a number of situations in software engineering when it

Another use of interfaces There a number of situations in software engineering when it is important for disparate groups of programmers to agree to a "contract" that spells out how their software interacts. Each group should be able to write their code without any knowledge of how the other group's code is written. Generally speaking, interfaces are such contracts. http: //docs. oracle. com/javase/tutorial/java/Iand. I/createinterface. html 21