containers (data structures)

47
Containers (Data Structures)

Upload: sharne

Post on 06-Jan-2016

32 views

Category:

Documents


0 download

DESCRIPTION

Containers (Data Structures). Container object: data Structure. An object that groups things. We group things so we can store, retrieve, and manipulate data. Containers: Automatically grow in size Only contain objects No primitive types (e.g., int , float, double) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Containers (Data Structures)

Containers(Data Structures)

Page 2: Containers (Data Structures)

Container object: data Structure

• An object that groups things. We group things so we can store, retrieve, and manipulate data.

• Containers:• Automatically grow in size• Only contain objects

• No primitive types (e.g., int, float, double)• Have a bunch of convenient methods written for us

• E.g., add

Page 3: Containers (Data Structures)

Why containers?

• What’s wrong with arrays?

Page 4: Containers (Data Structures)

Methods of Containers (collections):

• Containers store data and provide methods that you’d use with stored data

• Add(object)• Remove(object)• Size• Contains• Equals• Iterator (we’ll talk about this one later)

• One type of container: Lists

Page 5: Containers (Data Structures)

ArrayLists:

• Container: used to store a list of some objects• Remember arrays?Student[] classroom = new Student[60];• Why is this a pain? Do you see any difficulties with this array?

• How do you add a 61st student?• What if students drop?

Page 6: Containers (Data Structures)

ArrayLists:• The ArrayList class has the following methods:

• add(o) appends the new object o to the end of the list• add(i, o) adds the object o at the index i (an int)• addAll(Collection<?> c) adds everythin from collection c to the ArrayList (at the end)• clear() removes all elements from the list• contains(o) returns true if the list contains the object o• get(i) returns the element located at index i in the list• indexOf(o) returns the index of the object o in the list• isEmpty() returns true if the list contains no elements• lastIndexOf(o) returns the index of the last matching element in the list• remove(index) removes the element at index and returns it from the list• remove(object) removes the object from the list and returns true if successful, false otherwise• removeAll(Collection<?> c) remove from this list all the elements in the collection list• removeRange(fromindex, toindex) removes everything in the list from the fromindex to the toindex (excluding the toindex)• retainAll(Collection<?> c) keeps in the list only objects in the collection• set(index, o) sets the element at the index to the object o• size() returns the number of elements in the list• subList(fromindex, toindex) returns a list from the from index to the toindex.

Page 7: Containers (Data Structures)

ArrayLists: • import java.util.ArrayList;

• ArrayList al = new ArrayList(); // this works…

• What type is al?• This is a container: what does it contain?

Page 8: Containers (Data Structures)

ArrayLists

• Work on Generic Types (we’ll discuss this more shortly)• We don’t have to specify the type for the methods in ArrayList to work• But we can…

• And then we can use the methods and values associated with the class type:• ArrayList<Dog> al = new ArrayList<Dog>();• Or the shortcut:• ArrayList<Dog> aI= new ArrayList(); // this is better!

Page 9: Containers (Data Structures)

Example:public static void main(String[] args) {

ArrayList<Dog> DogShowWinners = new ArrayList();DogShowWinners.add(new Dog("Spot"));DogShowWinners.add(new Dog("Fang"));DogShowWinners.add(new Dog("Puppy"));DogShowWinners.add(new Dog("Bernice"));DogShowWinners.add(new Dog("Sandy"));System.out.println("List size? "+DogShowWinners.size());DogShowWinners.add(2,new Dog("Prince"));for (Dog x: DogShowWinners) {

System.out.println(x.getName());}

}

List size? 5SpotFangPrincePuppyBerniceSandy

Page 10: Containers (Data Structures)

Using dog methods?

• System.out.println(DogShowWinners.get(3).getName());

