Top Banner
CSS446 Spring 2014 Nan Wang
23

CSS446 Spring 2014 Nan Wang. Java Collection Framework ◦ Set ◦ Map 2.

Jan 02, 2016

Download

Documents

Gregory Wilkins
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.

CSS446Spring 2014

Nan Wang

Page 2: CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.

Java Collection Framework◦ Set◦ Map

2

Page 3: 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

Page 4: CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.

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

Page 5: CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.

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

Page 6: CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.

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

Page 7: CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.

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

Page 8: CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.

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

Page 9: CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.

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

Page 10: CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.

10

Page 11: CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.

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

Page 12: CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.

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

Page 13: CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.

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

Page 14: CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.

14

Page 15: CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.

15

Page 16: CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.

16

Page 17: CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.

A map keeps associations between key and value objects.

17

Page 18: CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.

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

Page 19: CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.

19

Page 20: CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.

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

Page 21: CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.

21

Page 22: CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.

22

Page 23: CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.

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