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:
- add(element): This method is used to add elements in vector.
- get(index): This method is used to retrieve elements on the basis of index number.
- isEmpty(): This method tests if this vector has no components.
- remove(index): This method removes a single element on the basis of index number.
- clear(): This method clear all the elements from vector.
- size(): This method returns the number of components in this vector.
- removeAllElements(): This method removes all components from this vector and sets its size to zero.
- indexOf(element): This method returns first occurrence of given element or -1 if element is not present in vector.
- 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
- push(element) : Pushes an element on the top of the stack.
- pop() : Removes and returns the top element of the An ‘EmptyStackException’ exception is thrown if we call pop() when the invoking stack is empty.
- peek() : Returns the element on the top of the stack, but does not remove it.
- empty() : It returns true if nothing is on the top of the Else, returns false.
- 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:
- put(key,value): This method is used to add elements in hashtable.
- get(key): This method is used to retrieve elements on the basis of key.
- isEmpty(): This method tests if this hashtable has no components.
- remove(key): This method removes a single element on the basis of key.
- clear(): This method clear all the elements from hashtable.
- size(): This method returns the number of components in this hashtable.
- 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:
- put(key,value): This method is used to add elements in Dictionary.
- get(key): This method is used to retrieve elements on the basis of key.
- isEmpty(): This method tests if this Dictionary has no components.
- remove(key): This method removes a single element on the basis of key.
- clear(): This method clear all the elements from Dictionary.
- size(): This method returns the number of components in this Dictionary.
- 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