SlideShare a Scribd company logo
1 of 19
HASHTABLE
J.SHIRISHA
WHAT IS HASHTABLE??
 It is similar to Hashmap.
 Hashtable is an implementation of a key-value pair
data structure in java(where key and value are
objects).
 Hashtable inherits dictionary class and implements
Map interface.
 It is synchronized.
 It is found in java.util.Hashtable package.
DECLARATION OF HASHTABLE
java.util.Hashtable class
HASHTABLE CLASS DECLARATION:
HASHTABLE CLASS PARAMETERS:
Let's see the Parameters for java.util.Hashtable class.
K: It is the type of keys maintained by this map.
V: It is the type of mapped values.
public class Hashtable<k,V> extends Dictionary<K,V>
implements Map<K,V>, Cloneable, Serializable
IMPORTANT POINTS ABOUT 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 contains unique elements.
 It may have not have any null key or value.
 It is synchronized.
 Hashtable is a legacy class(old version).
 Hashtable is traversed by Enumerator and Iterator.
 Hashtable inherits Dictionary class.
WORKING OF HASHTABLE:
Working of Hashtable depends on various parameters. Initial
capacity, load factor, size and Threshold value are the parameters
which affect Hashtable performance.
Initial capacity: This is the capacity of Hashtable to store number
of key value pairs when it is instantiated. Default capacity is 11.
Load Factor: A parameter responsible to determine when to
increase size of Hashtable. Default load factor is 0.75.
Size: Number of key value pairs in Hashtable.
Threshold value: When number of key value pairs is more than
threshold value, then Hashtable is resized
CONSTRUCTOR AND DESCRIPTION:
Hashtable(): This is the default constructor to the Hashtable it
instantiates the Hashtable class.
Hashtable(int size): This constructor accepts an integer
parameter an dcreates a Hashtable the has an initial size specified
by the integer value size.
Hashtable(int size, float fillRatio): This creates a Hashtable that
has an initial size specified by size and a fill ratio specified by
fillRatio. This ratio must be between 0.0 and 1.0, and it
determines how full the Hashtable can be before it is resized
upward.
Hashtable(Map < ? extends k, ? extends v > t): This constructs
a Hashtable with the given mappings.
METHODS AND DESCRIPTION:
void clear(): Resets and empties the Hashtable.
object clone(): Returns a duplicate of the invoking
object.
boolean contains(object value): returns true if some value
equal to the value exists within the Hashtable. Returns false
if the value isn’t found.
boolean containsKey(object key): Returns true if some
key equal to the key exists within the Hashtable. Returns
false if the key isn’t found.
boolean containsValue(object value): Returns true if some
value equl to the exists within the hashtable. Returns false if the
value isn’t found.
Enumeration elements(): Returns an enumeration of the values
contained in the Hashtable.object get(object key): Returns the
object that contains the value associated with the key. If the key is
not in the Hashtable, a null object is returned.
boolean isEmpty(): Returns true if the Hashtable is empty;
returns false if it contains at least one key.
object put(object key, object value): Inserts a key and a value
into the Hashtable. Returns null if the key isn’t already in the
Hashtable; returns the previous value associated with the key if
the key is already in the hash table.
Enumeration keys(): Returns an enumeration of the keys
contains in the Hashtable.
void rehash(): Increase the size of the Hashtable and
rehashes all of its keys.
object remove(object key): Removes the key and its
value. Returns the value associated with the key. If the key
is not in the Hashtable, a null object is returned.
int size(): Returns the number of entries in the Hashtable.
String toString(): Returns the string equivalent of a
Hashtable
HASHMAP HASHTABLE
It is not synchronized. It is synchronized.
It allows one null key and many number of
null values.
It does not allow null keys or null values.
HashMap is fast as it is not synchronizes. Hashtable is slow as it is synchronized.
HashMap is a new class introduced in JDK
1.2.
Hashtable is a legacy class(old version).
We can make the HashMap as synchronized
by calling this code
Map m =Collections.synchronizedMap
(hashMap);
Hashtable is internally synchronized and can't
be unsynchronized.
HashMap is traversed by Iterator Hashtable is traversed by Enumerator and
Iterator.
Iterator in HashMap is fail-fast. Enumerator in Hashtable is not fail-fast
HashMap inherits AbstractMap class. Hashtable inherits Dictionary class
ADVANTAGES OF HASHTABLE:
 Hashtable may have not have any null key or value.
 Hashtable is an implementation of a key-value pair data
