1 / 47

Containers (Data Structures)

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)

sharne
Download Presentation

Containers (Data Structures)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Containers(Data Structures)

  2. 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

  3. Why containers? • What’s wrong with arrays?

  4. 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

  5. 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?

  6. 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.

  7. ArrayLists: • import java.util.ArrayList; • ArrayList al = new ArrayList(); // this works… • What type is al? • This is a container: what does it contain?

  8. 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!

  9. Example: • publicstaticvoid main(String[] args) { • ArrayList<Dog> DogShowWinners = newArrayList(); • 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? 5 Spot Fang Prince Puppy Bernice Sandy

  10. Using dog methods? • publicstaticvoid main(String[] args) { • ArrayList<Dog> DogShowWinners = newArrayList(); • 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")); • } • System.out.println(DogShowWinners.get(3).getName());

  11. Diff between arrays and ArrayLists

  12. 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>

  13. 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?

  14. 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)

  15. 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

  16. 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?

  17. 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

  18. Back to Linked Lists LinkedList<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”); }

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

  20. 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).

  21. Singly Linked List class: publicclass Node<AnyType> { publicAnyTypedata; publicNode<AnyType> next; public Node(AnyType data, Node<AnyType> next) { this.data = data; this.next = next; } publicNode(AnyTypedata) { this.data = data; this.next = Null; } }

  22. LinkedLists vs ArrayLists For ArrayList<E> • get(int index) is O(1) //main benefit of ArrayList<E> • add(E element) isO(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)

  23. 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

  24. 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

  25. 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.

  26. 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

  27. 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()

  28. 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?

  29. 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

  30. Idea: Add 51? Add 15?

  31. 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()

  32. 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()); }

  33. 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)); } } 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 Node while (true) { parent = focusNode; // root is the top parent so we start the if (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 child if (focusNode == null) { // If the left child has no children parent.leftChild= newNode; // then place the new node on the left of it return; // 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 it return; // All Done } } else { //already in tree return; } } } } public Node findNode(int key) { Node focusNode = root; // Start at the top of the tree while (focusNode.key != key) { // While we haven't found the Node keep looking if (key < focusNode.key) { // If we should search to the left focusNode= 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 nod System.out.print(focusNode + " "); // Visit the currently focused on node PrintTree(focusNode.rightChild); // Traverse the right node } }

  34. 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

  35. 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)

  36. MapMethods • Maps keys to values • No duplicate keys • Each key maps to a value Methods: • void clear( )Removes all key/value pairs from the invoking map. • booleancontainsKey(Object k)Returns true if the invoking map contains k as a key. Otherwise, returns false. • booleancontainsValue(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. • booleanisEmpty( )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.

  37. Map Implementations • HashMap (uses hashing function on keys) • Fast • TreeMap (orders keys) • Sorted ordering • Key-ordered iteration • LinkedHashMap (guess) • Fast • Insertion-order iteration

  38. Declaring Maps • Declare types for both keys and values • Class HashMap<K,V> HashMap<String, ArrayList<String>> map = new(); Keys are Strings Values are Lists of Strings

  39. 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 integer Integer k = 21; String value = map.get(k); // gets value associated with key System.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); TreeMapsortedHashMap = new TreeMap(map); // cool stuff System.out.println("Sorted HashMap: " + sortedHashMap); map.clear(); //clears hashmap , removes all element System.out.println("Is HashMap is empty: " + map.isEmpty()); System.out.println("Size of Map: " + map.size());

  40. 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

  41. 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 iterator In most cases, the iterator is the more efficient and better way to traverse the objects in a collection

  42. How to use an iterator: • Iterator enables you to cycle through a collection • obtaining or removing elements • ListIteratorextends 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( ).

  43. Traversing Collections: Iterators • Java Interface • Object next() • get the next element • booleanhasNext() • are there more elements? • void remove() • remove the previous element • Only safeway to remove elements during iteration

  44. 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( ). • booleanhasNext( )Returns true if there is a next element. Otherwise, returns false. • booleanhasPrevious( )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. • intnextIndex( )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. • intpreviousIndex( )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( ).

  45. 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 ListIteratorlitr = 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(); } }

  46. 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(); } } }

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

More Related