HashMap :
This class provides all of the map operations, and allows null values and the null key.
It is unsynchronized.
Order is not maintained.
- It has to parameters: initial capacity and load factor.
- The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created.
- The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased.
- When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is rehashed (that is, internal data structures are rebuilt) so that the hash table has approximately twice the number of buckets.
There are following construtor to create HashMap
HashMap()
HashMap(int initialCapacity)
HashMap(int initialCapacity, float loadFactor)
HashMap(Map m)
package collection.demos;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class HashMapDemo {
public static void main(String[] args) {
Map<String,Long> phoneNumbers=new HashMap<String,Long>();
//Putting values in HashMap
phoneNumbers.put("Abc", 12345674L);
phoneNumbers.put("xyz", 453453344L);
phoneNumbers.put("Shoye", 23423433L);
phoneNumbers.put("Jusd", 656565653L);
phoneNumbers.put("Korp", 775554343L);
System.out.println("Values : "+phoneNumbers);
//getting value for specific key
System.out.println("Value of key xyz : "+phoneNumbers.get("xyz"));
//checking for specific key
System.out.println("There is a value of key xyz "+phoneNumbers.containsKey("xyz"));
//checking for specific Object
System.out.println("There is a value 453453344L "+phoneNumbers.containsValue(453453344L));
//getting collection of values
Collection<Long> phones=phoneNumbers.values();
System.out.println("Collection values : "+phones);
System.out.println("Size of HashMap : "+phoneNumbers.size());
//Removing the element associated with specified key
phoneNumbers.remove("Jusd");
System.out.println("Values after removed : "+phoneNumbers);
//Getting set of keys
Set<String> keys=phoneNumbers.keySet();
System.out.println("Set of Keys : "+keys);
//Getting entryset of mappings
Set<Map.Entry<String,Long>> entryset=phoneNumbers.entrySet();
System.out.println("Entry set : "+entryset);
//Removing all entries
phoneNumbers.clear();
System.out.println("Values after cleared : "+phoneNumbers);
}
}
No comments:
Post a Comment