0% found this document useful (0 votes)
22 views3 pages

Assignment 1 - CLV

The document introduces various data structures in Java, including BitSet, Vector, Stack, and Hashtable, detailing their theories, advantages, and disadvantages. Each data structure is accompanied by a Java example demonstrating its usage. Key points include BitSet's memory efficiency, Vector's thread safety, Stack's LIFO mechanism, and Hashtable's key-value mapping with thread safety.

Uploaded by

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

Assignment 1 - CLV

The document introduces various data structures in Java, including BitSet, Vector, Stack, and Hashtable, detailing their theories, advantages, and disadvantages. Each data structure is accompanied by a Java example demonstrating its usage. Key points include BitSet's memory efficiency, Vector's thread safety, Stack's LIFO mechanism, and Hashtable's key-value mapping with thread safety.

Uploaded by

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

Assignment 1: Introduction of Data Structure Using JAVA

1. BitSet in Java
Theory:
• BitSet is a class in java.util that implements a vector of bits (binary values). Each bit
has a boolean value: true or false.
• Bits are indexed by non-negative integers.
• It's useful when memory-efficient storage of binary flags is required.
Java Example:
import java.util.BitSet;

public class BitSetExample {


public static void main(String[] args) {
BitSet bitset = new BitSet(8);
bitset.set(2);
bitset.set(4);
bitset.set(6);

System.out.println("BitSet: " + bitset);


}
}
Output:
BitSet: {2, 4, 6}
Advantages:
• Memory efficient for binary data.
• Easy to manipulate individual bits.
• Useful for flags and state tracking.
Disadvantages:
• Not type-safe.
• Limited to binary values (true/false).
• Doesn’t directly support storing other data types.

2. Vector in Java
Theory:
• Vector is a growable array of objects from the java.util package.
• It is synchronized, hence thread-safe.
• It dynamically resizes when needed.
Java Example:
import java.util.Vector;
public class VectorExample {
public static void main(String[] args) {
Vector<String> fruits = new Vector<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Mango");

System.out.println("Fruits: " + fruits);


}
}
Output:
Fruits: [Apple, Banana, Mango]
Advantages:
• Thread-safe due to synchronization.
• Dynamic resizing.
• Can store duplicate elements.
Disadvantages:
• Slower compared to non-synchronized alternatives (like ArrayList).
• Consumes more memory.

3. Stack in Java
Theory:
• Stack is a subclass of Vector that implements a Last-In-First-Out (LIFO) stack of objects.
• It has methods like push(), pop(), peek().
Java Example:
import java.util.Stack;

public class StackExample {


public static void main(String[] args) {
Stack<Integer> numbers = new Stack<>();
numbers.push(10);
numbers.push(20);
numbers.push(30);

System.out.println("Top Element: " + numbers.peek());


System.out.println("Popped: " + numbers.pop());
System.out.println("Stack: " + numbers);
}
}
Output:
Top Element: 30
Popped: 30
Stack: [10, 20]
Advantages:
• Easy to implement undo/redo operations.
• Provides in-built LIFO mechanism.
• Methods are easy to use.
Disadvantages:
• Not suitable for random access.
• Can lead to stack overflow if not handled correctly.

4. HashTable in Java
Theory:
• Hashtable is a collection that maps keys to values.
• It doesn't allow null keys or values.
• It is synchronized (thread-safe).
Java Example:
import java.util.Hashtable;

public class HashTableExample {


public static void main(String[] args) {
Hashtable<Integer, String> students = new Hashtable<>();
students.put(101, "Alice");
students.put(102, "Bob");

System.out.println("Student 101: " + students.get(101));


}
}
Output:
Student 101: Alice
Advantages:
• Thread-safe.
• Fast access to data using keys.
• No duplicate keys allowed.
Disadvantages:
• Synchronized – may affect performance.
• Does not allow null keys or values.
• Unordered – does not maintain insertion order.s

You might also like