public static void main(String[] args) {ArrayList<Dog> DogShowWinners = new ArrayList();DogShowWinners.add(new Dog("Spot"));DogShowWinners.add(new Dog("Fang"));DogShowWinners.add(new Dog("Puppy"));DogShowWinners.add(new Dog("Bernice"));DogShowWinners.add(new Dog("Sandy"));System.out.println("List size? "+DogShowWinners.size());DogShowWinners.add(2,new Dog("Prince"));

}

Page 11: Containers (Data Structures)

Diff between arrays and ArrayLists

Operation Array ArrayList

Creating

Accessing

Updating

Getting Size

Adding a new element

Insertnig a new element

Removing an element

Removing all elements

String[] a = new String[10]

a[index]

a[index] = “London”;

a.length

ArrayList<String> list = new ArrayList();

list.get(index);

list.set(index,”London”);

list.size();

list.add(“London”);

list.add(index,”London”);

list.remove(“index”);

list.clear();

Page 12: Containers (Data Structures)

ArrayLists:

Can’t do:• ArrayList<int> x = new ArrayList();(Can’t use primitive types, must use object types)Instead:• ArrayList<Integer> x = new ArrayList();• ArrayList<Character>• ArrayList<Double>• ArrayList<Boolean>

Page 13: Containers (Data Structures)

Behind the scenes:

• We create a relatively long array that can resize dynamically• This means that if we add an object, we move everything from where

we added it down by one• If we’re adding to the end, how many operations do we need to do?• If we add to the beginning of the list?

• Removing an object – how many operations will we need to remove the object from the array?

Page 14: Containers (Data Structures)

Linked Lists

• Another type of list in our containers• Does a lot of what ArrayLists do:

• Trying to find ways of being efficient (we’ll talk about this more later)

Page 15: Containers (Data Structures)

Linked List methods:• The LinkedList class has the following methods:

• add(o) appends the new object o to the end of the list• add(i, o) adds the object o at the index i (an int)• addAll(Conllection<?> c) adds all of the elements in c to the end of the linked list• clear() removes all elements from the list• contains(o) returns true if the list contains the object o• get(i) returns the element located at index i in the list• indexOf(o) returns the index of the object o in the list• isEmpty() returns true if the list contains no elements• lastIndexOf(o) returns the index of the last matching element in the list• remove(index) removes the element at index and returns it from the list• remove(object) removes the object from the list and returns true if successful, false otherwise• removeAll(Collection<?> c) remove from this list all the elements in the collection list• retainAll(Collection<?> c) keeps in the list only objects in the collection• set(index, o) sets the element at the index to the object o• size() returns the number of elements in the list

Page 16: Containers (Data Structures)

Why so many methods in common with ArrayLists?• Both ArrayLists and LinkedLists inherit from the Collection Interface

• What does this mean?

• What are the implications?

Page 17: Containers (Data Structures)

Collection Interface

• add(? x) //? Is the type – type varies• addAll(Collection<?> c)• clear()• contains(? x)• containsAll(Collection<?> c)• equals(? x)• hashCode()• isEmpty()• remove(? x)• removeAll(Collection<?> c)• retainAll(Collection<?> c)• Size()• toArray()

Anything that

Page 18: Containers (Data Structures)

Back to Linked ListsLinkedList<String> x = new LinkedList();x.add(“cat”);x.add(“dog”);x.add(“bunny”);x.remove(“dog”);

ArrayList<String> q = new ArrayList();q.add(“cat”);q.add(“puppy”);q.add(“bunny”);q.add(“cow”);if (q.containsAll(x) ) {

System.out.println(“all the animals are in the list”);}

Page 19: Containers (Data Structures)

So why do we have LinkedLists and ArrayLists?• Because of how they work…

Page 20: Containers (Data Structures)

Linked Lists (behind the scenes):

• Linked Lists are a series of single objects, with each object having a pointer (a link) to the next object.

• Each element (node) consists of 2 things: data and a pointer to another node.

• The first node is the head, and the last is the tail (pointing to null)• This is a representation of a singly linked list (meaning each node

points to the next node in the list).

Page 21: Containers (Data Structures)

