Vector Class

The Vector class implements a growable array of objects. Vector implements a dynamic array that means it can grow or shrink as required. Like an array, it contains components that can be accessed using an integer index.                 

Methods in Vector:

  1. add(element): This method is used to add elements in vector.
  2. get(index): This method is used to retrieve elements on the basis of index number.
  3. isEmpty(): This method tests if this vector has no components.
  4. remove(index): This method removes a single element on the basis of index number.
  5. clear(): This method clear all the elements from vector.
  6. size(): This method returns the number of components in this vector.
  7. removeAllElements(): This method removes all components from this vector and sets its size to zero.
  8. indexOf(element): This method returns first occurrence of given element or -1 if element is not present in vector.
  9. lastIndexOf(element): This method returns the last occurrence of given element or - 1 if element is not present in vector.

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Item at index 5= 6

After removing item from index 5 [1, 2, 3, 4, 5, 7, 8, 9, 10]

Size of vector is: 9

After Clearing vector object

Stack Class

Java Collection framework provides a Stack class which models and implements Stack data structure. The class is based on the basic principle of last-in-first-out. In addition to the basic push and pop operations, the class provides three more functions of empty, search and peek.

Methods in Stack class
  1. push(element) : Pushes an element on the top of the stack.
  2. pop() : Removes and returns the top element of the An ‘EmptyStackException’ exception is thrown if we call pop() when the invoking stack is empty.
  3. peek() : Returns the element on the top of the stack, but does not remove it.
  4. empty() : It returns true if nothing is on the top of the Else, returns false.
  5. search(element) : It determines whether an object exists in the stack. If the element is found, it returns the position of the element from the top of the stack. Else, it returns -1.

Output:

After Insertion in Stack

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

After Deletion from Stack [1, 2, 3, 4, 5, 6, 7, 8]

Stack is not empty Peek Item= 8

Item Postion of 5 = 4

Hashtable Class

This class implements a hash table, which maps keys to values. Any non-null object can be used as a key or as a value.

To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method.

  • It is similar to HashMap, but is synchronised.
  • Hashtable stores key/value pair in hash table.
  • In Hashtable we specify an object that is used as a key, and the value we want to associate to that key. The key is then hashed, and the resulting hash code is used as the index at which the value is stored within the table.

Methods in Hashtable:

  1. put(key,value): This method is used to add elements in hashtable.
  2. get(key): This method is used to retrieve elements on the basis of key.
  3. isEmpty(): This method tests if this hashtable has no components.
  4. remove(key): This method removes a single element on the basis of key.
  5. clear(): This method clear all the elements from hashtable.
  6. size(): This method returns the number of components in this hashtable.
  7. removeAllElements(): This method removes all components from this hashtable and sets its size to zero.

Output:

{5=Coffee, 2=tea, 1=beer}

Getting value of key 5: Coffee

{5=Coffee, 1=beer}

Dictionary Class

Dictionary is an abstract class, representing a key-value relation and works similar to a map. Given a key you can store values and when needed can retrieve the value back using its key. Thus, it is a list of key-value pair.

Methods in Dictionary:

  1. put(key,value): This method is used to add elements in Dictionary.
  2. get(key): This method is used to retrieve elements on the basis of key.
  3. isEmpty(): This method tests if this Dictionary has no components.
  4. remove(key): This method removes a single element on the basis of key.
  5. clear(): This method clear all the elements from Dictionary.
  6. size(): This method returns the number of components in this Dictionary.
  7. removeAllElements(): This method removes all components from this Dictionary and sets its size to zero.

Output:

Value at key = 6 : null Value at key = 456 : Code

There is no key-value pair : false Remove : Code

Check the value of removed key : null

Size of Dictionary : 1