1 of 21

To do: November 15

Copy and paste WhereAmI code into:

Repl.it lab 05-07 setting up for file I/O.

2 of 21

To do: November 15

Solve the following equations by factoring. Then write the solution set:

1) x2 + x - 6 = 0

2) x2 + 6x + 9 = 0

3) x2 + x + 1 = 0

3 of 21

Go over homework

  • Work on USACO Where am I in Eclipse
    • upload screenshot after you submitted java file
    • not graded on whether tests pass - want to see you successfully uploaded file
  • Copy and paste code into Repl.it and submit - okay if not all tests passed

Open your WhereAmI.java program. How did we do?

4 of 21

The following are important

interfaces included in the

Java language ::

Collection

Set

Map

Java Interfaces

5 of 21

A Collection represents a group of objects, known as its elements. Some collections allow duplicate elements and others do not. Some are ordered and others unordered.

Collection

List

Set

Collection Interface

6 of 21

The Collection interface is the parent of the List interface and Set interface. The Collection interface has many methods listed including add(), clear(), remove(), and size().

Collection

List

Set

Collection Interface

7 of 21

The Set interface extends the Collection

interface and HashSet and TreeSet implements the Set interface.

Set

HashSet

SortedSet

TreeSet

Set Interface

8 of 21

A set is a group of items all of the same type of which none are duplicates.

What is a set?

9 of 21

Because Set is an interface, you cannot instantiate it.

Set bad = new Set(); //illegal

Set hash = new HashSet(); //legal

Set tree = new TreeSet(); //legal

Java Set

10 of 21

HashSet – a set ordered by each

item’s hashCode that is extremely time efficient. O(1) for search/insert/delete operations

TreeSet – a naturally ordered set that is very efficient, but not as efficient as HashSet.

O(logn) for search/insert/delete operations

Java Set

11 of 21

  • HashSet and HashMap are both created around hash tables.
  • A hash table essentially an array. Each item is inserted into the array according to a hash function. We will discuss this more in detail later in course.

0 1 2 3 4

What is a hash table?

12 of 21

© A+ Computer Science - www.apluscompsci.com

TreeSet is built on a binary tree.

A Binary Tree is a group of nodes that contain left and right references. Each item is inserted into the tree according to its relationship to the other nodes.

What is a binary tree?

13 of 21

Set

frequently used methods

Name

Use

boolean add(E e)

adds specified element in set if not already present and returns true - if this set already contains the element, the call leaves the set unchanged and returns false.

boolean remove(Object o)

removes element from the set if element is in the set and returns true - if this set did not contain the element, the call leaves the set unchanged and returns false.

void clear()

removes all elements from the set

int size()

returns the # of elements in the set (its cardinality)

14 of 21

Set<Integer> vals = new HashSet<Integer>();

vals.add(3);

System.out.println(vals.add(6));

System.out.println(vals.add(3));

vals.add(-5);

System.out.println(vals);

OUTPUT�

HashSet add()

15 of 21

Set<Integer> vals = new HashSet<Integer>();

vals.add(3);

System.out.println(vals.add(6));

System.out.println(vals.add(3));

vals.add(-5);

System.out.println(vals);

OUTPUT�true

false�[3, 6, -5]

HashSet add()

16 of 21

Set<String> vals = new TreeSet<String>();

vals.add("sci");

vals.add("comp");

vals.add("aplus");

vals.add("sci");

System.out.println(vals);

OUTPUT�

TreeSet add()

17 of 21

Set<String> vals = new TreeSet<String>();

vals.add("sci");

vals.add("comp");

vals.add("aplus");

vals.add("sci");

System.out.println(vals);

OUTPUT�[aplus, comp, sci]

TreeSet add()

18 of 21

Set<Double> vals = new HashSet<Double>();

vals.add( .3 );

vals.add( 1.2 );

vals.add( 2.6 );

System.out.println(vals);

System.out.println(vals.remove( 9.1 ));

System.out.println(vals.remove( 0.3 ));

System.out.println(vals);

OUTPUT�

HashSet remove()

19 of 21

Set<Double> vals = new HashSet<Double>();

vals.add( .3 );

vals.add( 1.2 );

vals.add( 2.6 );

System.out.println(vals);

vals.remove( 9.1 );

vals.remove( 0.3 );

System.out.println(vals);

OUTPUT�[2.6, 1.2, 0.3]

false

true�[2.6, 1.2]

HashSet remove()

20 of 21

Repl.it 06-01 Intro to Sets and 06-02 New Words

21 of 21

Homework

  • Repl.it 06-01 Intro to Sets and 06-02 New Words
  • goFormative - Sets