Singly Linked List class: public class Node<AnyType> { public AnyType data; public Node<AnyType> next;

public Node(AnyType data, Node<AnyType> next) { this.data = data; this.next = next; } public Node(AnyType data) { this.data = data; this.next = Null; } }

Page 22: Containers (Data Structures)

LinkedLists vs ArrayLists

For ArrayList<E>• get(int index) is O(1) //main benefit of ArrayList<E> • add(E element) is O(n) worst-case (since the array

must be resized and copied)• add(int index, E element) is O(n) worst-case (as

above) • remove(int index) is O(n - index) (i.e. removing last

is O(1))

For LinkedList<E>• get(int index) is O(n) • add(E element) is O(1) // main benefit of

LinkedList<E>• add(int index, E element) is O(n)

• remove(int index) is O(n)

Page 23: Containers (Data Structures)

Back to Collections• Lists:

• ArrayList• LinkedList

• Sets• No duplicates• No order (TreeSets happen to have an order)

• Maps (more to come)• Mapping a key to a value

• E.g., our soccer players: map each player to a position• Each player is unique, each position doesn’t have to be• The player’s name should bring up their position

Page 24: Containers (Data Structures)

Sets

• Uses the Collection interface• So has to implement the method signatures in the Collection interface

• E.g., add, remove, contains, • In this case, the add method must not only add the object to the set, it must check to

see if the object is already in the set and only add it if it is not already in the set)• Note that we don’t have a get method (does Collection interface have a get signature?)

• Get doesn’t make sense with sets:• Set.get(index)

• There’s no order to a set

• TreeSet/HashSet• Both sets implemented by java using the Collection interface

Page 25: Containers (Data Structures)

Hash Set• Used more frequently• Faster than TreeSet for contains, add, remove

• Generally faster than lists too! (but not the same as lists)

• No ordering• Idea: we take an object that we want to place into the set, then use a

“hashing function” on the object to get a unique index (hopefully). The object is then placed at that unique index in an array.

• E.g., a ridiculously simple hashing function (that no one would ever use) is to take the numeric equivalent of all the characters in an object’s fields, add them together, and then take that number modulus the length of the array into which we are placing the object.

Page 26: Containers (Data Structures)

HashSets• Idea: Use a “hashing function” on an object to get a unique index and

place the object at that unique index in an array.• Issues to think about :

• What if more than one object generates the same index? What do we want to do then?

• How big should we make the original array? Is this wasteful?• How do we make sure the hash function doesn’t generate index values outside of

the range of the array?

• Goal: to be able to locate objects quickly • The hash function should generate the same index for all objects that are equal

Page 27: Containers (Data Structures)

Methods:• add(o) appends the new object o to the set• addAll(Collection<?> c) adds everything from collection c to the HashSet (at the end)• clear() removes all elements from the set• contains(o) returns true if the set contains the object o• containsAll(Collection<?> c) returns true if the set contains all the elements in the collection

set• equals(Collection<?> c) checks if the collection c and the hashset have the same set of

objects• isEmpty() returns true if the set contains no elements• remove(object) removes the object from the set and returns true if successful, false

otherwise• removeAll(Collection<?> c) remove from this set all the elements in the collection set• retainAll(Collection<?> c) keeps in the set only objects in the collection• size() returns the number of elements in the set• toArray()• toString()

Page 28: Containers (Data Structures)

HashSet<String> hs = new HashSet(); // add elements to the hash set hs.add("B"); hs.add("A"); hs.clear();hs.add("D"); hs.add("E"); hs.add("C"); hs.add("F"); hs.remove("F"); hs.remove(1);hs.add("D"); hs.add(1, "U"); hs.add ("K"); System.out.println(hs);

// What does this give you?

Page 29: Containers (Data Structures)

TreeSet• Tree

• Faster than HashSet for subset

• Ordered!

• Theoretically should be pretty quick for adding, removing, and contains (log n time), even while maintaining sorted order

Page 30: Containers (Data Structures)

