This beginner-friendly guide covers Data Structures and Algorithms (DSA) in Java, including built-in structures like arrays, strings, ArrayList, HashMap, HashSet, and user-defined structures such as linked lists, stacks, queues, trees, heaps, and graphs. It also explains how to analyze algorithm efficiency.
It is recommended to read about Analysis of Algorithms before beginning this tutorial.
1. Array
In Java, there are mainly two types of arrays.
- Array : Fixed-size collection of similar data types stored contiguously, ideal when element count is known.
- ArrayList : Dynamic and grow as needed. It is from the Java Collections Framework, suitable when element count varies.
import java.util.*;
class Geeks{
public static void main(String[] args) {
// Array example
int[] arr = {10, 20, 30, 40, 50};
System.out.println(Arrays.toString(arr));
// ArrayList example
ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
list.add(30);
System.out.println(list);
}
}
Output
[10, 20, 30, 40, 50] [10, 20, 30]
Related Posts:
Recommended DSA Problems:
2. Searching Algorithms
Searching algorithms help locate an element in data structures like arrays or lists. Java provides both linear search and binary search (via Arrays.binarySearch).
import java.util.*;
class Geeks {
public static void main