structure in java. You can store and retrieve a ‘value’
using a ‘key’ and it is an identifier stored. It is obvious
that the ‘key’ should be unique.
 They are widely used in many kinds of computer
software, particularly for associative arrays, database
indexing, caches and sets.
 You will be limited by available memory.
 Hashtable becomes quite inefficient when there are many
collisions.
 Hash collisions are practically unavoidable. When
hashing a random subset of a large set of possible keys.
 Hashtable does not allow null values, like hash map
COLLISION IN HASHTABLE:
 When you pass a key/value to the Hashtable , it queries the
key's hashcode.
 The Hashtable uses that code to determine the bucket in
which to place the key/value.
 In Java, the Hashtable responds to a collision by placing
multiple values into the same bucket (other implementations
may handle collisions differently).
COLLISION HANDLING IN HASHTABLE:
SEPERATE CHAINING:
EXAMPLE PROGRAM TO DEMONSTRATE HASHTABLE:
import java.util.Hashtable;
import java.util.Enumeration;
public class HashtableExample
{
public static void main(String[] args)
{
Enumeration names;
String key;
// Creating a Hashtable
Hashtable<String, String> hashtable = new Hashtable<String, String>();
//Adding key and value pairs to Hashtable
hashtable.put ( “key1”, “apple” );
hashtable.put( “key2”, “banana” );
hashtable.put( “key3”, “mango” );
hashtable.put( “key4”, “grapes” );
hashtable.put( “key5”, “orange” );
names=hashtable.keys();
while(names.hasMoreElements())
Key=(String) names.nextElement();
System.out.println(“key:”+key+“&value:”+hashtable.get(key));
}
}
}
OUTPUT:
Key:key5 & value: orange
Key:key4 & value: grapes
Key:key3 & value: mango
Key:key2 & value:banana
Key:key1 & value: apple
Hash table in java

More Related Content

What's hot

collection framework in java
collection framework in javacollection framework in java
collection framework in javaMANOJ KUMAR
 
How Hashmap works internally in java
How Hashmap works internally  in javaHow Hashmap works internally  in java
How Hashmap works internally in javaRamakrishna Joshi
 
Java - Collections framework
Java - Collections frameworkJava - Collections framework
Java - Collections frameworkRiccardo Cardin
 
This keyword and final keyword
This keyword and final  keywordThis keyword and final  keyword
This keyword and final keywordkinjalbirare
 
Collections - Maps
Collections - Maps Collections - Maps
Collections - Maps Hitesh-Java
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentationguest11106b
 
Wrapper class (130240116056)
Wrapper class (130240116056)Wrapper class (130240116056)
Wrapper class (130240116056)Akshay soni
 
5 collection framework
5 collection framework5 collection framework
5 collection frameworkMinal Maniar
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVAVINOTH R
 
Finalize() method
Finalize() methodFinalize() method
Finalize() methodJadavsejal
 
Java Collections
Java CollectionsJava Collections
Java Collectionsparag
 
Cookies in servlet
Cookies in servletCookies in servlet
Cookies in servletchauhankapil
 
Hibernate Tutorial
Hibernate TutorialHibernate Tutorial
Hibernate TutorialRam132
 
Collections - Lists, Sets
Collections - Lists, Sets Collections - Lists, Sets
Collections - Lists, Sets Hitesh-Java
 

What's hot (20)

JDBC
JDBCJDBC
JDBC
 
Java annotations
Java annotationsJava annotations
Java annotations
 
collection framework in java
collection framework in javacollection framework in java
collection framework in java
 
How Hashmap works internally in java
How Hashmap works internally  in javaHow Hashmap works internally  in java
How Hashmap works internally in java
 
Java collections
Java collectionsJava collections
Java collections
 
Java - Collections framework
Java - Collections frameworkJava - Collections framework
Java - Collections framework
 
This keyword and final keyword
This keyword and final  keywordThis keyword and final  keyword
This keyword and final keyword
 
Collections - Maps
Collections - Maps Collections - Maps
Collections - Maps
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentation
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
Wrapper class (130240116056)
Wrapper class (130240116056)Wrapper class (130240116056)
Wrapper class (130240116056)
 
5 collection framework
5 collection framework5 collection framework
5 collection framework
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
 
Finalize() method
Finalize() methodFinalize() method
Finalize() method
 
Java Collections
Java CollectionsJava Collections
Java Collections
 
Cookies in servlet
Cookies in servletCookies in servlet
Cookies in servlet
 
Java collections notes
Java collections notesJava collections notes
Java collections notes
 
sets and maps
 sets and maps sets and maps
sets and maps
 
Hibernate Tutorial
Hibernate TutorialHibernate Tutorial
Hibernate Tutorial
 
Collections - Lists, Sets
Collections - Lists, Sets Collections - Lists, Sets
Collections - Lists, Sets
 

Similar to Hash table in java

Assignment4 Assignment 4 Hashtables In this assignment we w.pdf
Assignment4 Assignment 4 Hashtables In this assignment we w.pdfAssignment4 Assignment 4 Hashtables In this assignment we w.pdf
Assignment4 Assignment 4 Hashtables In this assignment we w.pdfkksrivastava1
 
Data Structure and Algorithms: What is Hash Table ppt
Data Structure and Algorithms: What is Hash Table pptData Structure and Algorithms: What is Hash Table ppt
Data Structure and Algorithms: What is Hash Table pptJUSTFUN40
 
Reference Guide to Java - Day4
Reference Guide to Java - Day4Reference Guide to Java - Day4
Reference Guide to Java - Day4apextgi0204
 
Data structures and algorithms lab11
Data structures and algorithms lab11Data structures and algorithms lab11
Data structures and algorithms lab11Bianca Teşilă
 
Java collections-interview-questions
Java collections-interview-questionsJava collections-interview-questions
Java collections-interview-questionsyearninginjava
 
JAVA Collections frame work ppt
 JAVA Collections frame work ppt JAVA Collections frame work ppt
JAVA Collections frame work pptRanjith Alappadan
 
Complete code in Java The hashtable you'll be making will use String.pdf
Complete code in Java   The hashtable you'll be making will use String.pdfComplete code in Java   The hashtable you'll be making will use String.pdf
Complete code in Java The hashtable you'll be making will use String.pdfaarifi9988
 
The hashtable youll be making will use Strings as the keys and Obje.pdf
The hashtable youll be making will use Strings as the keys and Obje.pdfThe hashtable youll be making will use Strings as the keys and Obje.pdf
The hashtable youll be making will use Strings as the keys and Obje.pdfvicky309441
 
Chapter 10: hashing data structure
Chapter 10:  hashing data structureChapter 10:  hashing data structure
Chapter 10: hashing data structureMahmoud Alfarra
 
Collection in Java
Collection in JavaCollection in Java
Collection in JavaHome
 
Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptxAgonySingh
 

Similar to Hash table in java (20)

Assignment4 Assignment 4 Hashtables In this assignment we w.pdf
Assignment4 Assignment 4 Hashtables In this assignment we w.pdfAssignment4 Assignment 4 Hashtables In this assignment we w.pdf
Assignment4 Assignment 4 Hashtables In this assignment we w.pdf
 
Java.util
Java.utilJava.util
Java.util
 
Data Structure and Algorithms: What is Hash Table ppt
Data Structure and Algorithms: What is Hash Table pptData Structure and Algorithms: What is Hash Table ppt
Data Structure and Algorithms: What is Hash Table ppt
 
Reference Guide to Java - Day4
Reference Guide to Java - Day4Reference Guide to Java - Day4
Reference Guide to Java - Day4
 