Idea:Add 51? Add 15?

Page 31: Containers (Data Structures)

Methods:• add(o) appends the new object o to the set• addAll(Collection<?> c) adds everything from collection c to the HashSet (at the end)• ceiling(? 0) Returns the least element in this set greater than or equal to the given element, or null if there is no such element. • clear() removes all elements from the set• contains(o) returns true if the set contains the object o• containsAll(Collection<?> c) returns true if the set contains all the elements in the collection set• descendingSet() – returns a reverse order view of the set• equals(Collection<?> c) checks if the collection c and the hashset have the same set of objects• first() returns the smallest element in the set• floor(? o) returns the greatest element in the set less than o• headset (? o) returns a view of the set up to the object o• higher(? o) retuns the first element after o in the set• isEmpty() returns true if the set contains no elements• last() – returns the last object in the set• lower(? o) – returns the greatest object in the set that is less than the object o• pollFirst() – removes and returns the first object in the set• pollLast() – removes and returns the last object in the set• remove(object) removes the object from the set and returns true if successful, false otherwise• removeAll(Collection<?> c) remove from this set all the elements in the collection set• retainAll(Collection<?> c) keeps in the set only objects in the collection• size() returns the number of elements in the set• subset(? o1 ? o2) – returns a view of the set of objects that range from the first object (inclusive) to the last object (exclusive)• tailSet(? o) – returns a view of the set whose elements are greater than or equal to the object• toArray()• toString()

Page 32: Containers (Data Structures)

TreeSet

TreeSet<Integer> tree = new TreeSet();tree.add(12);tree.add(63);tree.add(34);tree.add(45);tree.add(16);tree.add(22);tree.add(54); for (Integer x: tree) {

System.out.print(x + " ");}System.out.println();System.out.println(tree.subSet(22,54));if (tree.isEmpty()) {

System.out.print("Tree Set is empty.");} else {

System.out.println("Tree Set size: " + tree.size());} System.out.println("First data: " + tree.first());System.out.println("Last data: " + tree.last());if (tree.remove(45)) {

System.out.println("Data is removed from tree set");} else {

System.out.println("Data doesn't exist!");}tree.clear();if (tree.isEmpty()) {

System.out.print("Tree Set is empty.");} else {

System.out.println("Tree Set size: " + tree.size());}

Page 33: Containers (Data Structures)

public class Tree {Node root;public void addNode(int key) {

Node newNode = new Node(key); // Create a new Node and initialize if (root == null) { // If there is no root this becomes root

root = newNode;} else { // Set root as the Node we will start with as we traverse the tree

Node focusNode = root;Node parent; // Future parent for our new Nodewhile (true) {

parent = focusNode; // root is the top parent so we start theif (key < focusNode.key) { // Check if the new node should go on the left side of the parent nod

focusNode = focusNode.leftChild; // Switch focus to the left childif (focusNode == null) { // If the left child has no children

parent.leftChild = newNode; // then place the new node on the left of itreturn; // All Done

}} else if (key > focusNode.key){ // If we get here put the node on the right

focusNode = focusNode.rightChild;if (focusNode == null) { // If the right child has no children

parent.rightChild = newNode; // then place the new node on the right of itreturn; // All Done

}}else { //already in tree

return;}

}}

}public Node findNode(int key) {

Node focusNode = root; // Start at the top of the treewhile (focusNode.key != key) { // While we haven't found the Node keep looking

if (key < focusNode.key) { // If we should search to the leftfocusNode = focusNode.leftChild; // Shift the focus Node to the left child

} else {focusNode = focusNode.rightChild; // Shift the focus Node to the right child

}if (focusNode == null) { // The node wasn't found

return null;}

}return focusNode;

}public void PrintTree(Node focusNode) { // Recursion is used to go to one node and then go to its child nodes and so forth

if (focusNode != null) {PrintTree(focusNode.leftChild); // Traverse the left nodSystem.out.print(focusNode + " "); // Visit the currently focused on nodePrintTree(focusNode.rightChild); // Traverse the right node

}}

