In this article, you are going to learn Data Structures in Java Programming Language.
Data Structures perform various functions and consists of classes and interfaces which comes along with the java utility package.
Also Read:- Arrays in Java Programming Language
Following is a list of classes and interfaces.
- BitSet Class
- Enumeration Interface
- Vector Class
- Dictionary Class
- Stack Class
- Hashtable Class
- Properties Class
[1] BITSET
This class is used to create arrays containing bit values. The size of the array can be increased according to the need. You can also set or clear the bit values.
The BitSet Class has following Constructors and Methods.
Constructors:
- BitSet( )
- BitSet(int size)
Methods:
- void and(BitSet bitSet)
- int cardinality( )
- void clear(int index)
- Object clone( )
- void flip(int index)
- boolean get(int index)
- boolean equals(Object bitSet)
- int hashCode( )
- boolean isEmpty( )
- void set(int index)
- int nextSetBit(int startIndex)
- void or(BitSet bitSet)
- int size( )
- void xor(BitSet bitSet)
- String toString( )
Example:
class BitSetExample {
public static void main(String args[]) {
BitSet obj1 = new BitSet(12) ;
BitSet obj2 = new BitSet(12) ;
for(int a = 0 ; a < 12 ; a++) {
if((a % 2) == 0) obj1.set(a) ;
if((a % 3) != 0) obj2.set(a) ;
}
System.out.println(“obj1 pattern will be : “) ;
System.out.println(obj1) ;
System.out.println(“\nobj2 pattern will be : “) ;
System.out.println(obj2) ;
obj2.or(obj1) ;
System.out.println(“\nobj2 OR obj1 will be : “) ;
System.out.println(obj2) ;
obj2.and(obj1) ;
System.out.println(“\nobj2 AND obj1 will be : “) ;
System.out.println(obj2) ;
}
}
Output:
obj1 pattern will be :
{0, 2, 4, 6, 8, 10}
obj2 pattern will be :
{1, 2, 4, 5, 7, 8, 10, 11}
obj2 OR obj1 will be :
{0, 1, 2, 4, 5, 6, 7, 8, 10, 11}
obj2 AND obj1 will be :
{0, 2, 4, 6, 8, 10}
[2] ENUMERATION
The enumeration interface contains methods that are used to obtain consecutive elements from a group of objects(data structure). This interface is used in vector, properties and several other API classes for creating applications.
The enumeration interface has the following methods.
Method:
- Object nextElement( )
- boolean hasMoreElements( )
Example:
class EnumerationExample {
public static void main(String args[]) {
Enumeration championstrophy ;
Vector matchSchedule = new Vector() ;
matchSchedule.add(“India vs Pakistan”) ;
matchSchedule.add(“Australia vs New Zealand”) ;
matchSchedule.add(“South Africa vs Sri Lanka”) ;
matchSchedule.add(“India vs Bangladesh”) ;
matchSchedule.add(“England vs Pakistan “) ;
matchSchedule.add(“India vs England”) ;
championstrophy = matchSchedule.elements() ;
while (championstrophy.hasMoreElements()) {
System.out.println(championstrophy.nextElement()) ;
}
}
}
Output:
India vs Pakistan
Australia vs New Zealand
South Africa vs Sri Lanka
India vs Bangladesh
England vs Pakistan
India vs England
[3] VECTOR
Constructors:
- Vector( )
- Vector(int size, int incr)
- Vector(int size)
- Vector(Collection c)
Methods:
- void add(int index, Object element)
- boolean add(Object o)
- void addElement(Object obj)
- int capacity()
- boolean contains(Object elem)
- Object elementAt(int index)
- Enumeration elements()
- void ensureCapacity(int minCapacity)
- int hashCode()
- int indexOf(Object elem, int index)
- int lastIndexOf(Object elem, int index)
- Object remove(int index)
- void removeAllElements()
- protected void removeRange(int fromIndex, int toIndex)
- int size()
- List subList(int fromIndex, int toIndex)
- Object[] toArray(Object[] a)
- void trimToSize()
- String toString()
- boolean retainAll(Collection c)
Example:
class VectorExample {
public static void main(String args[]) {
Vector vect = new Vector(2, 3) ;
System.out.println(“Initial Capacity : “ + vect.size()) ;
System.out.println(“Capacity Increment : “ + vect.capacity()) ;
vect.addElement(new Integer(1)) ;
vect.addElement(new Integer(2)) ;
System.out.println(“Capacity after two additions : “ + vect.capacity()) ;
vect.addElement(new Double(3.23)) ;
System.out.println(“Current capacity : “ + vect.capacity()) ;
vect.addElement(new Integer(4)) ;
System.out.println(“Current capacity : “ + vect.capacity()) ;
System.out.println(“First element : “ + (Integer)vect.firstElement()) ;
System.out.println(“Last element : “ + (Integer)vect.lastElement()) ;
Enumeration vectEnum = vect.elements() ;
System.out.println(“\nElements in vector :“) ;
while(vectEnum.hasMoreElements())
System.out.print(vectEnum.nextElement() + ” “) ;
System.out.println() ;
}
}
Output:
Initial Capacity: 0
Capacity Increment: 2
Capacity after two additions: 2
Current capacity: 5
Current capacity: 5
First element: 1
Last element: 4
Elements in vector:
1 2 3.23 4
[4] DICTIONARY
Methods:
- Enumeration elements( )
- Enumeration keys( )
- Object get(Object key)
- Object put(Object key, Object value)
- Object remove(Object key)
- int size( )
- boolean isEmpty( )
[5] STACK
Methods:
- int search(Object element)
- Object pop( )
- Object peek( )
- Object push(Object element)
- boolean empty()
Example:
class StackExample {
static void displaypush(Stack s, int i) {
s.push(new Integer(i)) ;
System.out.println(“push(” + i + “)”) ;
System.out.println(“stack : “ + s) ;
}
static void displaypop(Stack s) {
System.out.print(“pop – “) ;
Integer i = (Integer) s.pop() ;
System.out.println(i) ;
System.out.println(“stack : “ + s) ;
}
public static void main(String args[]) {
Stack s = new Stack() ;
System.out.println(“stack : “ + s) ;
displaypush(s, 23) ;
displaypush(s, 34) ;
displaypop(s) ;
displaypop(s) ;
try {
displaypop(s) ;
}
catch (EmptyStackException ref) {
System.out.println(“Empty Stack”) ;
}
}
}
Output:
stack : [] push(23)
stack : [23] push(34)
stack : [23, 34] pop – 34
stack : [23] pop – 23
stack : [] pop – Empty Stack
[6] HASHTABLE
Constructors:
- Hashtable( )
- Hashtable(int size)
- Hashtable(Map < ? extends K, ? extends V > t)
- Hashtable(int size, float fillRatio)
Methods:
- boolean contains(Object value)
- boolean containsValue(Object value)
- boolean containsKey(Object key)
- boolean isEmpty( )
- Object clone( )
- Object get(Object key)
- Object put(Object key, Object value)
- Object remove(Object key)
- Enumeration elements( )
- Enumeration keys( )
- void clear( )
- void rehash( )
- int size( )
- String toString( )
[7] PROPERTIES
Constructors:
- Properties( )
- Properties(Properties propDefault)
Methods:
- void list(PrintWriter streamOut)
- void list(PrintStream streamOut)
- void store(OutputStream streamOut, String description)
- void load(InputStream streamIn) throws IOException
- String getProperty(String key)
- String getProperty(String key, String defaultProperty)
- Object setProperty(String key, String value)
- Enumeration propertyNames( )
Also Read: Nested Classes in Java Programming Language
We have provided you the description on Data Structures in Java Programming Language. Hope you like this post. For more updates and related information, stay connected top our blogs on java programming language.