220 likes | 233 Views
Explore time complexity analysis for methods like isEmpty() and contains(), compare ArrayIntList and LinkedIntList, and learn about Iterator interface for efficient iteration in Java collections.
E N D
CSE 143Lecture 21 Iterators slides created by Alyssa Harding http://www.cs.washington.edu/143/
AbstractIntList review • Recall our AbstractIntList class public abstract class AbstractIntList implements IntList { public abstract int indexOf(int value); public boolean contains(int value) return indexOf(value) >= 0; } ... } It contains both methods with bodies and abstract methods
AbstractIntList review • Now let’s examine the complexity of some of the methods • Recall that time complexity refers to the growth rate of how long code takes as the input size increases • O(1): no matter how much input there is, we only have to do one step • O(n): if there is n amount of input, we’ll have to do n steps
isEmpty() complexity • What’s the complexity of isEmpty() for ArrayIntList? public boolean isEmpty() return size() == 0; } • Its size() method returns the size field • This is O(1)
isEmpty() complexity • What’s the complexity of isEmpty() for LinkedIntList? public boolean isEmpty() return size() == 0; } • Its size() method loops through all the nodes to calculate the size • This is O(n)
contains() complexity • What’s the complexity of contains() for ArrayIntList? public abstract int indexOf(int value); public boolean contains(int value) return indexOf(value) >= 0; } • contains() calls indexOf(), which looks at every value • This is O(n)
contains() complexity • What’s the complexity of contains() for LinkedIntList? public abstract int indexOf(int value); public boolean contains(int value) return indexOf(value) >= 0; } • contains() calls indexOf(), which looks at every value • This is O(n)
equals() complexity • What’s the complexity of equals() for ArrayIntList? public boolean equals(Object other) { if ( other instanceof IntList ) { IntList otherList = (IntList)other; if ( this.size() == otherList.size() ) { for (int i = 0; i < this.size(); i++) { if ( this.get(i) != otherList.get(i) ) return false; } return true; } } return false; } The most repeated line of code is getting a value from the list, so this is our most significant line
equals() complexity • What’s the complexity of equals() for ArrayIntList? • If there are n values, we’ll loop through n times • This is O(n) • For each value, we get the value from the array • This is O(1) • Overall, this is O(n) * O(1) = O(n)
equals() complexity • What’s the complexity of equals() for LinkedIntList? • If there are n values, we’ll loop through n times • This is O(n) • For each value, we get the value by looping throughthe nodes in the list • This is O(n) • Overall, this is O(n) * O(n) = O(n2)
Complexity comparison • Ouch! • Our code worked pretty well for ArrayIntList, but not LinkedIntList • Can we both reduce redundancy and maintain efficiency?
Iterator<E> • Java gives us an Iterator<E> that we can use to loop through the values in a collection • http://java.sun.com/javase/6/docs/api/java/util/Iterator.html public interface Iterator<E> { // returns true if another value exists public booleanhasNext(); // returns the next value in the collection public E next(); // removes the current value public void remove(); }
Iterable<E> interface • Java also gives us a way of letting it know that a collection can be iterated over • http://java.sun.com/javase/6/docs/api/java/lang/Iterable.html public interface Iterable<E> { public Iterator<E> iterator(); }
Iterable<E> interface • Any class that implements this interface musthave an iterator public interface IntList extends Iterable<Integer> { ... }
Iterable<E> interface • First we’d need to write iterators for ArrayIntList and LinkedIntList • What would we need to keep track of for the ArrayIntList iterator? • the index we’re examining • How fast would it be to look at the next value? • We just increment the index • O(1)
Iterable<E> interface • First we’d need to write iterators for ArrayIntList and LinkedIntList • What would we need to keep track of forthe LinkedIntList iterator? • the node that we’re examining • How fast would it be to look at the next value? • We just increment the index • O(1)
Iterator example • Now we can use an iterator to loop through our list: IntList list1 = new ArrayIntList();list1.add(7);list1.add(42); Iterator<Integer> it = list.iterator(); while (it.hasNext()) { System.out.println(it.next()); }
Iterator example • Now we can also use a foreach loop with any class that is Iterable: foreach (int i : list) System.out.println(i); • This accomplishes the same thing as the previous slide,but looks much cleaner
indexOf() • Now we can use iterators for indexOf() for any AbstractIntList public int indexOf(int value) { Iterator<Integer> it = iterator(); int i = 0; while ( it.hasNext() ) { if ( it.next() == value ) return i; } return -1; }
isEmpty() • Instead of calculating the entire size, we can check whether our list has any elements public boolean isEmpty() return !iterator().hasNext(); }
equals() • The next method is more complex: public boolean equals(Object other) { if ( other instanceof IntList ) { Iterator<Integer> it1 = this.iterator(); Iterator<Integer> it2 = (IntList)other.iterator(); while ( it1.hasNext() && it2.hasNext() ) { if ( it1.next() != it2.next() ) return false; } return !it1.hasNext() && !it2.hasNext(); } } return false; }
Complexity revisited • Now they’re the same!