10 Event Of Hashtable Inwards Coffee – Coffee Hashtable Tutorial
These Java Hashtable Examples contains roughly of the oft used operations on Hashtable inward Java. when I discussed throw of How HashMap or Hashtable industrial plant inward Java I touched based on inner working of Hashtable, patch inward this Java Hashtable tutorial we volition run into roughly examples of hashtable inward Java like checking a substitution exits inward Hashmap or not or get all keys too values from HashMap , Iterating on hashtable keys too values using Enumeration, checking if Hashtable contains a item substitution or value, how to re-create mappings from a Hashtable to HashMap inward Java etc. Even though Hashtable is an erstwhile shape too a proficient direct chances that it volition live on deprectated inward future, y'all volition nevertheless uncovering lots of Hashtable based code inward legacy Java application, so, it's of import to know virtually this Map impelmentation. At the same time, produce non write whatever novel code using Hashtable instead purpose ConcurrentHashMap wherever possible.
Hashtable Examples inward Java
ConcurrentHashMap. You tin besides see Difference betwixt Hashtable too HashMap for comparison each other. Examples of the Hashtable are non real dissimilar than hashmap or ConcurrentHashMap too put() too get() almost travel same except roughly synchronization divergence e.g. on ConcurrentHashMap get() too put() tin overlap but on hashtable they can’t because the whole map gets locked on either operation. On the other hand, since hashmap is non synchronized at all using it on concurrent coffee application is non advised.
10 Java Hashtable Examples:
Now let’s deed on Hashtable examples inward Java next is a listing of all examples of hashtable performance nosotros volition run into inward this article:
1. How to pose an object into Hashtable?
2. How to recall an object from Hashtable inward Java?
3. How to reuse Hashtable yesteryear using clear()?
4. How to banking concern fit if Hashtable contains a item value?
5. How to banking concern fit if Hashtable contains a item key?
6. How to traverse Hashtable inward Java?
7. How to banking concern fit if Hashtable is empty inward Java?
8. How to Copy content of Hashtable into HashMap?
9. How to uncovering the size of Hashtable inward Java?
10. How to larn all values shape hashtable inward Java?
11. How to larn all keys from hashtable inward Java?
Here is consummate code event of all higher upwards hashtable event or exercises:
import java.util.Collection;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Set;
public class HashtableDemo {
public static void main(String args[]) {
// Creating Hashtable for example
Hashtable companies = new Hashtable();
// Java Hashtable event to pose object into Hashtable
// put(key, value) is used to insert object into map
companies.put("Google", "United States");
companies.put("Nokia", "Finland");
companies.put("Sony", "Japan");
// Java Hashtable event to larn Object from Hashtable
// get(key) method is used to recall Objects from Hashtable
companies.get("Google");
// Hashtable containsKey Example
// Use containsKey(Object) method to banking concern fit if an Object exits equally substitution in
// hashtable
System.out.println("Does hashtable contains Google equally key: "
+ companies.containsKey("Google"));
// Hashtable containsValue Example
// simply similar containsKey(), containsValue returns truthful if hashtable
// contains specified object equally value
System.out.println("Does hashtable contains Nihon equally value: "
+ companies.containsValue("Japan"));
// Hashtable enumeration Example
// hashtabl.elements() return enumeration of all hashtable values
Enumeration enumeration = companies.elements();
while (enumeration.hasMoreElements()) {
System.out
.println("hashtable values: " + enumeration.nextElement());
}
// How to banking concern fit if Hashtable is empty inward Java
// purpose isEmpty method of hashtable to banking concern fit emptiness of hashtable in
// Java
System.out.println("Is companies hashtable empty: "
+ companies.isEmpty());
// How to uncovering size of Hashtable inward Java
// purpose hashtable.size() method to uncovering size of hashtable inward Java
System.out.println("Size of hashtable inward Java: " + companies.size());
// How to larn all values shape hashtable inward Java
// y'all tin purpose keySet() method to larn a Set of all the keys of hashtable
// inward Java
Set hashtableKeys = companies.keySet();
// y'all tin besides larn enumeration of all keys yesteryear using method keys()
Enumeration hashtableKeysEnum = companies.keys();
// How to larn all keys from hashtable inward Java
// There are 2 ways to larn all values from hash table kickoff yesteryear using
// Enumeration too minute getting values advertizement Collection
Enumeration hashtableValuesEnum = companies.elements();
Collection hashtableValues = companies.values();
// Hashtable clear example
// yesteryear using clear() nosotros tin reuse an existing hashtable, it clears all
// mappings.
companies.clear();
}
}
Output:
Does hashtable contains Google equally key: true
Does hashtable contains Nihon equally value: true
hashtable values: Finland
hashtable values: United States
hashtable values: Japan
Is companies hashtable empty: false
Size of hashtable inward Java: 3
That’s all on Hashtable Example inward Java. We bring seen almost all oft used hashtable operations inward java. As I said before don’t purpose Hashtable equally its pretty erstwhile too in that place are amend alternatives available similar ConcurrentHashMap until it’s absolutely necessary for you.
Further Learning
Java In-Depth: Become a Complete Java Engineer
How to Convert Date to String inward Java
0 Response to "10 Event Of Hashtable Inwards Coffee – Coffee Hashtable Tutorial"
Post a Comment