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

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

View Full Document Right Arrow Icon
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/
Background image of page 1

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

View Full DocumentRight Arrow Icon
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?
Background image of page 2
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 ?
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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( value ) returns a string such as "[3, 42, -7, 15]" toString()
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 22

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

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online