Searching Key and finding value associated with it in a HashTable

 

import java.util.Hashtable;
import java.util.Scanner;

public class FindSizeAndKey {

public static void main(String[] args) {
Hashtable<Integer, String> ht = new Hashtable<>();
//adding element to our HashTable
ht.put(1, "Vidya");
ht.put(2, "Sambhavna");
ht.put(3, "Tina");
ht.put(4, "Gravit");
ht.put(5, "Anmol");
ht.put(6, "Veer");
//finding size of our HashTable
System.out.println("Size of Table is = " + ht.size());
//checking if a key is present in the table or not
Scanner sc = new Scanner(System.in);
System.out.println("Enter the key you want to find");
int key = sc.nextInt();
if (ht.containsKey(key)) {
String value = ht.get(key);
System.out.println(
"The value stored at key " + key + " is --->>> " + value
);
}
}
}

Setting initial capacity and load factor of a HashTable

 

import java.util.Hashtable;

public class HashTableInitialCapacity {

public static void main(String[] args) {
//Creating a HashTable with Initial Capacity
//Default load factor is .75;
Hashtable<Integer, String> ht = new Hashtable<>(5);
//adding element to our HashTable
ht.put(1, "Vidya");
ht.put(2, "Sambhavna");
ht.put(3, "Tina");
ht.put(4, "Gravit");
ht.put(5, "Anmol");
ht.put(6, "Veer");
//finding size of our HashTable
System.out.println(ht.size());

Hashtable<Integer, String> ht1 = new Hashtable<>(4, .90f);
ht1.put(9, "Rehana");
ht1.put(10, "Ritik");
ht1.put(11, "Viresha");

System.out.println("Mapping of ht " + ht);
System.out.println("Mapping of ht1 " + ht1);
}
}

Adding Element to a HashTable

 

import java.util.Hashtable;
import java.util.Iterator;

public class HashTableDemo {

public static void main(String[] args) {
//creating hashTable
Hashtable<Integer, String> ht = new Hashtable<>();
//Adding element to our HashTable
ht.put(1, "Vidya");
ht.put(2, "Sambhavna");
ht.put(3, "Tina");
ht.put(4, "Gravit");
ht.put(5, "Anmol");

Iterator<Integer> itr = ht.keySet().iterator();
//cant perform any manupulation after taking iterator out
// ht.put(6, "Kiran");
while (itr.hasNext()) {
Integer key = itr.next();
String value = ht.get(key);
System.out.println("key = " + key + " " + "Value = " + value);
}
}
}

Finding size of a HashMap

 

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class HashMapSize {

public static void main(String[] args) {
//Creating HashMap;
Map<Integer, String> hm = new HashMap<>();
//Putting element
hm.put(1, "Naidu");
hm.put(3, "Rohilla");
hm.put(5, "Vijay");
hm.put(2, "Rahul");
hm.put(4, "Ramakesh");
hm.put(6, "Sudeep");
hm.putIfAbsent(7, "Badrabahu");

System.out.println(hm.size());
}
}

Combining elements of two HashMap using in-built method putAll()

 

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class HashMapPutAll {

public static void main(String[] args) {
//Creating HashMap;
Map<Integer, String> hm = new HashMap<>();
//Putting element
hm.put(1, "Naidu");
hm.put(3, "Rohilla");
hm.put(5, "Vijay");
hm.put(2, "Rahul");
hm.put(4, "Ramakesh");
hm.put(6, "Sudeep");
hm.putIfAbsent(7, "Badrabahu");

HashMap<Integer,String> hm1 = new HashMap<>();
//must value must not be same to use putAll() method
hm1.put(8,"language");
hm1.put(9,"java");
hm1.put(10,"bike");

hm.putAll(hm1);
System.out.println(hm);
}
}

Replacing element in a HashMap

 

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class HashMapReplace {

public static void main(String[] args) {
//Creating HashMap;
Map<Integer, String> hm = new HashMap<>();
//Putting element
hm.put(1, "Naidu");
hm.put(3, "Rohilla");
hm.put(5, "Vijay");
hm.put(2, "Rahul");
hm.put(4, "Ramakesh");
hm.put(6, "Sudeep");
//Storing null value
hm.put(null, null);

hm.replace(1, "Sourav");
//as key is not present in the map, no action will be performed
hm.replace(7, "Virender");
System.out.println(hm);
}
}

Deleting element from HashMap using built-in method remove()

 

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class HashMapRemove {

public static void main(String[] args) {
//Creating HashMap;
Map<Integer, String> hm = new HashMap<>();
//Putting element
hm.put(1, "Naidu");
hm.put(3, "Rohilla");
hm.put(5, "Vijay");
hm.put(2, "Rahul");
hm.put(4, "Ramakesh");
hm.put(6, "Sudeep");
//Storing null value
hm.put(null, null);

hm.remove(5);
System.out.println(hm);
hm.remove(6, "Sudeep");
System.out.println(hm);
//key and value doesn't match, no operation will be performed
hm.remove(4, "Rahul");
System.out.println(hm);
}
}

Getting specific element from HashMap Using built-in method get()

 

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class HashMapGetDemo {

public static void main(String[] args) {
//Creating HashMap;
Map<Integer, String> hm = new HashMap<>();
//Putting element
hm.put(1, "Naidu");
hm.put(3, "Rohilla");
hm.put(5, "Vijay");
hm.put(2, "Rahul");
hm.put(4, "Ramakesh");
hm.put(6, "Sudeep");
//Storing null value
hm.put(null, null);

System.out.println(hm.get(5));
System.out.println(hm.get("Sudeep")); //returns null
System.out.println(hm.get(4));
}
}

Iterating HashMap Using ForEach(action) loop


import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class HashMapIteration4 {

public static void main(String[] args) {
//Creating HashMap;
Map<Integer, String> hm = new HashMap<>();
//Putting element
hm.put(1, "Naidu");
hm.put(3, "Rohilla");
hm.put(5, "Vijay");
hm.put(2, "Rahul");
hm.put(4, "Ramakesh");
hm.put(6, "Sudeep");
//Storing null value
hm.put(null, null);
System.out.println("Iterating HashMap Using ForEach(action) loop");

hm.forEach(
(k, v) -> System.out.println("key = " + k + "," + "Value = " + v)
);
}
}

Iterating over hashmap using Iterator

 

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


public class HashMapIteration3 {

public static void main(String[] args) {
//Creating HashMap;
Map<Integer, String> hm = new HashMap<>();
//Putting element
hm.put(1, "Naidu");
hm.put(3, "Rohilla");
hm.put(5, "Vijay");
hm.put(2, "Rahul");
hm.put(4, "Ramakesh");
hm.put(6, "Sudeep");
//Storing null value
hm.put(null, null);
System.out.println("Iterating HashMap Using For each loop");

Iterator<Map.Entry<Integer,String>> itr = hm.entrySet().iterator();
while (itr.hasNext()) {
HashMap.Entry<Integer, String> entry = itr.next();
System.out.println(
"Key = " + entry.getKey() + " " + "Value = " + entry.getValue()
);
}
}
}

Google Script for Data Entry Form in Google Spreadsheet

// function to validate the entry made by user in user form function validateEntry (){ // declare a variable and referernece of active goog...