What is a Data Structure? Chapter 20 Lists, Stacks, Queues, and Priority Queues A collection of data elements Stored in a structured fashion With operations that access & manipulate elements Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 1 2 rights reserved. rights reserved. Three Types of Collections Java Collections Framework (interfaces that implement Collection) ▪ Lists – Stores elements in sequential order Collection is a java interface ▪ Ordered Collection – Java.utils.Container ▪ Sets – lists allow duplicates, sets do not Defines abstract methods for objects that ▪ Unordered Collection contain other objects ( elements ) ▪ Maps – data structure based on {key, value} – Add(E e) pair These are – Remove(E e) ▪ Holds two objects per entry examples, not an exhaustive ▪ May contain duplicate values – Contains(E e) list ▪ Keys are always unique – toArray(E e) Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 3 4 rights reserved. rights reserved. The List Interface Java Collections Framework Elements stored in sequential order Set and List are subinterfaces of Collection. Programs can specify where an element is stored. Programs can access elements by index. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 5 6 rights reserved. rights reserved.
The List Interface, cont. Iterators An iterator is a generalization of a reference – An abstract way of accessing an element Iterator is an interface – Java.util.Iterator Methods for sequentially accessing elements – hasNext() – next() – remove() Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 7 8 rights reserved. rights reserved. «interface» java.lang.Iterable<E> +iterator(): Iterator<E> Returns an iterator for the elements in this collection. Why Iterators? The Collection Interface +forEach(action: Consumer<? super Performs an action for each element in this iterator. E>): default void The Iterable interface has an Iterator (diamond) «interface» allowing sequential access to the elements Iterators allow you to abstract away the data java.util.Collection<E> Adds a new element e to this collection. +add(e: E): boolean structure +addAll(c: Collection<? extends E>): Adds all the elements in the collection c to this collection. boolean +clear(): void Removes all the elements from this collection. +contains(o: Object): boolean Returns true if this collection contains the element o . Given an iterator, you can access elements +containsAll(c: Returns true if this collection contains all the elements in c . Collection<?>):boolean +isEmpty(): boolean Returns true if this collection contains no elements. in order Removes the element o from this collection. +remove(o: Object): boolean Removes all the elements in c from this collection. +removeAll(c: Collection<?>): – In a list boolean Retains the elements that are both in c and in this collection. +retainAll(c: Collection<?>): boolean Returns the number of elements in this collection. – In a set +size(): int Returns an array of Object for the elements in this collection. +toArray(): Object[] Returns a stream from this collection (covered in Ch 23). +stream(): Stream default – In a map Returns a parallel stream from this collection (covered in Ch +parallelStream(): Stream default 23). The Iterable interface requires an object to «interface» java.util.Iterator<E> implement iterators +hasNext(): boolean Returns true if this iterator has more elements to traverse. +next(): E Returns the next element from this iterator. +remove(): void Removes the last element obtained using the next method. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 9 10 rights reserved. rights reserved. The List Iterator Array vs ArrayList vs LinkedList • ArrayList class and the LinkedList class • Concrete implementations of the List interface. • Usage depends on your specific needs. • Efficiency • ArrayList – Fast random access through indices • LinkedList – Fast insertion and deletion of elements at specific locations • Array – Does not support insertion or deletion of elements • But the most efficient if insert/delete not needed Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 11 12 rights reserved. rights reserved.
java.util.LinkedList java.util.ArrayList «interface» java.util.Collection<E> «interface» java.util.Collection<E> «interface» java.util.List<E> «interface» java.util.List<E> java.util.LinkedList<E> +LinkedList() Creates a default empty linked list. +LinkedList(c: Collection<? extends E>) java.util.ArrayList<E> Creates a linked list from an existing collection. +addFirst(o: E): void Adds the object to the head of this list. +ArrayList() Creates an empty list with the default initial capacity. +addLast(o: E): void Adds the object to the tail of this list. +ArrayList(c: Collection<? extends E>) Creates an array list from an existing collection. +getFirst(): E Returns the first element from this list. +ArrayList(initialCapacity: int) Creates an empty list with the specified initial capacity. +getLast(): E Returns the last element from this list. +removeFirst(): E +trimToSize(): void Trims the capacity of this ArrayList instance to be the Returns and removes the first element from this list. list's current size. +removeLast(): E Returns and removes the last element from this list. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 13 14 rights reserved. rights reserved. Example: Using ArrayList and interface List Hierarchy Iterable LinkedList interface Iterator interface Create an array list filled with numbers Collection AbstractCollection Insert new elements in specific locations interface Create a linked list from the array list AbstractList List Insert and remove elements from the list. Traverse the list forward and backward. AbstractSequentialList ArrayList LinkedList Run TestArrayAndLinkedList Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 15 16 rights reserved. rights reserved. The Comparator Interface Comparable vs Comparator An interface for comparing arbitrary elements Comparable – The elements don’t have to be Comparable – Implemented with compareTo – Java.util.Comparator – Defines the natural order for the object Defines a method called compare(T o1, T o2) i.e. the order you will use most of the time Used as an argument to methods like sort(collection, Comparator CompareObject) – Implemented with compare() – Define an order for a specific purpose Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 17 18 rights reserved. rights reserved.
Recommend
More recommend