java.util.HashMap Class

advertisement
http://www.hudatutorials.com
Download link: http://www.hudatutorials.com/ht/java/util/hashmap.html
java.util.HashMap Class
The HashMap is the most commonly used implementation of the Map interface. It
provides a basic key−value map where the elements are unordered. If you need to
maintain map keys in an ordered fashion, that's where the TreeMap comes in handy.
The default initial capacity of the internal data structure is 16 and the default load factor
is 0.75. Unlike a Hashtable, both the key and the value for a HashMap can be null. If the
key happens to already be in the map, the old value is replaced and returned. Otherwise,
null is returned. The map uses the key's hash code to determine where to store the
key−value pair internally.
Similar to the other collection classes, the toString() returned value will be a
comma−delimited list of the collection elements within braces ({}). For the HashMap,
each key−value element is displayed separated by an equal sign. The listed order does
not reflect the order in which the elements are added to the HashMap. Instead, the order
reflects the range conversion of the hash codes generated from the keys. Removing all
elements from a map does not return the space used by the internal data structure. The
capacity of the structure remains the same. Only the entries of the structure are nulled
out.
HashMap class has following constructors.
HashMap()
Constructs an empty HashMap with the default initial capacity (16) and the default load
factor (0.75).
HashMap(int initialCapacity)
Constructs an empty HashMap with the specified initial capacity and the default load
factor (0.75).
HashMap(int initialCapacity, float loadFactor)
Constructs an empty HashMap with the specified initial capacity and load factor.
HashMap(Map<? extends K,? extends V> m)
Constructs a new HashMap with the same mappings as the specified Map.
/* java.util.HashMap class Example */
/* Save with file name HashMapExample.java */
import java.util.HashMap;
import java.util.Enumeration;
public class HashMapExample
{
public static void main(String args[])
{
//java.util.HashMap DECLARATION
HashMap<String,Integer> h;
//java.util.HashMap OBJECT CREATION
//USING DEFAULT CONSTRUCTOR
h = new HashMap<String,Integer>();
1
http://www.hudatutorials.com
Download link: http://www.hudatutorials.com/ht/java/util/hashmap.html
//ADD KEY AND VALUE
h.put("ONE", new Integer(1));
h.put("TWO", new Integer(2));
h.put("THREE", new Integer(3));
//ALLOW null KEY AND VALUE
h.put(null,null);
//HashMap OUTPUT
System.out.println(h);
}
}
/* java.util.HashMap class Example 2 */
/* Save with file name HashMapExample2.java */
import
import
import
import
import
java.util.HashMap;
java.util.Iterator;
java.util.Set;
java.util.Collection;
java.util.Iterator;
public class HashMapExample2
{
public static void main(String args[])
{
//java.util.HashMap DECLARATION
HashMap<String,Integer> h;
//java.util.HashMap OBJECT CREATION
//USING DEFAULT CONSTRUCTOR
h = new HashMap<String,Integer>();
//ADD KEY AND VALUE
h.put("ONE", new Integer(1));
h.put("TWO", new Integer(2));
h.put("THREE", new Integer(3));
//ALLOW null KEY AND VALUE
h.put(null,null);
//HashMap KEYS OUTPUT
Set s = h.keySet();
Iterator itr = s.iterator();
int i=1;
while(itr.hasNext())
{
System.out.println("Key " + i++ + " : " + itr.next());
}
//HashMap VALUES OUTPUT
Collection c = h.values();
Iterator values = c.iterator();
int j=1;
while(values.hasNext())
2
http://www.hudatutorials.com
Download link: http://www.hudatutorials.com/ht/java/util/hashmap.html
{
System.out.println("Value " + j++ + " : " + values.next());
}
}
}
/* java.util.HashMap class Example 3 */
/* Save with file name HashMapExample3.java */
import
import
import
import
import
import
java.util.HashMap;
java.util.Map;
java.util.Iterator;
java.util.Set;
java.util.Collection;
java.util.Iterator;
public class HashMapExample3
{
public static void main(String args[])
{
//java.util.HashMap DECLARATION
HashMap<String,Integer> h;
//java.util.HashMap OBJECT CREATION
//USING DEFAULT CONSTRUCTOR
h = new HashMap<String,Integer>();
//ADD KEY AND VALUE
h.put("ONE", new Integer(1));
h.put("TWO", new Integer(2));
h.put("THREE", new Integer(3));
//ALLOW null KEY AND VALUE
h.put(null,null);
//HashMap KEYS OUTPUT
Set s = h.entrySet();
Iterator itr = s.iterator();
int i=1;
while(itr.hasNext())
{
//Map.Entry IS INNER INTERFACE OF Map INTERFACE
Map.Entry entry = (Map.Entry) itr.next();
System.out.println(entry.getKey()+" "+entry.getValue());
}
}
}
/* java.util.HashMap class Example 4*/
/* Save with file name HashMapExample4.java */
import java.util.HashMap;
import java.util.Enumeration;
public class HashMapExample4
{
3
http://www.hudatutorials.com
Download link: http://www.hudatutorials.com/ht/java/util/hashmap.html
public static void main(String args[])
{
//java.util.HashMap DECLARATION
HashMap<String,Integer> h;
//java.util.HashMap OBJECT CREATION
//USING DEFAULT CONSTRUCTOR
h = new HashMap<String,Integer>();
//ADD AN ELEMENTS
h.put("ONE", new Integer(1));
h.put("TWO", new Integer(2));
h.put("THREE", new Integer(3));
System.out.println("isEmpty : " + h.isEmpty());
//RETURNS THE NUMBER OF KEYS IN THIS HashMap
System.out.println("noof Keys : " + h.size());
System.out.println("Key ONE value : " + h.get("ONE"));
System.out.println("Contains key THREE : " + h.containsKey("THREE"));
System.out.println("Contains value 2 : " + h.containsValue(new Integer(2)));
}
}
The following example shows how to save HashMap into file.
/* java.util.HashMap class Example 5 */
/* Save with file name HashMapExample5.java */
import java.util.HashMap;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
public class HashMapExample5
{
public static void main(String args[])
{
try
{
//java.util.HashMap DECLARATION
HashMap<String,Integer> h;
//java.util.HashMap OBJECT CREATION
//USING DEFAULT CONSTRUCTOR
h = new HashMap<String,Integer>();
//ADD AN ELEMENTS
h.put("ONE", new Integer(1));
h.put("TWO", new Integer(2));
h.put("THREE", new Integer(3));
//FileOutputStream CREATION
FileOutputStream fos = new FileOutputStream("hashmap.set");
4
http://www.hudatutorials.com
Download link: http://www.hudatutorials.com/ht/java/util/hashmap.html
//ObjectOutputStream CREATION
ObjectOutputStream oos = new ObjectOutputStream(fos);
//WRITE Set OBJECT TO ObjectOutputStream
oos.writeObject(h);
//CLOSE THE ObjectOutputStream
oos.close();
System.out.println("HashMap Saved into File Sucessfully");
}
catch(Exception e)
{
System.out.println("Error Occurred : " + e.getMessage());
}
}
}
The following example shows how to retrieve HashMap from file.
/* java.util.HashMap class Example 6 */
/* Save with file name HashMapExample6.java */
import java.util.HashMap;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
public class HashMapExample6
{
public static void main(String args[])
{
try
{
//java.util.HashMap DECLARATION
HashMap<String,Integer> h;
//FileInputStream CREATION
FileInputStream fis = new FileInputStream("hashmap.set");
//ObjectInputStream CREATION
ObjectInputStream ois = new ObjectInputStream(fis);
//READ HashMap OBJECT FROM ObjectInputStream
h = (HashMap) ois.readObject();
ois.close();
System.out.println(h);
}
catch(Exception e)
{
System.out.println("Error Occurred : " + e.getMessage());
}
}
}
5
Download