15-set-map - CSE 143 Lecture 15 Sets and Maps reading: 11.1...

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

CSE 143 Lecture 15 Sets and Maps 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 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

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

View Full Document
4 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
5 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() returns the number of elements in list size() removes the given value from the set remove( value ) adds the given value to the set add( value )

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

View Full Document
6 Set operations returns an array of the elements in this set toArray() removes elements not found in given collection from this set retainAll( coll ) removes all elements in the given collection from this set removeAll( coll ) returns an object used to examine set's contents (seen later) iterator() returns true if given other set contains the same elements equals( set ) returns true if this set contains every element from given set containsAll( coll ) adds all elements from the given collection to this set addAll( collection ) addAll retainAll removeAll
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/30/2011 for the course COMPUTER S 143 taught by Professor John during the Spring '11 term at Washington State Community College.

Page1 / 21

15-set-map - CSE 143 Lecture 15 Sets and Maps reading: 11.1...

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

View Full Document
Ask a homework question - tutors are online