public class Node {int key;Node leftChild;Node rightChild;

public Node(int key) {this.key = key;

}

public String toString() {String str = "";str += key + " ";return str;

}

public static void main(String[] args) {Tree theTree = new Tree();theTree.addNode(50);theTree.addNode(30); theTree.addNode(15);theTree.addNode(30);theTree.PrintTree(theTree.root);System.out.println(theTree.findNode(30));System.out.println(theTree.findNode(15));

}}

Page 34: Containers (Data Structures)

Back to Collections• Lists:

• ArrayList• LinkedList

• Sets• No duplicates• No order (TreeSets happen to have an order)

• Maps (more to come)• Mapping a key to a value

• E.g., our soccer players: map each player to a position• Each player is unique, each position doesn’t have to be• The player’s name should bring up their position

Page 35: Containers (Data Structures)

• The Map interface maps unique keys to values. A key is an object that you use to retrieve a value at a later date.

• Given a key and a value, you can store the value in a Map object. After the value is stored, you can retrieve it by using its key.

• Examples: • web page url (key) -> web page content (value)• Word (key) -> count of word in document (value)• Student name (key) -> class schedule (value)

Page 36: Containers (Data Structures)

Map Methods• Maps keys to values• No duplicate keys

Each key maps to a valueMethods:• void clear( )

Removes all key/value pairs from the invoking map.• boolean containsKey(Object k)

Returns true if the invoking map contains k as a key. Otherwise, returns false.• boolean containsValue(Object v)

Returns true if the map contains v as a value. Otherwise, returns false.• Set entrySet( )

Returns a Set that contains the entries in the map. The set contains objects of type Map.Entry. This method provides a set-view of the invoking map.• boolean equals(Object obj)

Returns true if obj• Object get(Object k)

Returns the value associated with the key k. is a Map and contains the same entries. Otherwise, returns false.• boolean isEmpty( )

Returns true if the invoking map is empty. Otherwise, returns false.• Set keySet( )

Returns a Set that contains the keys in the invoking map. This method provides a set-view of the keys in the invoking map.• Object put(Object k, Object v)

Puts an entry in the invoking map, overwriting any previous value associated with the key. The key and value are k and v, respectively. Returns null if the key did not already exist. Otherwise, the previous value linked to the key is returned.

• void putAll(Map m)Puts all the entries from m into this map.

• Object remove(Object k)Removes the entry whose key equals k.

• int size( )Returns the number of key/value pairs in the map.

• Collection values( )Returns a collection containing the values in the map. This method provides a collection-view of the values in the map.

Page 37: Containers (Data Structures)

Map Implementations• HashMap (uses hashing function on keys)

Fast

• TreeMap (orders keys)Sorted orderingKey-ordered iteration

• LinkedHashMap (guess)FastInsertion-order iteration

Page 38: Containers (Data Structures)

Declaring Maps

• Declare types for both keys and values• Class HashMap<K,V>

HashMap<String, ArrayList<String>> map = new();

Keys are StringsValues are Lists of Strings

Page 39: Containers (Data Structures)

HashMap<Integer, String> map = new HashMap();map.put(21, "Twenty One"); // which is the key, and which is the value?//map.put(21.0, "Twenty One"); //this will throw an error because 21.0 is not integerInteger k = 21;String value = map.get(k); // gets value associated with keySystem.out.println("Key: " + key +" value: "+ value); map.put(31, "Thirty One"); System.out.println("Size of Map: " + map.size());System.out.println("Does HashMap contains 21 as key: " + map.containsKey(21));System.out.println("Does HashMap contains 21 as value: " + map.containsValue(21));System.out.println("Does HashMap contains Twenty One as value: " + map.containsValue("Twenty One")); map.put(41, "Thirty One");

