15-set-map-iterator - CSE143 Lecture15...

Info icon This 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/
Image of page 1

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

View Full Document Right 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?
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 ?
Image of page 3

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

View Full Document Right 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
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
Image of page 5

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

View Full Document Right Arrow Icon
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 add( value ) adds the given value to the set contains( value ) returns  true
Image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern