Thursday, November 22, 2018
Home > java > Legacy Collection Framework in java

Legacy Collection Framework in java

Legacy Collection Framework

Legacy Collection Framework is Renamed form of existing data structure of JAVA collection framework.

What is the difference between New collection framework and Legacy collection framework


New collection frameworkLegacy collection framework.
1) classes and interfaces are not synchronized .1) classes and interfaces are synchronized.
2) Lagecy collection framework does not provide thread safety.2) Lagecy collection framework provide thread safety.

Lagecy collection framework classes and interfaces are recommended to use in JSE application(standalone application,GUI application and two tier application) and and not recommended to use in new collection framework because all the application run in server side software and there is a chance of locking multiple times and unlocking multiple Times.

New collection framework classes and interfaces are recommended to use in JEE application because these classes and interfaces are belongs to non-synchronized and not provide thread safety. If we use them in JEE application they are running the content of server and server software will provide thread safety .

new collection framework classes and interfaces are recommended to use in JEE application.

Like a new collection framework Legacy collection framework contains some predefined classes and interfaces. They are present in java.util.*.Legacy collection framework also contains one dimensional and two-dimensional collection framework.

The following are the interfaces and classes used in the Legacy collection framework.


1) Enumeration
2) Vector
3) Stack
4) Dictionary
5) HashTable
6)Properties

Enumeration


1) It is one of the predefined interface present in java.util.* package.
2) The purpose of the Enumeration interface object is to retrieve the data from any Legacy collection framework variable.
3) the functionality of Enumeration is more/less similar to Iterator Interface but Enumeration object belongs to synchronized and Iterator interface object belongs to non-synchronised.
4) like the iterator interface object Enumeration interface object also pointing just before the first element of any legacy collection framework variable.

Method

1) public boolean hasMoreElement()
2) public object nextElement()

Method1 return true when Enumeration interface object contains next element otherwise it returns false.
Method2 use for obtaining nest element of any Legacy collection framework variable if method1 must return true.

Vector


1) It is one of the predefined classes present in java.util.* package.
2) In Vector the data is organized in the form of a cell. Cell value is stored in heap memory and cell address are stored in associative memory.
3) Creating a Vector is nothing but creating an object of Vector class.
Example: Vector v=new Vector();
4) Here the capacity of Vectors(number of cells) is 10. By default size of the vector is 0 (number of value present in cells).
5) The functionality of the vector is more or less similar to the ArrayList but Vector belongs to synchronize and ArrayList belongs to non-synchronised.

Profile of Vector classes

Constructor

1)Vector()
2) Vector(int)

Instance method

1) public int capacity()
2) public int size()
3) public object elementAt(int)
4) public void removeElementAt(int)
5) public void removeElement(Object)
6) public void addElement(Object)
7) public Enumeration elements()

Method7 is used for retrieving the element from Vector class object and forms a forward direction chain. this method returned an object of Enumeration and it is by default pointing just before the first element.

Example:

System.out.println(v);
Enumeration en=v.elements();
while(en.hasMoreElement())
{
Object obj=en.hasNextElement();
-----------------
-----------------
}

Write a Java Program which illustrate the concept of Vector

 

Stack


1) Stack is for a subclass of the vector so that all the methods of the vector are inherited into stack class.
2) Creating a Stack is nothing but creating an object of Stack class.
Example: Stack s=new Stack();
3) the real-time implementation of Stack are following below.
a) converting infix expression into either prefix or postfix.
b) evaluation of recursive method class.
c) evaluation of ordinary method calls.
4) The memory management of stack depends on the working principle of a stack.
5) the working principle of the Stack is LIFO(last in first out).

Profile of java.util.Stack

Constructor
1) Stack(): Constructor is used for creating an object of a Stack.

Instance methods
1) public boolean empty()
2) public void push(Object)
3) public Object pop()
4) public Object peek()
5) public int search(Object)

Method1 and 2 returns if Stack is empty otherwise return false.
Method3 is used for inserting an element into the stack.
Method4 used for removing the topmost element from the stack.
Method5 is used for extracting the topmost element.
Method6 is used for searching an element from the Stack if the element is found in the Stack then it returns Stack relative position of the elements otherwise returns -1.

