Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2."— Presentation transcript:

1 CSS446 Spring 2014 Nan Wang

2  Java Collection Framework ◦ Set ◦ Map 2

3  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

4  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

5  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

6  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, HashSet >  HashSet (BankAccount should have equals() and hashCode() methods implemented) 6

7  HashSet, 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

8  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 names = new TreeSet ()  Set names = new HashSet () 8

9  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 10

11  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

12  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

13  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 14

15 15

16 16

17  A map keeps associations between key and value objects. 17

18  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 19

20  Enumerate all keys in a map.  Set name = scores.keySet(); for(String key: name){ Integer grade = scores.get(name); System.out.println(name + “->” + grade); } 20

21 21

22 22

23  Due date: March 18 th.  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


Download ppt "CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2."

Similar presentations


Ads by Google