CS49J Lesson 24 – Sets

Ch 15: 15.3

Big Java 6

When you come in

  1. Connect to the Internet
  2. Log in to Piazza
  3. Start Eclipse or your IDE
  4. Navigate to laughton.com/obrien/sjsu/
    cs49j/lessons and open this lesson

Questions

Concept - Sets

Concept - Set interface

Activity - Set implementations

Record your participation in Piazza clicker question Lesson24 Q1

There are two classes in the Java library that implement the Set interface. What are they? Click all that apply. (section 15.3)

  1. HashSet
  2. MapSet
  3. LinkedSet
  4. TreeSet
  5. ArraySet
  6. ListSet

Concept - HashSet

 

Concept - TreeSet

 

Activity - Use a set?

Record your participation in Piazza clicker question Lesson24 Q2

I want to keep a collection of all my grades for a course I am taking. Since we just learned about sets, I want to try to use a HashSet. Will that work? Think about why or why not.

Activity - Spell check

Record your participation in Piazza clicker question Lesson24 Q3

In your text in section 15.3.2, Dr Horstmann implements a spell check application (opens in a new window) using sets. Could the implementation use an ArrayList for documentWords instead? Think about why or why not.

Activity - Which Set implementation?

Record your participation in Piazza clicker question Lesson24 Q4

I want to print all the unique words in a text in alphabetical order. Can I use a set? If so, which implementation should I use?

  1. no
  2. yes, HashSet
  3. yes, TreeSet
  4. yes, LinkedSet

Activity - SpellCheck

Record your participation in Piazza clicker question Lesson24 Q5

Suppose you wanted the misspelled words in aphabetical order. You would need to change line 40 of the SpellCheck program to use a TreeSet instead of a HashSet.

Set<String> words = new HashSet<>(); // current line 40
Set<String> words = new TreeSet<>(); // new line 40

What else would need to change?

  1. everything
  2. nothing except add an import for TreeSet
  3. the return type of the readWords method
  4. it won't compile

 

How would the output change?

Activity - Sets

Record your participation in Piazza clicker question Lesson24 Q6

What is wrong with the following test to check whether the Set<String> names contains the elements "Thomas", "Diana", and "Harry" and only those elements?

if (words.toString().equals("[Thomas, Diana, Harry]")) . . .

  1. There is no equals method for Sets
  2. A set might contain the elements in a different order
  3. All elements in a set are unique
  4. "Thomas" will be printed last because it is longest

Concept - Binary Search Tree

Tree

Binary Tree

Binary Search Tree

bst