• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Data Structures in Java
Next →
← Prev

Data Structures in Java

By Dinesh Thakur

This tutorial will cover the fundamentals of implements the standard varieties of data structures in Java. We’ll give you a basic introduction to the usual suspects-arrays, vectors, hash tables, and stacks-and how to use them in your Java programs.

We’ll be covering the following topics in this tutorial:

  • Array
  • Vector
  • BitSets
  • Hash Tables
  • Stacks

Array

An Array is a (fixed length) data structures used to store multiple data elements of the same type. The memory allocated to an array is consecutive. All the elements of an array are under one variable name. Its position accesses every data element of an array, and an integer value indicates that position that starts from 0 (zero) called index. The array size is fixed and cannot change at runtime of the program.

Java supports two types of arrays:

• One Dimensional Array
• Multiple Dimensional Array

In Java, arrays can be of either type, primitive data types or references types. In primitive data type array, all elements are of a definite primitive data type. In references type array, all elements are of definite reference type.

Vector

Arrays have a fixed length. They are not suitable for a group of things that grow and shrink over an application’s lifetime. In such situations, Java provides the Vector class, which is available in the standard Java.util package.

A Vector is similar to an array in that it holds multiple objects and can retrieve the stored objects by using an index value. However, the primary difference between an array and a Vector is that it can grow automatically when its initial capacity exceeded. A Vector also provides methods for adding and removing elements that you would normally have to do manually in an array. To sum up, the Vector class represents an ordered collection of objects referenced using indexes and can grow and shrink in size.

At any time, a Vector contains a number of elements that is less than or equal to its capacity. The capacity represents the size of the Vector in memory. After the capacity reached, the Vector must grow before a new element can be added. Its attribute capacity increment represents the amount by which the Vector should grow. If the capacity increment is 0, the array doubles in capacity each time it needs to grow.

The standard Vector class, Java.util.Vector is a parameterized class, i.e. when you create an object of that class, you must supply the underlying type.

BitSets

The Java BitSet class, like arrays and vectors, also allows you to store, manipulate, and retrieve sequential data. This class is different, however, in that it stores sequences of bits. BitSets are useful for storing a collection of Boolean style flags (True or False). Although you can accomplish nearly the same thing with an array or Vector of Boolean types or objects, the BitSet class is more efficient for this job. It collects the bits into bytes for compact storage.

Hash Tables

A hash table is a storage mechanism that creates a key-and-value pair for each element. For example, suppose you are keeping records of employees within a company. You need to develop a mechanism for retrieving these records based on employees’ social security numbers. An array really won’t work well for this because the nine-digit identification number is too large. (Vectors and arrays work best for an ordered list of sequential values.) In this situation, hash tables are the answer.

Hash tables enable quick location and retrieval of data by using a key. This key is associated with data contained within an object. The key and value can be from any object type, but the key object must implement the hashCode() and equals() methods. Once these methods have implemented, the hash table computes a small integer from the key; this is the hash code and the location where the key/value pair exists. Unlike arrays and vectors, the order of values in a hash table is not important-the hash code ID is all that matters. What is important is that the keys are unique: Only one value can be stored for each key. If you add a value to a key that is already holding another value, then the added value will replace the preexisting value.

As with other data structures, the hash table’s performance is affected by the ratio between the number of elements and the structure’s capacity. This ratio is called the load factor and represented as a percentage. Hash tables need to allocate more storage space than is needed because they generate the special hash code from the identifying key. As the load factor increases, so does the likelihood that two or more generated hash codes will
have the same value; this is called a collision. The extra space used to ensure that all key/value pairs remain unique. Ideally, the load factor is not higher than 50%; that is, the capacity should double the number of elements in the hash table. If the load factor goes over 75%, the hash table will automatically double the capacity to ensure adequate performance.

Stacks

A stack is a mechanism for building what’s called a “last in, first out” structure, Think of this as a virtual Pez candy dispenser; each piece of candy is a data element. (For those who are a little more ferocious, another analogy for a stack is a clip for a handgun.) The last element added is also the first one to come out. A stack can use as an easy instrument for reversing the order of a sequence of data.

You’ll also like:

  1. What is data structure? List out the areas in which data structures are applied extensively
  2. Data Structures in C
  3. Data Structures in Python
  4. What is Data Mining? and Explain Data Mining Techniques. Compare between Data Mining and Data Warehousing.
  5. Difference between Structures and Unions
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Java Tutorials