Write a Java Program which will illustrate the operation of stack.

Dictionary


1) which one of the two dimensional Legacy collection framework predefined abstract class present in java.util.* package.
2) Dictionary class object organizes the data in the form of a key-value pair.
3) Since Dictionary class is an abstract class and we cannot create an object of an abstract class directly so that there is no way of using this class in our application development but we always use implementation class of Dictionary.

Profile of Dictionary

Instance methods

1) public int size()
2) public boolean isEmpty()
3) public void put(Object,Object)
4) public Object get(Object)
5) public void remove(Object)
6) public Enumeration keys()

Method6 is used for retrieving the keys from any two dimensional Legacy collection framework variable and forms forward directional chain.This method6 return an object of Enumeration and it is by default.

System.out.println(D); //{_,_,_,}
Enumeration en=D.keys();
while(en.hasNextElement())
{
Object kobj=en.nextElement();
Object vobj=D.get(kobj);
System.out.println(vobj+kobj);
}

Hashtable


1) It is one of the subclass of the Dictionary so that all the methods of Dictionary are inherited into Hashtable and overridden.
2) Hashtable class object organizes the data in the form of the key-value pair by following the hashing mechanism.
3) since Hashtable is the following hashing mechanism so that we cannot determine in which order has HashTable class object displays the data that’s mean undetermined form.
4) the functionality of Hashtable is more or less similar to HashMap but as table class object belongs to synchronize and never allows the null value for key and value pair whereas HashMap class object belongs to non-synchronised and it allows the null value for key and value pair.

Profile of Hashtable

Constructor

1) Hashtable()
2) Hashtable(int)

Constructor1 is used for creating an object of Hashtable without specifying the size.
Example: Hashtable ht=new Hashtable();
Constructor 2 is used for creating an object of Hashtable with size.
Example: Hashtable ht=new Hashtable(5);

Write a Java Program.

Limitation of Hashtable

1) Hashtable class object is unable to read the data from properties or resource bundle file.
2) Hashtable class object is unable to read environmental variable names and their value(path, classpath,username…).
3) Hashtable class subject is unable to develop flexible application.
To overcome the limitation Hashtable class, we have another pre-define class called java.util.Properties.

Property file


1) A property file is one of the text file to be created in any text editor and it should be saved with some file name with an extension either .prop/.rbj.
2) Properties file organize data in the form of key-value pair.
3) Property file always decided secondary memory.
4) In order to read the data from properties file by using Properties class, property file must be open in read mode with the help of FileInputStream class.

Example:

FileInputStream fis=new FileInputStream(“student.prop”);
student.prop reside in secondary memory.

java.util.Properties


1) Properties class is one of the subclass of Hashtable.
2) Creating a Properties is nothing but creating an object of Properties class.
Example: Properties p=new Properties();

Advantage Properties class over Hashtable class

1) Properties class object is able to read the data from a properties file.
2) Properties class object is able to read environment variable names and their values.
3)With Properties class object we are able to develop flexible applications.

Profile of Properties class

Constructor

1) Properties()

It is used for creating object properties class.
Example: Properties p=new Properties();

Instance method

1) public void load()
2) public String getProperty(String)

Method1 is used for loading/transfer the content of properties file in a Properties class object by opening the properties file in read mode with the help of FileInputStream class.
Example:

FileInputStream fis=new FileInputStream("student.prop"); 
p.load(fis);

Method2 is using for reading the properties value by passing the property name.
Example:

String sno=p.getProperty("stno"); 
String name=p.getProperty("name");

Step/guideline for reading the data from a property file.

1) creating a property file in any text editor and save it on the filename with an extension either .prop/.rbj.
2) open the properties file in read mode with the help of FileInputStream class.
3) create an object of Properties class.
4) load/transfer the properties file data into Properties class object.
5) Read the data from Properties class object and display on the console.
6) close the Properties file

Admin
We are group of application developer and admin.We are providing good content of information about IT technology and services .

Leave a Reply

Your email address will not be published. Required fields are marked *