{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

15-set-map-iterator

# 15-set-map-iterator - CSE 143 Lecture 15 Sets and Maps...

This preview shows pages 1–7. Sign up to view the full content.

CSE 143 Lecture 15 Sets and Maps; Iterators reading: 11.1 - 11.3; 13.2 slides created by Marty Stepp http://www.cs.washington.edu/143/

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 Exercise • Write a program that counts the number of unique words in a large text file (say, Moby Dick or the King James Bible). Store the words in a collection and report the # of unique words. Once you've created this collection, allow the user to search it to see whether various words appear in the text file. • What collection is appropriate for this problem?
3 Empirical analysis (13.2) Running a program and measuring its performance System.currentTimeMillis() Returns an integer representing the number of milliseconds that have passed since 12:00am, January 1, 1970. • The result is returned as a value of type long , which is like int but with a larger numeric range (64 bits vs. 32). Can be called twice to see how many milliseconds have elapsed between two points in a program. • How much time does it take to store Moby Dick into a List ?

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 Sets (11.2) set : A collection of unique values (no duplicates allowed) that can perform the following operations efficiently: add, remove, search (contains) We don't think of a set as having indexes; we just add things to the set in general and don't worry about order set.contains("to") true set "the" "of" "from" "to" "she" "you" "him" "why" "in" "down" "by" "if" set.contains("be") false
5 Set implementation • in Java, sets are represented by Set interface in java.util •Set is implemented by HashSet and TreeSet classes – HashSet : implemented using a "hash table" array; very fast: O(1) for all operations elements are stored in unpredictable order – TreeSet : implemented using a "binary search tree"; pretty fast: O(log N) for all operations elements are stored in sorted order – LinkedHashSet : O(1) but stores in order of insertion

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
6 Set methods List<String> list = new ArrayList<String>(); ... Set<Integer> set = new TreeSet<Integer>(); // empty Set<String> set2 = new HashSet<String>( list ); can construct an empty set, or one based on a given collection removes all elements of the set clear() returns true if the set's size is 0 isEmpty() returns true if the given value is found in this set contains(
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}