12 hashtable tủ tài liệu bách khoa

47 52 0
12   hashtable tủ tài liệu bách khoa

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Trần Thị Thanh Nga ngattt@hcmuaf.edu.vn Khoa Công nghệ thông tin, ĐH Nông Lâm HCM HashTable Agenda  Hash function in Java  Hash table  Collinsions  Java Collection HashTable Introduction  Consider the problem of searching an array for a given value  If the array is not sorted, the search might require examining each and all elements of the array  If the array is sorted, we can use the binary search, and therefore reduce the worse-case runtime complexity to O(log n)  We could search even faster if we know in advance the index at which that value is located in the array HashTable Hash function  Suppose we have that magic function that would tell us the index for a given value  With this magic function our search is reduced to just one probe, giving us a constant runtime O(1) Such a function is called a hash function  A hash function is a function which when given a key, generates an address in the table HashTable Hash function example  The example of a hash function is a book call number  Each book in the library has a unique call number  A call number is like an address: it tells us where the book is located in the library HashTable Hash function  A hash function that returns a unique hash number is called a universal hash function  Hash function has the following properties:  it always returns a number for an object  two equal objects will always have the same number  two unequal objects not always have different numbers HashTable Hash function  The precedure of storing objects using a hash function is the following  Create an array of size M  Choose a hash function h, that is a mapping from objects into integers 0, 1, , M-1  Put these objects into an array at indexes computed via the hash function index = h(object)  Such array is called a hash table HashTable Hash function HashTable How to choose a hash function?  One approach of creating a hash function is to use Java's hashCode() method  The hashCode() method is implemented in the Object class and therefore each class in Java inherits it  The hash code provides a numeric representation of an object HashTable Hash function Integer obj1 = new Integer(2009); String obj2 = new String("2009"); System.out.println("hashCode for an integer is " + obj1.hashCode()); System.out.println("hashCode for a string is " + obj2.hashCode()); HashTable 10 HashMap Example String[] data = new String("Nothing is as easy as it looks").split(" "); HashMap hm = new HashMap(); for (String key : data) { Integer freq = hm.get(key); if (freq == null) freq = 1; else freq++; hm.put(key, freq); } HashTable 33 HashMap Example System.out.println(hm); for (Map.Entry m : hm.entrySet()) { System.out.println(m.getKey() + " " + m.getValue()); } HashTable 34 Anagrams solver  An anagram is a word or phrase formed by reordering the letters of another word or phrase For example:  barde, ardeb, bread, debar, beard, bared bears, saber, bares, baser, braes, sabre  In this program you read a dictionary from the web site at http://www.andrew.cmu.edu/course/15121/dictionary.txt  Build a Map( ):  whose key is a sorted word (meaning that its characters are sorted in alphabetical order)  and whose values are the word's anagrams HashTable 35 Anagrams solver File file = new File("dictionary.txt"); Scanner sc = new Scanner(file); HashMap map = new HashMap(); int i = 0; while (sc.hasNextLine()) { String word = sc.nextLine(); String sortedWord = sortString(word); // this is a key ArrayList anagrams = map.get(sortedWord); // this is a value HashTable 36 Anagrams solver if (anagrams == null) anagrams = new ArrayList(); anagrams.add(word); map.put(sortedWord, anagrams); } // end while System.out.println(map.get( sortString(“bread"))); // testing } HashTable 37 Anagrams solver private static String sortString(String w) { char[] ch = w.toCharArray(); Arrays.sort(ch); return new String(ch); } HashTable 38 Java Collections  Map Interface  Entry  HashMap  LinkedHashMap  HashTable  HashMap and Hashtable HashTable 39 LinkedHashMap  A LinkedHashMap contains values based on the key  It implements the Map interface and extends HashMap class  It contains only unique elements  It may have one null key and multiple null values  It is same as HashMap instead maintains insertion order HashTable 40 LinkedHashMap HashTable 41 Example LinkedHashMap hm = new LinkedHashMap(); hm.put(100, "Amit"); hm.put(102, "Rahul"); hm.put(101, "Vijay"); for (Map.Entry m : hm.entrySet()) { System.out.println(m.getKey() + " " + m.getValue()); } HashTable 42 Java Collections  Map Interface  Entry  HashMap  LinkedHashMap  HashTable  HashMap and Hashtable HashTable 43 Java Hashtable class  A Hashtable is an array of list  Each list is known as a bucket  The position of bucket is identified by calling the hashcode() method  A Hashtable contains values based on the key  It implements the Map interface and extends Dictionary class  It contains only unique elements  It may have not have any null key or value  It is synchronized HashTable 44 Difference between HashMap and Hashtable  HashMap and Hashtable both are used to store data in key and value form  Both are using hashing technique to store unique keys HashMap Hashtable HashMap allows one null key and Hashtable doesn't allow multiple null values any null key or value HashMap is a new class introduced in JDK 1.2 Hashtable is a legacy class HashMap is fast Hashtable is slow HashTable 45 Difference between HashMap and Hashtable HashMap Hashtable HashMap allows one null key and Hashtable doesn't allow multiple null values any null key or value HashMap is a new class introduced in JDK 1.2 Hashtable is a legacy class HashMap is fast Hashtable is slow HashTable 46 Question? HashTable 47 ... !dict.contains(token.toLowerCase())) System.out.println(token); HashTable 22 Java Collections  Map Interface  HashMap  LinkedHashMap  HashTable  HashMap and Hashtable HashTable 23 The Map interface  A map contains... Object getValue(): is used to obtain value HashTable 27 Java Collections  Map Interface  Entry  HashMap  LinkedHashMap  HashTable  HashMap and Hashtable HashTable 28 Java HashMap class  A HashMap... s.charAt(n-1) where s is a string and n is its length  Example "ABC" = 'A' * 312 + 'B' * 31 + 'C' = 65 * 312 + 66 * 31 + 67 = 64578 HashTable 11 Integer obj1 = new Integer(2009); System.out.println("hashCode

Ngày đăng: 09/11/2019, 07:23

Tài liệu cùng người dùng

Tài liệu liên quan