Java/J2EE and SOA (325 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (41 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (324 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

What are the Legacy Classes in Java?

Published on Oct 17,2019 165 Views
9 / 22 Blog from Java Collections

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

Java consists of several classes and interfaces to hold the objects before Java 1.2 version.  Before this version, there was no existence of Collection Framework. Legacy classes and interfaces are used to hold objects in that scenario. This article on Legacy class in Java will let you understand the concept in depth.detail.

Let us study about the Legacy class.

What are the Legacy Classes in Java? 

Earlier versions of Java did not include the Collections Framework. Only in from version 1.2, you could actually use this Legacy class. In this,  the original classes were reengineered to support the collection interface. These classes are also known as Legacy classes. All legacy classes and interfaces were redesigned by JDK 5 to support Generics

Dictionary

Dictionary is an abstract class. The main work is to hold the data as the key or value pair. It works in the form of Map Collection.

Properties

Properties class is a thread-safe i.e., multiple threads that can share single properties objects without external Synchronization. The set of properties in this class will be held in the key or value pair. Properties class extends the Hashtable class. Example:

package lc;

import java.util.Properties;
import java.util.Set;

public class Test {
public static void main(String[] args) {
Properties pr = new Properties();
pr.put("Joey", "Friends");
pr.put("Rachel", " Friends ");
pr.put("Phoebe", " Friends ");
pr.put("Chandler", " Friends ");
Set<?> creator = pr.keySet();
for (Object ob : creator) {
System.out.println(ob + " stars in " + pr.getProperty((String) ob));
}
}
}

Output:
Chandler stars in Friends
Phoebe stars in Friends
Rachel stars in Friends
Joey stars in Friends

Legacy Class in Java: HashTable

Hashtable is a part of Java.util package and it is a concrete class that extends the dictionary class. Hashtable is synchronized. From Java 1.2 framework onwards, hash table class implements the map interface and it is the part of the collection framework.

Example of Hashtable

import java.util.*;
class HashTableDemo
{
public static void main(String args[])
{
Hashtable<String,Integer>ht = new Hashtable<String,Integer>();
ht.put("a",new Integer(10));
ht.put("b",new Integer(20));
ht.put("c",new Integer(30));
ht.put("d",new Integer(40));

Set st = ht.entrySet();
Iterator itr=st.iterator();
while(itr.hasNext())
{
Map.Entry m=(Map.Entry)itr.next();
System.out.println(itr.getKey()+" "+itr.getValue());
}
}
}

Output:
10
20
30
40

Legacy Class in Java: Vector

Vector class is similar to the ArrayList class but there are certain differences. Vector is generally synchronized. It is used where the programmer doesn’t really have knowledge about the length of the Array.

Let us see some methods offered by this Vector method.

MethodDescription
E elementAt(int index)This method returns the element at the specified index
E firstElement()It helps to return the first element in the Vector
Enumeration elements()This helps to return an enumeration of the element in the vector
E lastElement()Returns the last element in the Vector
void removeAllElements()It helps in removing all the elements of the Vector

Example:

public class Test
{
public static void main(String[] args)
{
Vector ve = new Vector();
ve.add(1);
ve.add(2);
ve.add(3);
ve.add(4);
ve.add(5);
ve.add(6);
Enumeration en = ve.elements();
while(en.hasMoreElements())
{
System.out.println(en.nextElement());
}
}
}

Output:
1
2
3
4
5
6

Stack

Stack represents LIFO. Stack class extends the Vector class mentioned above.

class Stack{
public static void main(String args[]) {
Stack st = new Stack();
st.push(1);
st.push(2);
st.push(3);
st.push(4);
st.push(5);
Enumeration e1 = st.elements();
while(e1.hasMoreElements())
System.out.print(e1.nextElement()+" ");
st.pop();
st.pop();
System.out.println("nAfter popping out one element&rdquo;);
Enumeration e2 = st.elements();
while(e2.hasMoreElements())
System.out.print(e2.nextElement()+" ");
}
}

Output:
1 2 3 4 5
After popping out one element:
1 2 3 4

Now, let’s move on to the next segment that states the legacy interface.

Legacy Interface

Enumeration:

Enumeration interface is used to enumerate elements of Vector and all values of hashtable and properties of the properties class. Operations are cloned by the iterator interface and there are several more operations added like remove method and many more.

With this, we come to the end of this article on the “Legacy Class in Java”. I hope that the concept is clear to you by now. Keep reading, keep exploring!

Now that you’ve gone through this Legacy Class in Java article, Check out the Java Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

We are here to help you with every step on your journey, for becoming a besides this java interview questions, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer. 

Got a question for us? Please mention it in the comments section of this “Legacy Class in Java ”article and we will get back to you as soon as possible.

Comments
0 Comments

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.