Java Tutorials

  • Java - Home
  • Java - IDE
  • Java - Features
  • Java - History
  • Java - this Keyword
  • Java - Tokens
  • Java - Jump Statements
  • Java - Control Statements
  • Java - Literals
  • Java - Data Types
  • Java - Type Casting
  • Java - Constant
  • Java - Differences
  • Java - Keyword
  • Java - Static Keyword
  • Java - Variable Scope
  • Java - Identifiers
  • Java - Nested For Loop
  • Java - Vector
  • Java - Type Conversion Vs Casting
  • Java - Access Protection
  • Java - Implicit Type Conversion
  • Java - Type Casting
  • Java - Call by Value Vs Reference
  • Java - Collections
  • Java - Garbage Collection
  • Java - Scanner Class
  • Java - this Keyword
  • Java - Final Keyword
  • Java - Access Modifiers
  • Java - Design Patterns in Java

OOPS Concepts

  • Java - OOPS Concepts
  • Java - Characteristics of OOP
  • Java - OOPS Benefits
  • Java - Procedural Vs OOP's
  • Java - Polymorphism
  • Java - Encapsulation
  • Java - Multithreading
  • Java - Serialization

Java Operator & Types

  • Java - Operator
  • Java - Logical Operators
  • Java - Conditional Operator
  • Java - Assignment Operator
  • Java - Shift Operators
  • Java - Bitwise Complement Operator

Java Constructor & Types

  • Java - Constructor
  • Java - Copy Constructor
  • Java - String Constructors
  • Java - Parameterized Constructor

Java Array

  • Java - Array
  • Java - Accessing Array Elements
  • Java - ArrayList
  • Java - Passing Arrays to Methods
  • Java - Wrapper Class
  • Java - Singleton Class
  • Java - Access Specifiers
  • Java - Substring

Java Inheritance & Interfaces

  • Java - Inheritance
  • Java - Multilevel Inheritance
  • Java - Single Inheritance
  • Java - Abstract Class
  • Java - Abstraction
  • Java - Interfaces
  • Java - Extending Interfaces
  • Java - Method Overriding
  • Java - Method Overloading
  • Java - Super Keyword
  • Java - Multiple Inheritance

Exception Handling Tutorials

  • Java - Exception Handling
  • Java - Exception-Handling Advantages
  • Java - Final, Finally and Finalize

Data Structures

  • Java - Data Structures
  • Java - Bubble Sort

Advance Java

  • Java - Applet Life Cycle
  • Java - Applet Explaination
  • Java - Thread Model
  • Java - RMI Architecture
  • Java - Applet
  • Java - Swing Features
  • Java - Choice and list Control
  • Java - JFrame with Multiple JPanels
  • Java - Java Adapter Classes
  • Java - AWT Vs Swing
  • Java - Checkbox
  • Java - Byte Stream Classes
  • Java - Character Stream Classes
  • Java - Change Color of Applet
  • Java - Passing Parameters
  • Java - Html Applet Tag
  • Java - JComboBox
  • Java - CardLayout
  • Java - Keyboard Events
  • Java - Applet Run From CLI
  • Java - Applet Update Method
  • Java - Applet Display Methods
  • Java - Event Handling
  • Java - Scrollbar
  • Java - JFrame ContentPane Layout
  • Java - Class Rectangle
  • Java - Event Handling Model

Java programs

  • Java - Armstrong Number
  • Java - Program Structure
  • Java - Java Programs Types
  • Java - Font Class
  • Java - repaint()
  • Java - Thread Priority
  • Java - 1D Array
  • Java - 3x3 Matrix
  • Java - drawline()
  • Java - Prime Number Program
  • Java - Copy Data
  • Java - Calculate Area of Rectangle
  • Java - Strong Number Program
  • Java - Swap Elements of an Array
  • Java - Parameterized Constructor
  • Java - ActionListener
  • Java - Print Number
  • Java - Find Average Program
  • Java - Simple and Compound Interest
  • Java - Area of Rectangle
  • Java - Default Constructor Program
  • Java - Single Inheritance Program
  • Java - Array of Objects
  • Java - Passing 2D Array
  • Java - Compute the Bill
  • Java - BufferedReader Example
  • Java - Sum of First N Number
  • Java - Check Number
  • Java - Sum of Two 3x3 Matrices
  • Java - Calculate Circumference
  • Java - Perfect Number Program
  • Java - Factorial Program
  • Java - Reverse a String

Other Links

  • Java - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW