View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

Legacy classes in Java: What are they & Why Do They Matter?

By Rohan Vats

Updated on Nov 14, 2022 | 6 min read | 7.0k views

Share:

Java, in its earliest versions, didn’t come with the Collection framework. Instead, it relied on defining specific classes and interfaces to store objects and their assigned methods. It wasn’t until J2SE 1.2 that the collection interface was introduced, which is commonly known as legacy classes in Java. Since then, these classes have been engineered to work correctly with Generics, and given below are some of the widespread usage of legacy classes:

  • Dictionary: An abstract class in nature assigned to hold values as the value pair.
  • Properties: Thread-safe by nature, it doesn’t require any manual sync to hold values in the key pair, and is an extension to Hashtable class.
  • HashTable: It is a large part of the java.util package that is an extension to the dictionary class. In-sync with the Java1.2 framework, it works on the execution of the map interface, being a package of the collection framework.
  • Vector: Identical to the ArrayList, except for a few stark differences, this class is mainly used by programmers who aren’t sure about the array’s length. 
  • Stack: Commonly referred to as LIFO, it is an extension to the vector class.

Apart from the legacy classes, there is only one legacy interface in Java called the Enumeration. 

Check out our free courses to get an edge over the competition.

The legacy classes in Java are what was called as the ad hoc classes that used to contain values and object groups. Java 2 used to be missing a unified framework called the collections framework before it was introduced in Java 2. The objectives of such a framework are:

  • It needs to be a high-performing interface with several features like linked lists, trees, dynamic arrays, and more to remain hugely efficient. 
  • It would also enable different collection frameworks to work in sync with its superior interoperability. 

In general, a collections framework stands for combined infrastructure built for the representation and operating methods and variables. Developed around a predefined set of standard interfaces like LinkedList, HashSet, and more, these frameworks can be entirely used as per its presets or customized accordingly.

Check out upGrad’s Advanced Certification in Cyber Security 

Read more: A Guide to Iterator Implementation in Java

Collection Interfaces: Enumeration Interface and legacy classes in Java

The Enumeration: It is a legacy interface by nature; it can enumerate several values into a collection of objects and is followed by an Iterator. 

The Enumeration interface works similarly to that of its superseding Iterator as it obtains values one at a time. This method is also invoked by the legacy classes in Java-like that of the Vector and Properties for carrying out a few ways. Here are some characteristics of an Enumeration interface:

Check out upGrad’s Advanced Certification in Cloud Computing  

  • Any class that works on the execution of the Enumeration interface would gain values in a long series, yielding one value at a time.
  • Superseded by the Iterator, the enumeration processes only get copied by the Iterator. It also includes a few more methods like remove() and more in newer enforcing of the enumeration interface.

The methods followed by the Enumeration interface are:

  • boolean hasMoreElements(): This function returns 1, if more values are remaining for extraction, and returns 0 if all the values have been enumerated. 
  • Object nextElement(): This function returns the upcoming value in the enumeration process, and sets off the NoSuchElementException in the case of no more objects. 

Read: Top 6 Reasons Why Java Is So Popular With Developers

Vector Class Constructors and Legacy Methods

  • Vector(): By-default vector creation with a predefined size of 10.
  • Vector(int size): Creation of a vector with the initial capacity based upon its size.
  • Vector(int size, int incr): Making a vector with its initial capacity specified by size and increment specified by incr. Here, the increment function designates respective elements every time the vector gets resized and added by objects.
  • Vector (Collection C): Creation of a vector that consists of elements from the collection C

The Legacy Methods include:

  • void addElement(E element): It helps in adding up of element into a vector
  • E elementAt(int index): It returns the value at predefined index
  • Enumeration elements(): This returns an item, obtained one-by-one in Vector
  • E firstElement(): It returns only the first element in Vector
  • E last element (): It returns only the final component of the vector.
  • void removeAllElements(): It can remove all the elements of a vector.

Coverage of AWS, Microsoft Azure and GCP services

Certification8 Months
View Program

Job-Linked Program

Bootcamp36 Weeks
View Program

HashTable and HashMap: Key Differences 

HashTable HashMap
This class is synchronized. This class isn’t in sync.
Thread-safe and takes more time to work Faster in execution
The key or the values can’t be null.  Both the key and the values can be invalid.
The table order remains constant through time.  The map may or may not remain consistent throughout.

That was all about legacy classes in Java, we hope you found that useful!

Also read: Java Developer Salary in India: For Freshers & Experienced

Conclusion

If you’re interested to learn more about full-stack software development, check out upGrad & IIIT-B’s PG Diploma in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.

Frequently Asked Questions (FAQs)

1. What is the average salary of a Java Developer in India?

2. What is the difference between the Local variable and the Instance variable?

3. What is the difference between Collection and Collections?

Rohan Vats

408 articles published

Get Free Consultation

+91

By submitting, I accept the T&C and
Privacy Policy

India’s #1 Tech University

Executive PG Certification in AI-Powered Full Stack Development

77%

seats filled

View Program

Top Resources

Recommended Programs

upGrad

AWS | upGrad KnowledgeHut

AWS Certified Solutions Architect - Associate Training (SAA-C03)

69 Cloud Lab Simulations

Certification

32-Hr Training by Dustin Brimberry

View Program
upGrad KnowledgeHut

upGrad KnowledgeHut

Angular Training

Hone Skills with Live Projects

Certification

13+ Hrs Instructor-Led Sessions

View Program
upGrad

upGrad KnowledgeHut

Full Stack Development Bootcamp - Essential

Job-Linked Program

Bootcamp

36 Weeks

View Program