Data structures and algorithms lab11
Data structures and algorithms lab11Data structures and algorithms lab11
Data structures and algorithms lab11
 
Java collections-interview-questions
Java collections-interview-questionsJava collections-interview-questions
Java collections-interview-questions
 
JAVA Collections frame work ppt
 JAVA Collections frame work ppt JAVA Collections frame work ppt
JAVA Collections frame work ppt
 
DS THEORY 35.pptx
DS THEORY 35.pptxDS THEORY 35.pptx
DS THEORY 35.pptx
 
linear probing
linear probinglinear probing
linear probing
 
16 ruby hashes
16 ruby hashes16 ruby hashes
16 ruby hashes
 
Java util
Java utilJava util
Java util
 
Array list(1)
Array list(1)Array list(1)
Array list(1)
 
Java Collections.pptx
Java Collections.pptxJava Collections.pptx
Java Collections.pptx
 
Complete code in Java The hashtable you'll be making will use String.pdf
Complete code in Java   The hashtable you'll be making will use String.pdfComplete code in Java   The hashtable you'll be making will use String.pdf
Complete code in Java The hashtable you'll be making will use String.pdf
 
The hashtable youll be making will use Strings as the keys and Obje.pdf
The hashtable youll be making will use Strings as the keys and Obje.pdfThe hashtable youll be making will use Strings as the keys and Obje.pdf
The hashtable youll be making will use Strings as the keys and Obje.pdf
 
Chapter 10: hashing data structure
Chapter 10:  hashing data structureChapter 10:  hashing data structure
Chapter 10: hashing data structure
 
Collection in Java
Collection in JavaCollection in Java
Collection in Java
 
Hash
HashHash
Hash
 
Lecture notesmap
Lecture notesmapLecture notesmap
Lecture notesmap
 
Presentation.pptx
Presentation.pptxPresentation.pptx
Presentation.pptx
 

Recently uploaded

HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 

Recently uploaded (20)

HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 

