0% found this document useful (0 votes)
11 views13 pages

Java BitSet Class Overview

The document provides an overview of various data structures in Java, including Enumeration, BitSet, Vector, Stack, Dictionary, Hashtable, and Properties. It explains their functionalities, usage, and includes example code snippets for better understanding. Additionally, it mentions that these classes are considered legacy as Java has introduced the Collections Framework in Java-2.
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)
11 views13 pages

Java BitSet Class Overview

The document provides an overview of various data structures in Java, including Enumeration, BitSet, Vector, Stack, Dictionary, Hashtable, and Properties. It explains their functionalities, usage, and includes example code snippets for better understanding. Additionally, it mentions that these classes are considered legacy as Java has introduced the Collections Framework in Java-2.
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

Page 1 of 13

Home Whiteboard Online Compilers Practice Articles Tools

Chapters Categories

Java - Data Structures

The data structures provided by the Java utility package are very powerful and perform a
wide range of functions. These data structures consist of the following interface and
classes −

Enumeration

BitSet

Vector

Stack
Dictionary

Hashtable

Properties

All these classes are now legacy and Java-2 has introduced a new framework called
Collections Framework, which is discussed in the next chapter −

The Enumeration
The Enumeration interface isn't itself a data structure, but it is very important within the
context of other data structures. The Enumeration interface defines a means to retrieve
successive elements from a data structure.

For example, Enumeration defines a method called nextElement that is used to get the
next element in a data structure that contains multiple elements.

Example
Following is an example showing usage of Enumeration for a Vector.

[Link] w w .[Link]/java/java_data_structures.htm 1/13


Page 2 of 13

import [Link];
import [Link];

public class EnumerationTester {

public static void main(String args[]) {


Enumeration<String> days;
Vector<String> dayNames = new Vector<>();

[Link]("Sunday");
[Link]("Monday");
[Link]("Tuesday");
[Link]("Wednesday");
[Link]("Thursday");
[Link]("Friday");
[Link]("Saturday");
days = [Link]();

while ([Link]()) {
[Link]([Link]());
}
}
}

Output

Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday

To have more detail about this interface, check The Enumeration.

Advertisement

[Link] w w .[Link]/java/java_data_structures.htm 2/13


Page 3 of 13

The BitSet
The BitSet class implements a group of bits or flags that can be set and cleared individually.

This class is very useful in cases where you need to keep up with a set of Boolean values;
you just assign a bit to each value and set or clear it as appropriate.

Example

The following program illustrates several of the methods supported by BitSet data
structure −

import [Link];
public class BitSetDemo {

public static void main(String args[]) {


BitSet bits1 = new BitSet(16);
BitSet bits2 = new BitSet(16);

// set some bits


for(int i = 0; i < 16; i++) {
if((i % 2) == 0) [Link](i);
if((i % 5) != 0) [Link](i);
}

[Link]("Initial pattern in bits1: ");


[Link](bits1);
[Link]("\nInitial pattern in bits2: ");
[Link](bits2);

[Link] w w .[Link]/java/java_data_structures.htm 3/13


Page 4 of 13

// AND bits
[Link](bits1);
[Link]("\nbits2 AND bits1: ");
[Link](bits2);

// OR bits
[Link](bits1);
[Link]("\nbits2 OR bits1: ");
[Link](bits2);

// XOR bits
[Link](bits1);
[Link]("\nbits2 XOR bits1: ");
[Link](bits2);
}
}

Output

Initial pattern in bits1:


{0, 2, 4, 6, 8, 10, 12, 14}

Initial pattern in bits2:


{1, 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14}

bits2 AND bits1:


{2, 4, 6, 8, 12, 14}

bits2 OR bits1:
{0, 2, 4, 6, 8, 10, 12, 14}

bits2 XOR bits1:


{}

The Vector
The Vector class is similar to a traditional Java array, except that it can grow as necessary
to accommodate new elements.

Like an array, elements of a Vector object can be accessed via an index into the vector.

[Link] w w .[Link]/java/java_data_structures.htm 4/13


Page 5 of 13

The nice thing about using the Vector class is that you don't have to worry about setting it
to a specific size upon creation; it shrinks and grows automatically when necessary.

Example

The following program illustrates several of the methods supported by Vector collection −

import [Link].*;
public class VectorDemo {

public static void main(String args[]) {


// initial size is 3, increment is 2
Vector v = new Vector(3, 2);
[Link]("Initial size: " + [Link]());
[Link]("Initial capacity: " + [Link]());

[Link](new Integer(1));
[Link](new Integer(2));
[Link](new Integer(3));
[Link](new Integer(4));
[Link]("Capacity after four additions: " + [Link]());

[Link](new Double(5.45));
[Link]("Current capacity: " + [Link]());

[Link](new Double(6.08));
[Link](new Integer(7));
[Link]("Current capacity: " + [Link]());

[Link](new Float(9.4));
[Link](new Integer(10));
[Link]("Current capacity: " + [Link]());

[Link](new Integer(11));
[Link](new Integer(12));
[Link]("First element: " + (Integer)[Link]());
[Link]("Last element: " + (Integer)[Link]());

if([Link](new Integer(3)))
[Link]("Vector contains 3.");

// enumerate the elements in the vector.

[Link] w w .[Link]/java/java_data_structures.htm 5/13


Page 6 of 13

Enumeration vEnum = [Link]();


[Link]("\nElements in vector:");

while([Link]())
[Link]([Link]() + " ");
[Link]();
}
}

Output

Initial size: 0
Initial capacity: 3
Capacity after four additions: 5
Current capacity: 5
Current capacity: 7
Current capacity: 9
First element: 1
Last element: 12
Vector contains 3.

Elements in vector:
1 2 3 4 5.45 6.08 7 9.4 10 11 12

The Stack
The Stack class implements a last-in-first-out (LIFO) stack of elements.

You can think of a stack literally as a vertical stack of objects; when you add a new
element, it gets stacked on top of the others.

When you pull an element off the stack, it comes off the top. In other words, the last
element you added to the stack is the first one to come back off.

Example
The following program illustrates several of the methods supported by Stack collection −

[Link] w w .[Link]/java/java_data_structures.htm 6/13


Page 7 of 13

import [Link].*;
public class StackDemo {

static void showpush(Stack st, int a) {


[Link](new Integer(a));
[Link]("push(" + a + ")");
[Link]("stack: " + st);
}

static void showpop(Stack st) {


[Link]("pop -> ");
Integer a = (Integer) [Link]();
[Link](a);
[Link]("stack: " + st);
}

public static void main(String args[]) {


Stack st = new Stack();
[Link]("stack: " + st);
showpush(st, 42);
showpush(st, 66);
showpush(st, 99);
showpop(st);
showpop(st);
showpop(st);
try {
showpop(st);
} catch (EmptyStackException e) {
[Link]("empty stack");
}
}
}

Output

stack: [ ]
push(42)
stack: [42]
push(66)
stack: [42, 66]

[Link] w w .[Link]/java/java_data_structures.htm 7/13


Page 8 of 13

push(99)
stack: [42, 66, 99]
pop -> 99
stack: [42, 66]
pop -> 66
stack: [42]
pop -> 42
stack: [ ]
pop -> empty stack

The Dictionary
The Dictionary class is an abstract class that defines a data structure for mapping keys to
values.

This is useful in cases where you want to be able to access data via a particular key rather
than an integer index.

Since the Dictionary class is abstract, it provides only the framework for a key-mapped
data structure rather than a specific implementation.

Example
The following example shows the usage of Java Dictionary keys() method. We're creating
a dictionary instance using Hashtable object of Integer, Integer. Then we've added few
elements to it. An enumeration is retrieved using keys() method and enumeration is then
iterated to print the keys of the dictionary.

package [Link];

import [Link];
import [Link];
import [Link];

public class DictionaryDemo {


public static void main(String[] args) {

// create a new hashtable


Dictionary<Integer, Integer> dictionary = new Hashtable<>();

[Link] w w .[Link]/java/java_data_structures.htm 8/13


Page 9 of 13

// add 2 elements
[Link](1, 1);
[Link](2, 2);

Enumeration<Integer> enumeration = [Link]();

while([Link]()) {
[Link]([Link]());
}
}
}