System.out.println("Unsorted HashMap: " + map);TreeMap sortedHashMap = new TreeMap(map); // cool stuffSystem.out.println("Sorted HashMap: " + sortedHashMap);

map.clear(); //clears hashmap , removes all elementSystem.out.println("Is HashMap is empty: " + map.isEmpty());System.out.println("Size of Map: " + map.size());

Page 40: Containers (Data Structures)

Looping through map:

• More than one way:

for (Integer key : map.keySet()) {System.out.println("Key : " + key.toString() + " Value : "+ map.get(key));

}

Or we could use an Iterator

Page 41: Containers (Data Structures)

Traversing Collections (1)

• For-each loop:for (Object o : collection)

System.out.println(o);

• Equivalent to:for (Iterator i = collection.iterator(); i.hasNext();) {

Object o = i.next();

System.out.println(o);

}

Everything that inherits from Collections Interface has an iteratorIn most cases, the iterator is the more efficient and better way to traverse the objects in a collection

Page 42: Containers (Data Structures)

How to use an iterator:• Iterator enables you to cycle through a collection

• obtaining or removing elements

• ListIterator extends Iterator to allow bidirectional traversal of a list• Can go backwards and forward in list.

• To use an iterator:• Obtain an iterator (pointer) to the start of the collection

• call the collection's iterator( ) method.• Set up a loop that makes a call to hasNext( )

• Have the loop iterate as long as hasNext( ) returns true.• Within the loop, obtain each element by calling next( ).

Page 43: Containers (Data Structures)

Traversing Collections: Iterators

• Java Interface • Object next()

• get the next element

• boolean hasNext() • are there more elements?

• void remove() • remove the previous element• Only safe way to remove elements during iteration

Page 44: Containers (Data Structures)

ListIterator methods:

• void add(Object obj)Inserts obj into the list in front of the element that will be returned by the next call to next( ).

• boolean hasNext( )Returns true if there is a next element. Otherwise, returns false.

• boolean hasPrevious( )Returns true if there is a previous element. Otherwise, returns false.

• Object next( )Returns the next element. A NoSuchElementException is thrown if there is not a next element.

• int nextIndex( )Returns the index of the next element. If there is not a next element, returns the size of the list.

• Object previous( )Returns the previous element. A NoSuchElementException is thrown if there is not a previous element.

• int previousIndex( )Returns the index of the previous element. If there is not a previous element, returns -1.

• void remove( )Removes the current element from the list. An IllegalStateException is thrown if remove( ) is called before next( ) or previous( ) is invoked.

• void set(Object obj)Assigns obj to the current element. This is the element last returned by a call to either next( ) or previous( ).

Page 45: Containers (Data Structures)

public class IteratorDemo {

public static void main(String args[]) { // Create an array list ArrayList al = new ArrayList(); // add elements to the array list al.add("C"); al.add("A"); al.add("E"); al.add("B"); al.add("D"); al.add("F");

// Use iterator to display contents of al System.out.print("Original contents of al: "); Iterator itr = al.iterator(); while(itr.hasNext()) { Object element = itr.next(); System.out.print(element + " "); } System.out.println(); // Modify objects being iterated ListIterator litr = al.listIterator(); while(litr.hasNext()) { Object element = litr.next(); litr.set(element + "+"); } System.out.print("Modified contents of al: "); itr = al.iterator(); while(itr.hasNext()) { Object element = itr.next(); System.out.print(element + " "); } System.out.println();

// Now, display the list backwards System.out.print("Modified list backwards: "); while(litr.hasPrevious()) { Object element = litr.previous(); System.out.print(element + " "); } System.out.println(); }}

Page 46: Containers (Data Structures)

Filter Algorithm

void filter(Collection c) {

Iterator i = c.iterator();

while( i.hasNext() ) {

// if the next element does not

// adhere to the condition, remove it

if (!cond(i.next())) {

i.remove();

}

}

}

Page 47: Containers (Data Structures)

Linked Lists

• http://www.youtube.com/watch?v=195KUinjBpU