Collections

HashTable methods Java Code With Examples

Table of Contents

Program

import java.util.Collections;
import java.util.Hashtable;
import java.util.Map.Entry;
public class HashTableExample {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Hashtable<Integer,String> map=new Hashtable<Integer,String>();  
		 
                  
		  map.put(100,"Amit");  
		  map.put(102,"Ravi");  
		  map.put(101,"Vijay");  
		  map.put(103,"Rahul");  
		  System.out.println("only mapping");

                  //Map.Entry<K,​V>
		  for(Entry<Integer, String> m:map.entrySet())
		  {  
			  
		   System.out.println(m.getKey()+" "+m.getValue());  
		  }  
		 Hashtable<Integer,String> hm=new Hashtable<Integer,String>();  
		  
		  hm.put(100,"Amit");  
		  hm.put(102,"Ravi");  
		  hm.put(101,"Vijay");  
		  hm.put(103,"Rahul");  
		  System.out.println("with hash map");
		  for(Entry<Integer, String> m:hm.entrySet())
		  {  
		   
		   System.out.println(m.getKey()+" "+m.getValue()); 
		  } 
		   
		  System.out.println("Before remove: "+ map);    
	          
                  //remove​(Object key, Object value)
	          map.remove(102);  
	          System.out.println("After remove: "+ map); 
	             
	          
                   //getOrDefault​(Object key, V defaultValue)
                   System.out.println(map.getOrDefault(101, "Not Found"));  
	          System.out.println(map.getOrDefault(105, "Not Found")); 
	             
                  //putIfAbsent​(K key, V value)
	               map.putIfAbsent(104,"Gaurav");  
	          System.out.println("Updated Map: "+map);  
	               map.putIfAbsent(105,"Guru");  
	          System.out.println("Updated Map: "+map); 
	             
	               map.putIfAbsent(101,"Vijay");  
	          System.out.println("Updated Map: "+map);  
		 }  
  
   }

Output

     only mapping
	103 Rahul
	102 Ravi
	101 Vijay
	100 Amit
     with hash map
	103 Rahul
	102 Ravi
	101 Vijay
	100 Amit
     Before remove: {103=Rahul, 102=Ravi, 101=Vijay, 100=Amit}
     After remove: {103=Rahul, 101=Vijay, 100=Amit}
     Vijay
     Not Found
     Updated Map: {104=Gaurav, 103=Rahul, 101=Vijay, 100=Amit}
     Updated Map: {105=Guru, 104=Gaurav, 103=Rahul, 101=Vijay, 100=Amit}
     Updated Map: {105=Guru, 104=Gaurav, 103=Rahul, 101=Vijay, 100=Amit} 

Description

public static interface Map.Entry

A map entry(key-value pair). The Map.entrySet method returns a collection-view of the map,
whose elements are of this class. The only way to obtain a reference to a map entry is from the literator of this collection-view. These Map. Entry objects are valid only for the duration of the iteration; more formally, the behavior of a map entry is undefined if the backing map has been modified after the entry was returned by the iterator, except through the setValue operation on the map entry

Since:

1.2

See Also:

Map.entrySet()

default boolean remove​(Object key, Object value)

Removes the entry for the specified key only if it is currently mapped to the specified value.
Implementation Requirements:The default implementation is equivalent to, for this map:
if (map.containsKey(key)&& Objects.equals(map.get(key), value)) {map.remove(key);return true;)
else
return false;

The default implementation makes no guarantees about the synchronization or atomicity properties of this method.
Any implementation providing atomicity guarantees must override this method and document its concurrency properties

Parameters:

key – key with which the specified value is associated
value – value expected to be associated with the specified key

Returns:

true if this value was removed

Throws:

UnsupportedOpreationException – if the remove operation is not supported by this map (optional)
ClassCastException – if the key or value is of an inappropriate type for 6this map (optional)
NullPointerException – if the specified key or value is null, and this map does not permit null keys or values (optional)

Since:

1.8

default V getOrDefault​(Object key, V defaultValue)

Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.

Implementation Requirements:

The default implementation makes no guarantees about the synchronization or atomicity properties of this method.Any implementation providing atomicity guarantees must override this method and document its concurrency properties.

Parameters:

key – the key whose associated value is to be returned
defaultValue – the default mapping of the key

Returns:

the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key

Throws:

ClassCastException – if the key is of an inappropriate type for this map (optional)
NullPointerException – if the specified key is null and this map does not permit null keys (optional)

Since:

1.8

default V putIfAbsent​(K key, V value)

If the specified key is not already associated with a value (or is mapped to null) associates it with the given value and returns null, else returns the current value.

Implementation Requirements:

The default implementation is equivalent to, for this map:
 V v = map.get(key);
 		if (v == null)
    		 v = map.put(key, value);
		return v;

The default implementation makes no guarantees about the synchronization or atomicity properties of this method.
Any implementation providing atomicity guarantees must override this method and document its concurrency properties.
Parameters:key – key with which the specified value is to be associated value – value to be associated with the specified key

Returns:

the previous value associated with the specified key, or null if there was no mapping for the key. (A null return can also indicate that the map
previously associated null with the key, if the implementation supports null values.)

Throws:

UnsupportedOperationException – if the put operation is not supported by this map (optional)
ClassCastException – if the key or value is of an inappropriate type for this map (optional)
NullPointerException – if the specified key or value is null, and this map does not permit null keys or values (optional)
IllegalArgumentException – if some property of the specified key or value prevents it from being stored in this map (optional)

Since:

1.8