Data structures in Java

Nickson Joram
Analytics Vidhya
Published in
4 min readApr 14, 2020

Data ! One of the most expensive word in this world right now. People are so keen in gathering data, processing the data and delivering a lot of outputs with that data.

If you are given with a number of data, what will you do first?

Let me take you to a library first. If I ask you to bring Linear Algebra done right, a textbook by Sheldon Axler, what will you do? Will you randomly search it? Please don’t say YES. First, you need to look at the map of library (if available) or you can ask the Library staff to locate the Mathematics block. Why? You know that Linear algebra is a Mathematical concept, isn’t it?

According to library science and architecture, once you enter the mathematics block you can see a lot of stacks. Now we have to start searching for the book from our near-most stack! Yes? NO!

There is a wording I’ve used in the above paragraph like library science and architecture. In Mathematics, there are two major divisions called Pure Mathematics and Applied Mathematics. Linear Algebra belongs to Algebra in Pure Mathematics. So first, you’ve to find Pure Mathematics area and then to Algebra stack. There you can start searching from your nearest point, Yes? NO!. Go with the alphabet indication.

Or else, we can have another example. The simplest available. Finding a word in a dictionary.

In both instances, you can see, there may be a hell lot of data. But the architecture in which the data is organized giving a nice access to the specifics, simply a data structure. Data Structure is a finite way to store and organize data so that it can be used efficiently.

So how to handle those data in our systems? Specifically in Java? That is what You are going to read today.

There are a number of data structures that we are also using in Java. The most frequently used structures are,

  1. Array
  2. Linked List
    - Singly Linked List
    - Circular Linked List
    - Doubly Linked List
  3. Stack
  4. Queue
  5. Binary Tree
  6. Binary Search Tree
  7. Heap
  8. Hashing
  9. Graph
  10. Matrix
  11. Advanced Data Structure
    - Advanced Lists
    - Segment Tree
    - Trie
    - Binary Indexed Tree
    - Suffix Array and Suffix Tree
    - AVL Tree
    - Splay Tree
    - B Tree
    - Red-Black Tree
    - K Dimensional Tree

Even though we have a number of data structures, we are using the most perfect structure based on the requirement. You can figure it out why once you came to know about the data structures and its applications.

The data structures which are provided by the Java utility package are very powerful and can perform a wide range of functions. There are some built-in interfaces and classes in Java for these data structures. They are,

  1. Enumeration
  2. BitSet
  3. Vector
  4. Stack
  5. Dictionary
  6. Hashtable
  7. Properties

Lets see about two of them.

  • Enumeration

Deals with accessing the successive element in a data structure. Enumerate in the sense, access one at a time. This is currently in widespread use in application codes.

Lets check about this interface by an example. We have to import two libraries from Java.

import java.util.Vector;
import java.util.Enumeration;

With the usual class naming and main method, we have to initialize and using the features of imported libraries.

Enumeration days;
Vector names= new Vector();

Now, we have to define or add data into the Vector created, names. We can simply add data into a Vector class like this.

names.add(“Monday”);
names.add(“Tuesday”);
names.add(“Wednesday”);
names.add(“Thursday”);
names.add(“Friday”);
names.add(“Saturday”);
names.add(“Sunday”);

Then, we have to define the Enumeration variable like this.

days = names.elements();

Now, we have to use the methods existing in Enumeration to get access to each element in it (simply, in days).

while (days.hasMoreElements()) {
System.out.println(days.nextElement());
}

You’ve seen how while loop works already (if not, refer this). So in this loop, hasMoreElements() will give a boolean value, either True or False. When the output be True the loop will run. And in this program you can guess the output right?

  • BitSet

BitSet class creates a special array that is intended to have bit values. It is similar to bits in a vector and we can increase the size as needed. Lets implement BitSet now.

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

This constructor initializes the size, that is, the number of bits that can be held. Also, all bits are initialized to zero.

Now, lets set the values for bits1 and bits2.

for(int i = 0; i < 16; i++) {
if((i % 2) == 0)
bits1.set(i);
if((i % 5) != 0)
bits2.set(i);
}

Can you guess the values assigned?

bits1 has {0, 2, 4, 6, 8, 10, 12, 14} and bits2 has {1, 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14}.

Now, you can simply try to get the things done.

// AND bits
bits2.and(bits1);
// OR bits
bits2.or(bits1);
// XOR bits
bits2.xor(bits1);

You can print the values and cross check as needed.

We shall see about a lot of data structures briefly and clearly in an another instance. Keep practicing the rest of the interfaces and classes in Java to handle the data structures.

--

--