Serialization in A IS-A relationship

 

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

public class SerilizableISA {

public static void main(String[] args) {
System.out.println("==========Serilization Started============");
try {
//creating object of Student class
Student1 s1 = new Student1(123, "Vijay Dehraj", "Java", 50000);
//creating stream to write data into the file
FileOutputStream fos = new FileOutputStream("inheritance.txt");
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(s1);
//closing the stream
out.close();
System.out.println("The state of object is saved");
System.out.println("==========Serilization Ended==============");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("==========Deserilization Started==========");
try {
//creating input stream to read the data of serialized object
ObjectInputStream ois = new ObjectInputStream(
new FileInputStream("inheritance.txt")
);
Student1 s = (Student1) ois.readObject();
//printing data of serialized object
System.out.println(s.id + " " + s.name + " " + s.course + " " + s.fee);
System.out.println("==========Deserilization Started==========");
} catch (Exception e) {
System.out.println(e);
}
}
}

Transient

 

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

class TransientDemo {

public static void main(String[] args) {
try {
//creating object of class Girl;
Girl g1 = new Girl("Bhaviya", 2345, 29);
//creating stram to write data;
FileOutputStream fos = new FileOutputStream("girl.txt");
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(g1);
out.flush();
//closing stream
out.close();
fos.close();
System.out.println("Write Operation Successful");
} catch (Exception e) {
System.out.println(e);
}
//deserializaton.................
try {
//creating stream to read data;
ObjectInputStream ois = new ObjectInputStream(
new FileInputStream("girl.txt")
);
//reading data
Girl g1 = (Girl) ois.readObject();
//g1.age =0 as age is transient;
//printing the data of serilized object
System.out.println(g1.name + " " + g1.id + " " + g1.age);
ois.close();
} catch (Exception e) {
System.out.println(e);
}
}
}

Deserialization

 

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;

class DeserializationDemo {

public static void main(String[] args)
throws IOException, FileNotFoundException, ClassNotFoundException {
//creating stream to read the object
ObjectInputStream ois = new ObjectInputStream(
new FileInputStream("student.txt")
);
Student s = (Student) ois.readObject();

//printing the data of serialized object
System.out.println(s.id + " " + s.name);
//closing the stream
ois.close();
}
}

Serialization

 

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

class SerializationDemo {
public static void main(String[] args) {
try {
Student s1 = new Student(23, "Ramakesh");
FileOutputStream fout = new FileOutputStream("student.txt");
ObjectOutputStream out = new ObjectOutputStream(fout);
out.writeObject(s1);
out.flush();
// closing the stream
out.close();
System.out.println("State of object is successfully saved");
} catch (Exception e) {
e.printStackTrace();
}
}
}

The TreeMap and its Methods

 

import java.util.TreeMap;

public class TreeMapMethods {

public static void main(String[] args) {
//creating our tree map
TreeMap<Integer, String> tm = new TreeMap<>();
//Inserting element in our TreeMap
tm.put(22, "this");
tm.put(14, "Invent");
tm.put(24, "blog");
tm.put(20, "on");
tm.put(18, "you");
tm.put(16, "welcomes");
tm.put(12, "Java");

System.out.println("--------------------------------------");
//will give first lowest key in the TreeMap
System.out.println("The first key in the TreeMap");
System.out.println(tm.firstKey());
System.out.println("--------------------------------------");
//will give last/largest key in the TreeMap
System.out.println("The last key in the TreeMap");
System.out.println(tm.lastKey());
System.out.println("--------------------------------------");
//will give lowest key and value associated with it
System.out.println("The First Entry");
System.out.println(tm.firstEntry());
System.out.println("--------------------------------------");
//will give largest key and value associated with it
System.out.println("The last Entry");
System.out.println(tm.lastEntry());
System.out.println("--------------------------------------");
//will give value equal or less than the provided key
System.out.println("The floor entry is");
System.out.println(tm.floorEntry(15));
System.out.println("--------------------------------------");
//will give entry equal or more than the provided key
System.out.println("The Ceiling entry is");
System.out.println(tm.ceilingEntry(15));
System.out.println("--------------------------------------");
//will give key equal or more than the provided key
System.out.println("The ceiling key is");
System.out.println(tm.ceilingKey(15));
System.out.println("--------------------------------------");
//will give key equal or less than the provided key
System.out.println("The floor key is");
System.out.println(tm.floorKey(15));
System.out.println("--------------------------------------");
System.out.println("The head map is");
System.out.println(tm.headMap(16));
System.out.println("--------------------------------------");
System.out.println("The tail map is");
System.out.println(tm.tailMap(16));
System.out.println("--------------------------------------");
System.out.println("The hashcode of Our Map");
System.out.println(tm.hashCode());
System.out.println("--------------------------------------");
System.out.println("If map is empty or not?");
System.out.println(tm.isEmpty());
System.out.println("--------------------------------------");
System.out.println("The size of our Map is");
System.out.println(tm.size());
System.out.println("--------------------------------------");
System.out.println("The sorted Decending KeySet ");
System.out.println(tm.descendingKeySet());
System.out.println("--------------------------------------");
System.out.println("The decending Map ");
System.out.println(tm.descendingMap());
System.out.println("--------------------------------------");
System.out.println("The class of Tree Map");
System.out.println(tm.getClass());
System.out.println("--------------------------------------");
System.out.println("The higher entry than the key 16 is");
System.out.println(tm.higherEntry(16));
System.out.println("--------------------------------------");
System.out.println("The lower entry than the key 16 is");
System.out.println(tm.lowerEntry(16));
System.out.println("--------------------------------------");
System.out.println("The first entry of given map");
System.out.println(tm.pollFirstEntry());
System.out.println("--------------------------------------");
System.out.println("The last entry of given map");
System.out.println(tm.pollLastEntry());
System.out.println("--------------------------------------");
System.out.println("The submap of given TreeMap from key 14 to key 20");
System.out.println(tm.subMap(14, 20));
System.out.println("--------------------------------------");
System.out.println(tm.subMap(14, false, 22, false));
System.out.println(tm.subMap(14, true, 22, true));
System.out.println(tm.subMap(14, true, 22, false));
System.out.println(tm.subMap(14, false, 22, true));
System.out.println("--------------------------------------");
System.out.println("The Key Set of our Mapping is");
System.out.println(tm.navigableKeySet());
System.out.println("--------------------------------------");
}
}

Printing the mappings of a TreeMap using entrySet()

 

import java.util.TreeMap;

public class TreeMapEntrySet {

public static void main(String[] args) {
//creating our tree map
TreeMap<Integer, String> tm = new TreeMap<>();
//Inserting element in our TreeMap
tm.put(17, "this");
tm.put(13, "Invent");
tm.put(18, "blog");
tm.put(16, "on");
tm.put(15, "you");
tm.put(14, "welcomes");
tm.put(12, "Java");

System.out.println("Mappings of TreeMap are");
System.out.println(tm.entrySet());
}
}

Searching a Key and finding value associated with it in a TreeMap

 

import java.util.Scanner;
import java.util.TreeMap;

public class MapTreeContainKey {

public static void main(String[] args) {
//creating our tree map
TreeMap<Integer, String> tm = new TreeMap<>();
//Inserting element in our TreeMap
tm.put(17, "this");
tm.put(13, "Invent");
tm.put(18, "blog");
tm.put(16, "on");
tm.put(15, "you");
tm.put(14, "welcomes");
tm.put(12, "Java");

Scanner sc = new Scanner(System.in);
System.out.println("Enter the key you want to search");
Integer key = sc.nextInt();

if (tm.containsKey(key)) {
System.out.println("Key is present and value associated with it is : ");
System.out.println(tm.get(key));
} else {
System.out.println("key not found");
}
}
}

Cloning a TreeMap

 

import java.util.TreeMap;

public class TreeMapClone {

public static void main(String[] args) {
//creating our tree map
TreeMap<Integer, String> tm = new TreeMap<>();
//Inserting element in our TreeMap
tm.put(17, "this");
tm.put(13, "Invent");
tm.put(18, "blog");
tm.put(16, "on");
tm.put(15, "you");
tm.put(14, "welcomes");
tm.put(12, "Java");
System.out.println("The clone of TreeMap is");
System.out.println(tm.clone());
}
}

Removing element from a TreeMap


import java.util.TreeMap;

public class TreeMapRemove {

public static void main(String[] args) {
//creating our tree map
TreeMap<Integer, String> tm = new TreeMap<>();
//Inserting element in our TreeMap
tm.put(17, "this");
tm.put(13, "Invent");
tm.put(18, "blog");
tm.put(16, "on");
tm.put(15, "you");
tm.put(14, "welcomes");
tm.put(12, "Java");

//deleting element from TreeMap
System.out.println("Before invoking remove opeation the mapping is");
System.out.println(tm);
tm.remove(12);
System.out.println("After invoking remove opeation the mapping is");
System.out.println(tm);
tm.remove(14, "welcomes");
System.out.println("After invoking remove opeation the mapping is");
System.out.println(tm);
tm.clear();
System.out.println("After invoking remove opeation the mapping is");
System.out.println(tm);
}
}

 

Adding element to a TreeMap and iterating using iterator and printing its key-value set

 

import java.util.Iterator;
import java.util.TreeMap;

public class TreeMapPut {

public static void main(String[] args) {
//creating our tree map
TreeMap<Integer, String> tm = new TreeMap<>();
//Inserting element in our TreeMap
tm.put(17, "this");
tm.put(13, "Invent");
tm.put(18, "blog");
tm.put(16, "on");
tm.put(15, "you");
tm.put(14, "welcomes");
tm.put(12, "Java");

Iterator itr = tm.keySet().iterator();
while (itr.hasNext()) {
Integer key = (Integer) itr.next();
String value = tm.get(key);
System.out.println(key + "---->" + value);
}
}
}

Getting a value associated with a key using key in a LinkedHashMap

 

import java.util.LinkedHashMap;
import java.util.Scanner;

public class HashMapGet {

public static void main(String[] args) {
LinkedHashMap<Integer, String> lhm = new LinkedHashMap<>();
//adding element to our LinkedHashMap
lhm.put(21, "Amit");
lhm.put(5, "Rahul");
lhm.put(13, "Veeru");
lhm.put(34, "Subhash");
lhm.put(19, "Saahil");
System.out.println("Initial mapping is :");
System.out.println(lhm);
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Key to find associated value with it");
int n = sc.nextInt();
System.out.println("The value associated with the entered key is :");
System.out.println(lhm.get(n));
}
}

Deleting element from a LinkedHashMap

 

import java.util.LinkedHashMap;

public class MapRemove {

public static void main(String[] args) {
//creating LinkedHashMap
LinkedHashMap<Integer, String> lhm = new LinkedHashMap<>();
//adding element to our LinkedHashMap
lhm.put(21, "Amit");
lhm.put(5, "Rahul");
lhm.put(13, "Veeru");
lhm.put(34, "Subhash");
lhm.put(19, "Saahil");
//removing one element from out map
System.out.println("Before invoking remove() method,the mapping :");
System.out.println(lhm);
lhm.remove(21);
System.out.println();
System.out.println("After invoking remove() method,the mapping :");
System.out.println(lhm);
System.out.println();
lhm.remove(34, "Subhash");
System.out.println("After invoking remove() method again,the mapping :");
System.out.println(lhm);
System.out.println();
lhm.clear();
System.out.println("After invoking clear() method,the mapping :");
System.out.println(lhm);
}
}

Printing Keys, value, and Key-value Pair of a LinkedHashMap.


import java.util.LinkedHashMap;

public class LinkedHashMapPrint {

public static void main(String[] args) {
//creating LinkedHashMap
LinkedHashMap<Integer, String> lhm = new LinkedHashMap<>();
//adding element to our LinkedHashMap
lhm.put(21, "Amit");
lhm.put(5, "Rahul");
lhm.put(13, "Veeru");
lhm.put(34, "Subhash");
lhm.put(19, "Saahil");

System.out.println("Keys = " + lhm.keySet());
System.out.println("Values = " + lhm.values());
System.out.println("Key-value Pair Set = " + lhm.entrySet());
}
}

Creating a LinkedHashMap and adding elements to it and printing the elements

 

import java.util.LinkedHashMap;
import java.util.Map;

public class AddDemo {

public static void main(String[] args) {
//creating LinkedHashMap
LinkedHashMap<Integer, String> lhm = new LinkedHashMap<>();
//adding element to our LinkedHashMap
lhm.put(21, "Amit");
lhm.put(5, "Rahul");
lhm.put(13, "Veeru");
lhm.put(34, "Subhash");
lhm.put(19, "Saahil");

//printing values using for each loop
//insertion order is maintained in LinkedHashMap
for (Map.Entry m : lhm.entrySet()) {
System.out.println(
"key = " + m.getKey() + "," + "Value = " + m.getValue()
);
}
}
}

Getting Keys stored Hashtable using Enumeration

 

import java.util.Enumeration;
import java.util.Hashtable;

public class HashtableKeys {

public static void main(String[] args) {
Hashtable<Integer, String> ht = new Hashtable<>();
ht.put(31, "Vidya");
ht.put(12, "Sambhavna");
ht.put(33, "Tina");
ht.put(45, "Reena");
ht.put(51, "Anmol");
ht.put(16, "Veer");
ht.put(27, "Rekha");

Enumeration enu = ht.keys();
while (enu.hasMoreElements()) {
System.out.println(enu.nextElement());
}
}
}

Searching value in a Hashtable using containsValue() method

 

import java.util.Hashtable;

public class HashMapContainsValue {

public static void main(String[] args) {
Hashtable<Integer, String> ht = new Hashtable<>();
//Adding element to our Table ht
ht.put(31, "Vidya");
ht.put(12, "Sambhavna");
ht.put(33, "Tina");
ht.put(45, "Tina");
ht.put(51, "Anmol");
ht.put(16, "Veer");
ht.put(27, "Tina");

System.out.println("Is 'Tina' is present in the table ?");
System.out.println(ht.containsValue("Tina"));
}
}

Searching a value in Hashtable using contains() method

 

import java.util.Hashtable;

public class HashtableContains {

public static void main(String[] args) {
Hashtable<Integer, String> ht = new Hashtable<>();
//Adding element to our Table ht
ht.put(31, "Vidya");
ht.put(12, "Sambhavna");
ht.put(33, "Tina");
ht.put(45, "Gravit");
ht.put(51, "Anmol");
ht.put(16, "Veer");

System.out.println("Is 'Tina' is present in the table ?");
System.out.println(ht.contains("Tina"));
System.out.println("Is 'Veera' is present in the table ?");
System.out.println(ht.contains("Veera"));
}
}

Cloning a Hashtable Using in-built method Clone()

 

import java.util.Hashtable;

public class HashTableClone {

public static void main(String[] args) {
Hashtable<Integer, String> ht = new Hashtable<>();
//adding element to our Table
ht.put(31, "Vidya");
ht.put(12, "Sambhavna");
ht.put(33, "Tina");
ht.put(45, "Gravit");
ht.put(51, "Anmol");
ht.put(16, "Veer");
Hashtable<Integer, String> ht_clone = (Hashtable) ht.clone();
System.out.println("Original Table is :" + ht);
System.out.println("cloned Table is : " + ht_clone);
}
}

Removing or Deleting an element or removing all elements from a HashTable

 

import java.util.Hashtable;

public class HashTableRemove {

public static void main(String[] args) {
Hashtable<Integer, String> ht = new Hashtable<>();
ht.put(31, "Vidya");
ht.put(12, "Sambhavna");
ht.put(33, "Tina");
ht.put(45, "Gravit");
ht.put(51, "Anmol");
ht.put(16, "Veer");
//null value can not be added in HashTable
// ht.put(null, null);

//printing out Table
System.out.println("Original Our Table is : ");
System.out.println(ht);

//remvoing element from out Table
ht.remove(31);
System.out.println("Mapping of ht after first removal" + ht);
ht.remove(51, "Anmol");
System.out.println("Mapping of ht after Second removal" + ht);
    //removing all element from the table
ht.clear();
System.out.println("Mapping of ht after first removal" + ht);
}
}

Replacing element in a HashTable

 

import java.util.Hashtable;

public class HashTableReplaceElement {

public static void main(String[] args) {
Hashtable<Integer, String> ht = new Hashtable<>();
ht.put(31, "Vidya");
ht.put(12, "Sambhavna");
ht.put(33, "Tina");
ht.put(45, "Gravit");
ht.put(51, "Anmol");
ht.put(16, "Veer");
//replacing element
ht.put(51, "Shakira");
System.out.println("Mapping of ht is : ");
System.out.println(ht);
}
}

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...