To do: November 15
Copy and paste WhereAmI code into:
Repl.it lab 05-07 setting up for file I/O.
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
Go over homework
Open your WhereAmI.java program. How did we do?
The following are important
interfaces included in the
Java language ::
Collection
Set
Map
Java Interfaces
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
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
The Set interface extends the Collection
interface and HashSet and TreeSet implements the Set interface.
Set
HashSet
SortedSet
TreeSet
Set Interface
A set is a group of items all of the same type of which none are duplicates.
What is a set?
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
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
| | | | |
0 1 2 3 4
What is a hash table?
© 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?
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) |
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()
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()
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()
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()
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()
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()
Repl.it 06-01 Intro to Sets and 06-02 New Words
Homework