CS 195 Course Outline & Introduction to Java

Download Report

Transcript CS 195 Course Outline & Introduction to Java

CSS446
Spring 2014
Nan Wang

Java Collection Framework
◦ Set
◦ Map
2


Set is an unordered collection of unique
elements.
Because a set does not track the order of the
elements, so the operations of finding,
adding and removing are more efficient.
3





The HashSet and TreeSet classes both
implement the Set interface.
A set does not admit duplicates. If you add an
element to a set that is already present, the
insertion is ignored.
HashSet --- Hash Table
TreeSet --- Binary Search Tree
Set implementations arrange the elements so
that they can locate them quickly.
4



Set elements are grouped into smaller
collections of elements that share the same
characteristic.
You can imagine a hash set of books as
having a group for each color, so that books
of the same color are in the same group. To
find whether a book is already present, you
just need to check it against the books in the
same color group.
Integer values (called hash codes) that can be
computed from the elements
5





Hash Table uses hash code which is computed
from the elements to find, add and remove
elements efficiently.
hashCode() method is used to compute the
integer values. (The class must have a proper
equals() implemented.)
You can form hash sets holding objects of type
String, Integer, Double, Point, Rectangle or
Color in standard library.
HashSet<Integer>, HashSet<HashSet<Integer>>
HashSet<BankAccount> (BankAccount should have
equals() and hashCode() methods implemented)
6


HashSet<Book>, you should provide
hashCode() and equals() methods for Book
Class.
Can I inherit the hashCode() and equals()
methods from Object Class?
◦ If all elements are distinct, you can inherit the
hashCode() and equals() of the Object class
7






TreeSet uses binary search tree for arrange its
elements, and elements are kept in sorted
order.
Elements are stored in nodes as in a linked
list which in a tree shape.
Use TreeSet for classes which implement
Comparable Interface (to compare which
node is larger)
If you want to visit the set’s element in sorted
order, you should choose a TreeSet.
Set<String> names = new TreeSet<String>()
Set<String> names = new HashSet<String>()
8



Sets don’t have duplicates. Adding a
duplicate of an element that is already
present is ignored.
Removing an element that is not in the set is
ignored too.
To use contains() method that you need
define the equals() method.
9
10



A set iterator visits the elements in the order
in which the set implementation keeps them.
In HashSet, elements are visited in random
order.
In TreeSet, elements are visited in sorted
order even you inserted them in different
order.
11




ListIterator has an add() method to add an
element at the list iterator position.
Iterator interface has no such method.
Removing element at iterator’s position for
both ListIerator and Iterator.
Iterator has no previous() method, but
ListIerator has.
12



Read all words from dictionary, and put them
in a set.
Read all words from a book, and put them in
other set.
Print the words that are not in dictionary,
which is potential misspelling.
13
14
15
16

A map keeps associations between key and
value objects.
17



A map allows you to associate elements from
a key set with elements from a value
collection.
You use a map when you want to look up
objects by using a key.
HashMap and TreeMap classes both
implements the Map Interface.
18
19


Enumerate all keys in a map.
Set<String> name = scores.keySet();
for(String key: name){
Integer grade = scores.get(name);
System.out.println(name + “->” + grade);
}
20
21
22



Due date: March 18th.
Create a program that reads a text file and
prints a list of all words in the file in
alphabetical order, together with a count that
indicates how often each word occurred in
the file.
What collection should you use for these
problem?
23