The Java utility package provides java wide range of Java data structures. These data structures perform various designated functions. Basically, data structures provide memory space to create, store and organize data.
Types of Java Data Structures
There are two types of Java Data Structures:
- Primitive Data Structures
- Non Primitive Data Structures
Primitive Data Stuctures
Primitive Java data structures are Java’s primitive Data Types: int, char, float, boolean, double, etc. These data structures hold a single value.
Non Primitive Data Structures
Non Primitive Java Data structures can be user defined. Below is a list of few non primitive data structures provided in a framework of Java 2 called the Collection Framework.
- Stack
- Vector
- Enumeration
- Hashtable
- BitSet
- Dictionary
Stack
The Stack data structure is based on ( last in first out ) LIFO behavior. It means that the element that is first added exists at the end of the stack. And the element that is added at the end, exists at the top of the stack. User can add and remove elements from the stack using built in methods. Here is a list of most commonly used methods:
- Object pop( ): Pops (returns and removes) the object present on the top of the stack.
- Object push(Object element): Adds and returns the element in the stack.
- int search(Object element): Searches the given element in the stack and returns its index from the top of the stack. If no match is found, it returns -1.
Vector
Java vector is a data structure that is similar to an array. The main difference in an array and java vector is that vector can grow depending on the requirement. Hence it is a dynamic array. Each vector has one or more elements stored in it. The user can access the Vector elements through their index value. The size of a vector can increase and decrease automatically. unlike arrays.
Vector can be created by using the constructor Vector(). User can perform further methods on a vector by using the built in methods. A few important methods are described below:
- void add(int index, Object element): It adds an element in the vector on the given index.
- int capacity(): It returns the capacity of the specified vector.
- void clear(): It clears all the elements of the specified vector.
- Object elementAt(int index): It returns the Object element at the given index.
- int size(): It returns the number of elements in the specified vector.
Enumeration
The Java data structure Enumeration is an interface of the Collection Framework. It is not a data structure but it contains methods that enumerate single elements in a collection of data. Enumeration refers to getting one element from collection of data at a time. It keeps a track of the next element in the data structure. The most commonly used methods in Enumeration interface are:
- boolean hasMoreElements( ): It returns true as long as there are elements present in the data structure. When there are no more elements to get from the data structure, it returns false.
- Object nextElement( ): It returns object reference to the next object in the data structure.
Hashtable
Hashtable is a data structure that saves data against a key value. User defines this key value. It organizes the key value pairs in a hash table. The constructor Hashtable() creates a hashtable. User can set the size of hashtable using the constructor Hashtable(int size). User can perform multiple actions on the hashtable using the built in methods:
- Object put(Object key, Object value): Adds a key and its value into the hashtable. If the key is not found in the hashtable, then it returns NULL.
- Object remove(Object key): Removes and returns the specified key and its value from the hashtable.
- int size( ): Returns the size ( number of entries ) of the hashtable.
- boolean isEmpty( ): Returns true if the hashtable is empty. Returns false if there is at least one entry in the hashtable.
- void clear( ): Empties the hashtable
BitSet
The Java data structure BitSet is used to define an array of bits or flags. User can set or clear values of these bits and flags. User can also define the size of BitSet arrays using the constructor BitSet(int size). Here is the number of bits present in the data structure. It creates a BitSet array of size defined in its parameter. But the size of this array can increase depending on the requirement. The most commonly used methods of BitSet are:
- void clear( ): Assigns zero value to all bits.
- void clear(int index): Assigns zero to the bit present on the given index.
- void flip(int index): Reverses the bit value present on the given index.
- BitSet get(int startIndex, int endIndex): Returns a BitSet object that contain set of bits from the given starting index to the given ending index.
- int length( ): Returns the length of the BitSet.
- void set(int index): Sets the bit present on the given index to true.
Dictionary
The Java data structure Dictionary is an abstract class similar to Map. It is a data structure that stores data in the form of key value pairs. The value is mapped to the keys. Dictionaries are used where programmer needs to fetch the value using keys rather than indices. Some of the abstract methods defined in Dictionary class are:
- Object put(Object key, Object value): Adds a key and its value in the dictionary. If the key is not found in the dictionary then it returns NULL. If the value is already assigned to the key then returns the last value.
- Object remove(Object key): Removes and returns the specified key and its value. If the key is not found in the dictionary then returns NULL.
- int size( ): Returns the number of elements present in the dictionary.
- Object get(Object key): Returns value object of the specified key.