Hash table in java

  • 1.
  • 3.
  • 4. WHAT IS HASHTABLE??  It is similar to Hashmap.  Hashtable is an implementation of a key-value pair data structure in java(where key and value are objects).  Hashtable inherits dictionary class and implements Map interface.  It is synchronized.  It is found in java.util.Hashtable package.
  • 5. DECLARATION OF HASHTABLE java.util.Hashtable class HASHTABLE CLASS DECLARATION: HASHTABLE CLASS PARAMETERS: Let's see the Parameters for java.util.Hashtable class. K: It is the type of keys maintained by this map. V: It is the type of mapped values. public class Hashtable<k,V> extends Dictionary<K,V> implements Map<K,V>, Cloneable, Serializable
  • 6. IMPORTANT POINTS ABOUT 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 contains unique elements.  It may have not have any null key or value.  It is synchronized.  Hashtable is a legacy class(old version).  Hashtable is traversed by Enumerator and Iterator.  Hashtable inherits Dictionary class.
  • 7. WORKING OF HASHTABLE: Working of Hashtable depends on various parameters. Initial capacity, load factor, size and Threshold value are the parameters which affect Hashtable performance. Initial capacity: This is the capacity of Hashtable to store number of key value pairs when it is instantiated. Default capacity is 11. Load Factor: A parameter responsible to determine when to increase size of Hashtable. Default load factor is 0.75. Size: Number of key value pairs in Hashtable. Threshold value: When number of key value pairs is more than threshold value, then Hashtable is resized
  • 8. CONSTRUCTOR AND DESCRIPTION: Hashtable(): This is the default constructor to the Hashtable it instantiates the Hashtable class. Hashtable(int size): This constructor accepts an integer parameter an dcreates a Hashtable the has an initial size specified by the integer value size. Hashtable(int size, float fillRatio): This creates a Hashtable that has an initial size specified by size and a fill ratio specified by fillRatio. This ratio must be between 0.0 and 1.0, and it determines how full the Hashtable can be before it is resized upward. Hashtable(Map < ? extends k, ? extends v > t): This constructs a Hashtable with the given mappings.
  • 9. METHODS AND DESCRIPTION: void clear(): Resets and empties the Hashtable. object clone(): Returns a duplicate of the invoking object. boolean contains(object value): returns true if some value equal to the value exists within the Hashtable. Returns false if the value isn’t found. boolean containsKey(object key): Returns true if some key equal to the key exists within the Hashtable. Returns false if the key isn’t found.
  • 10. boolean containsValue(object value): Returns true if some value equl to the exists within the hashtable. Returns false if the value isn’t found. Enumeration elements(): Returns an enumeration of the values contained in the Hashtable.object get(object key): Returns the object that contains the value associated with the key. If the key is not in the Hashtable, a null object is returned. boolean isEmpty(): Returns true if the Hashtable is empty; returns false if it contains at least one key. object put(object key, object value): Inserts a key and a value into the Hashtable. Returns null if the key isn’t already in the Hashtable; returns the previous value associated with the key if the key is already in the hash table.
  • 11. Enumeration keys(): Returns an enumeration of the keys contains in the Hashtable. void rehash(): Increase the size of the Hashtable and rehashes all of its keys. object remove(object key): Removes the key and its value. Returns the value associated with the key. If the key is not in the Hashtable, a null object is returned. int size(): Returns the number of entries in the Hashtable. String toString(): Returns the string equivalent of a Hashtable
  • 12. HASHMAP HASHTABLE It is not synchronized. It is synchronized. It allows one null key and many number of null values. It does not allow null keys or null values. HashMap is fast as it is not synchronizes. Hashtable is slow as it is synchronized. HashMap is a new class introduced in JDK 1.2. Hashtable is a legacy class(old version). We can make the HashMap as synchronized by calling this code Map m =Collections.synchronizedMap (hashMap); Hashtable is internally synchronized and can't be unsynchronized. HashMap is traversed by Iterator Hashtable is traversed by Enumerator and Iterator. Iterator in HashMap is fail-fast. Enumerator in Hashtable is not fail-fast HashMap inherits AbstractMap class. Hashtable inherits Dictionary class
  • 13. ADVANTAGES OF HASHTABLE:  Hashtable may have not have any null key or value.  Hashtable is an implementation of a key-value pair data structure in java. You can store and retrieve a ‘value’ using a ‘key’ and it is an identifier stored. It is obvious that the ‘key’ should be unique.  They are widely used in many kinds of computer software, particularly for associative arrays, database indexing, caches and sets.
  • 14.  You will be limited by available memory.  Hashtable becomes quite inefficient when there are many collisions.  Hash collisions are practically unavoidable. When hashing a random subset of a large set of possible keys.  Hashtable does not allow null values, like hash map
  • 15. COLLISION IN HASHTABLE:  When you pass a key/value to the Hashtable , it queries the key's hashcode.  The Hashtable uses that code to determine the bucket in which to place the key/value.  In Java, the Hashtable responds to a collision by placing multiple values into the same bucket (other implementations may handle collisions differently).
  • 16. COLLISION HANDLING IN HASHTABLE: SEPERATE CHAINING:
  • 17. EXAMPLE PROGRAM TO DEMONSTRATE HASHTABLE: import java.util.Hashtable; import java.util.Enumeration; public class HashtableExample { public static void main(String[] args) { Enumeration names; String key; // Creating a Hashtable Hashtable<String, String> hashtable = new Hashtable<String, String>(); //Adding key and value pairs to Hashtable hashtable.put ( “key1”, “apple” ); hashtable.put( “key2”, “banana” ); hashtable.put( “key3”, “mango” ); hashtable.put( “key4”, “grapes” ); hashtable.put( “key5”, “orange” ); names=hashtable.keys();
  • 18. while(names.hasMoreElements()) Key=(String) names.nextElement(); System.out.println(“key:”+key+“&value:”+hashtable.get(key)); } } } OUTPUT: Key:key5 & value: orange Key:key4 & value: grapes Key:key3 & value: mango Key:key2 & value:banana Key:key1 & value: apple