0% found this document useful (0 votes)
64 views10 pages

Legacy Classes

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views10 pages

Legacy Classes

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Legacy Class in Java

In the past decade, the Collection framework didn't include in Java. In the
early version of Java, we have several classes and interfaces which allow us
to store objects. After adding the Collection framework in JSE 1.2, for
supporting the collections framework, these classes were re-engineered. So,
classes and interfaces that formed the collections framework in the older
version of Java are known as Legacy classes. For supporting generic in
JDK5, these classes were re-engineered.

All the legacy classes are synchronized. The java.util package defines the
following legacy classes:

1. HashTable

2. Stack

3. Dictionary

4. Properties

5. Vector

6. Vector
Vector Class
Vector is a special type of ArrayList that defines a dynamic array. ArrayList is
not synchronized while vector is synchronized. The vector class has several
legacy methods that are not present in the collection framework. Vector
implements Iterable after the release of JDK 5 that defines the vector is fully
compatible with collections, and vector elements can be iterated by the for-
each loop.

Vector class provides the following four constructors:

Vector()

It is used when we want to create a default vector having the initial size of
10.

2) Vector(int size)
It is used to create a vector of specified capacity. It accepts size as a
parameter to specify the initial capacity.

3) Vector(int size, int incr)

is used to create a vector of specified capacity. It accepts two parameters


size and increment parameters to specify the initial capacity and the number
of elements to allocate each time when a vector is resized for the addition of
objects.

4) Vector(Collection c)

It is used to create a vector with the same elements which are present in the
collection. It accepts the collection as a parameter.

VectorExample.java

1. import java.util.*;

2. public class VectorExample

3. {

4. public static void main(String[] args)

5. {

6. Vector<String> vec = new Vector<String>();

7. vec.add("Emma");

8. vec.add("Adele");

9. vec.add("Aria");

10. vec.add("Aidan");

11. vec.add("Adriana");

12. vec.add("Ally");

13. Enumeration<String> data = vec.elements();

14. while(data.hasMoreElements())

15. {

16. System.out.println(data.nextElement());

17. }

18. }
19. }

Hashtable Class
The Hashtable class is similar to HashMap. It also contains the data into
key/value pairs. It doesn't allow to enter any null key and value because it is
synchronized. Just like Vector, Hashtable also has the following four
constructors.

1) Hashtable()

It is used when we need to create a default HashTable having size 11.

2) Hashtable(int size)

It is used to create a HashTable of the specified size. It accepts size as a


parameter to specify the initial size of it.

3) Hashtable(int size, float fillratio)

It creates the Hashtable of the specified size and fillratio. It accepts two
parameters, size (of type int) and fillratio (of type float). The fillratio must be
between 0.0 and 1.0. The fillratio parameter determines how full the hash
table can be before it is resized upward. It means when we enter more
elements than its capacity or size than the Hashtable is expended by
multiplying its size with the fullratio.

Hashtable(Map< ? extends K, ? extends V> m) t is used to create a


Hashtable. The Hashtable is initialized with the elements present in m. The
capacity of the Hashtable is the twice the number elements present in m.

HashtableExample.java

1. import java.util.*;

2. class HashtableExample

3. {

4. public static void main(String args[])

5. {

6. Hashtable<Integer,String> student = new Hashtable<Integer, String>();

7. student.put(new Integer(101), "Emma");


8. student.put(new Integer(102), "Adele");

9. student.put(new Integer(103), "Aria");

10. student.put(new Integer(104), "Ally");

11. Set dataset = student.entrySet();

12. Iterator iterate = dataset.iterator();

13. while(iterate.hasNext())

14. {

15. Map.Entry map=(Map.Entry)iterate.next();

16. System.out.println(map.getKey()+" "+map.getValue());

17. }

18. }

19. }

Properties Class
Properties class extends Hashtable class to maintain the list of values. The
list has both the key and the value of type string. The Property class has
the following two constructors:

Properties()

It is used to create a Properties object without having default values.

2) Properties(Properties propdefault)

It is used to create the Properties object using the specified parameter of


properties type for its default value.

The main difference between the Hashtable and Properties class is that in
Hashtable, we cannot set a default value so that we can use it when no value
is associated with a certain key. But in the Properties class, we can set the
default value.

PropertiesExample.java

1. import java.util.*;

2. public class PropertiesExample

3. {
4. public static void main(String[] args)

5. {

6. Properties prop_data = new Properties();

7. prop_data.put("India", "Movies.");

8. prop_data.put("United State", "Nobel Laureates and Getting Killed by Lawnmo

wers.");

9. prop_data.put("Pakistan", "Field Hockey.");

10. prop_data.put("China", "CO2 Emissions and Renewable Energy.");

11. prop_data.put("Sri Lanka", "Cinnamon.");

12. Set< ?> set_data = prop_data.keySet();

13. for(Object obj: set_data)

14. {

15. System.out.println(obj+" is famous for "+ prop_data.getProperty((String)obj

));

16. }

17. }

18. }

Stack Class
Stack class extends Vector class, which follows the LIFO(LAST IN FIRST OUT)
principal for its elements. The stack implementation has only one default
constructor, i.e., Stack().

1) Stack()

It is used to create a stack without having any elements.

There are the following methods can be used with Stack class:

1. The push() method is used to add an object to the stack. It adds an element
at the top of the stack.

2. The pop() method is used to get or remove the top element of the stack.
3. The peek() method is similar to the pop() method, but it can't remove the
stack's top element using it.

4. The empty() method is used to check the emptiness of the stack. It returns
true when the stack has no elements in it.

5. The search() method is used to ensure whether the specified object exists
on the stack or not.

StackExample.java

1. import java.util.*;

2. class StackExample {

3. public static void main(String args[]) {

4. Stack stack = new Stack();

5. stack.push("Emma");

6. stack.push("Adele");

7. stack.push("Aria");

8. stack.push("Ally");

9. stack.push("Paul");

10. Enumeration enum1 = stack.elements();

11. while(enum1.hasMoreElements())

12. System.out.print(enum1.nextElement()+" ");

13. stack.pop();

14. stack.pop();

15. stack.pop();

16. System.out.println("\nAfter removing three elements from stack");

17. Enumeration enum2 = stack.elements();

18. while(enum2.hasMoreElements())

19. System.out.print(enum2.nextElement()+" ");

20. }

21. }
Dictionary Class
The Dictionary class operates much like Map and represents
the key/value storage repository. The Dictionary class is an abstract class
that stores the data into the key/value pair. We can define the dictionary as
a list of key/value pairs.

The dictionary class provides the following methods:

1. The put(K key, V value) method is used to add a key-value pair to the
dictionary.

2. The elements() method is used to get the value representation in the


dictionary.

3. The get(Object key) method is used to get the value mapped with the
argumented key in the dictionary.

4. The isEmpty() method is used to check whether the dictionary is empty or


not.

5. The keys() method is used to get the key representation in the dictionary.

6. The remove(Object key) method removes the data from the dictionary.

7. The size() method is used to get the size of the dictionary.

DictionaryExample.java

1. import java.util.*;

2. public class DictionaryExample

3. {

4. public static void main(String[] args)

5. {

6. // Initializing Dictionary object

7. Dictionary student = new Hashtable();

8.

9. // Using put() method to add elements


10. student.put("101", "Emma");

11. student.put("102", "Adele");

12. student.put("103", "Aria");

13. student.put("104", "Ally");

14. student.put("105", "Paul");

15.

16. //Using the elements() method

17. for (Enumeration enum1 = student.elements(); enum1.hasMoreElements();)

18. {

19. System.out.println("The data present in the dictionary : " + enum1.nextEl

ement());

20. }

21.

22. // Using the get() method

23. System.out.println("\nName of the student 101 : " + student.get("101"));

24. System.out.println("Name of the student 102 : " + student.get("102"));

25.

26. //Using the isEmpty() method

27. System.out.println("\n Is student dictionary empty? : " + student.isEmpty()

+ "\n");

28.

29. // Using the keys() method

30. for (Enumeration enum2 = student.keys(); enum2.hasMoreElements();)

31. {

32. System.out.println("Ids of students: " + enum2.nextElement());

33. }

34.

35. // Using the remove() method


36. System.out.println("\nDelete : " + student.remove("103"));

37. System.out.println("The name of the deleted student : " + student.get("123"

));

38.

39. System.out.println("\nThe size of the student dictionary is : " + student.size(

));

40.

41. }

42. }

You might also like