Output

2
1

The Hashtable
The Hashtable class provides a means of organizing data based on some user-defined key
structure.

For example, in an address list hash table you could store and sort data based on a key
such as ZIP code rather than on a person's name.

The specific meaning of keys with regard to hash tables is totally dependent on the usage
of the hash table and the data it contains.

Example
The following example shows the usage of Java Hashtable contains() method to check if a
value is present in a Hashtable or not. We've created a Hashtable object of Integer,Integer.
Then few entries are added, table is printed and using contains() we're checking about two
values in the table.

package [Link];

import [Link];

[Link] w w .[Link]/java/java_data_structures.htm 9/13


Page 10 of 13

public class HashtableDemo {


public static void main(String args[]) {

// create hash table


Hashtable<Integer,Integer> hashtable = new Hashtable<>();

// populate hash table


[Link](1, 1);
[Link](2, 2);
[Link](3, 3);

[Link]("Initial table elements: " + hashtable);


[Link]("Hashtable contains 2 as value: " +
[Link](2));
[Link]("Hashtable contains 4 as value: " +
[Link](4));
}
}

Output

Initial table elements: {3=3, 2=2, 1=1}


Hashtable contains 2 as value: true
Hashtable contains 4 as value: false

The Properties
Properties is a subclass of Hashtable. It is used to maintain lists of values in which the key
is a String and the value is also a String.

The Properties class is used by many other Java classes. For example, it is the type of
object returned by [Link]( ) when obtaining environmental values.

Example
The following example shows the usage of Java Properties getProperty(String key)
method to get a value based on a key from a Properties. We've created a Properties
object. Then few entries are added. Using getProperty() method, a value is retrieved and
printed.

[Link] w w .[Link]/java/java_data_structures.htm 10/13


Page 11 of 13

package [Link];

import [Link];

public class PropertiesDemo {


public static void main(String[] args) {
Properties properties = new Properties();

//populate properties object


[Link]("1", "tutorials");
[Link]("2", "point");
[Link]("3", "is best");

[Link]("Properties elements: " + properties);


[Link]("Value: " + [Link]("1"));
}
}

Output

Properties elements: {1=tutorials, 2=point, 3=is best}


Value: tutorials

TOP TUTORIALS

Python Tutorial

Java Tutorial
C++ Tutorial

C Programming Tutorial

C# Tutorial
PHP Tutorial

R Tutorial

HTML Tutorial
CSS Tutorial

JavaScript Tutorial

SQL Tutorial

[Link] w w .[Link]/java/java_data_structures.htm 11/13


Page 12 of 13

TRENDING TECHNOLOGIES

Cloud Computing Tutorial

Amazon Web Services Tutorial


Microsoft Azure Tutorial

Git Tutorial

Ethical Hacking Tutorial


Docker Tutorial

Kubernetes Tutorial

DSA Tutorial
Spring Boot Tutorial

SDLC Tutorial

Unix Tutorial

CERTIFICATIONS

Business Analytics Certification


Java & Spring Boot Advanced Certification

Data Science Advanced Certification

Cloud Computing And DevOps


Advanced Certification In Business Analytics

Artificial Intelligence And Machine Learning

DevOps Certification
Game Development Certification

Front-End Developer Certification

AWS Certification Training


Python Programming Certification

COMPILERS & EDITORS

Online Java Compiler

Online Python Compiler

Online Go Compiler
Online C Compiler

Online C++ Compiler

Online C# Compiler
Online PHP Compiler

Online MATLAB Compiler

Online Bash Terminal

[Link] w w .[Link]/java/java_data_structures.htm 12/13


Page 13 of 13

Online SQL Compiler


Online Html Editor

ABOUT US | OUR TEAM | CAREERS | JOBS | CONTACT US | TERMS OF USE |

PRIVACY POLICY | REFUND POLICY | COOKIES POLICY | FAQ'S

Tutorials Point is a leading Ed Tech company striving to provide the best learning material on
technical and non-technical subjects.

© Copyright 2025. All Rights Reserved.

[Link] w w .[Link]/java/java_data_structures.htm 13/13

You might also like