Java के Data Structures
In this page:
What is a Data Structure?
एक data structure data को organize और store करने का एक तरीका है ताकि इसे efficiently access और modify किया जा सके, एक simple fixed-size array से लेकर lists, sets, और maps जैसी ज़्यादा flexible structures तक जो runtime पर बढ़ती और सिकुड़ती हैं।
उदाहरण: What is a Data Structure?
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>(); // flexible structure vs fixed array
numbers.add(1);
numbers.add(2);
System.out.println(numbers);
}
}
Login to try C/C++/Java/PHP code in the editor
Linear vs Non-Linear Structures
Arrays, ArrayList, और LinkedList जैसी Linear data structures elements को एक single sequential order में store करती हैं, जबकi HashMap और trees जैसी non-linear structures data को relationships से organize करती हैं, जैसे एक key का एक value में map होना, position से नहीं।
उदाहरण: Linear vs Non-Linear Structures
import java.util.ArrayList;
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>(); // linear: sequential order
list.add("a");
HashMap<String, Integer> map = new HashMap<>(); // non-linear: relationships
map.put("a", 1);
System.out.println(list);
System.out.println(map);
}
}
Login to try C/C++/Java/PHP code in the editor
Java's Collections Framework
Java का Collections Framework standard interfaces का एक set प्रदान करता है -- List, Set, Map, और Queue -- हर एक के कई ready-made implementations के साथ, इसलिए code एक specific implementation के बजाय general interface के खिलाफ लिखा जा सकता है।
उदाहरण: Java's Collections Framework
import java.util.List;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
List<String> names = new ArrayList<>(); // coded against the List interface
names.add("Alice");
System.out.println(names);
}
}
Login to try C/C++/Java/PHP code in the editor
Choosing the Right Structure
सही data structure चुनना इस पर निर्भर करता है कि data कैसे उपयोग होगा: एक List position से accessed ordered data को सूट करता है, एक Set उस data को सूट करता है जिसमें कभी duplicates नहीं होने चाहिए, और एक Map उस data को सूट करता है जिसे एक unique key से lookup करना है।
उदाहरण: Choosing the Right Structure
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
HashSet<String> uniqueNames = new HashSet<>(); // Set: no duplicates allowed
uniqueNames.add("Alice");
uniqueNames.add("Alice");
System.out.println(uniqueNames.size());
}
}
Login to try C/C++/Java/PHP code in the editor
Big O Intuition
Big O notation describe करता है कि data की मात्रा बढ़ने के साथ एक structure की performance कैसे बदलती है -- index से एक ArrayList access करना इसके size की परवाह किए बिना constant time रहता है, जबकि index से एक LinkedList access करना ज़्यादा elements जुड़ने पर धीमा होता जाता है।
उदाहरण: Big O Intuition
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
System.out.println(list.get(0)); // constant time, regardless of list size
}
}
Login to try C/C++/Java/PHP code in the editor
- Random access के लिए
LinkedListचुनना, जबArrayListget(index)के लिए तेज़ है। - एक array उपयोग करना जब size पहले से पता नहीं।
- Operations की cost नज़रअंदाज़ करना, जैसे एक list पर
contains()का O(n) होना।
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- Java Data Structures
- Java Collections Introduction
- Java List Interface
- Java ArrayList
- Java LinkedList
- Java Map Interface
- Java HashMap
- Java Set Interface
- Java TreeMap
- Java LinkedHashMap
- Java HashSet
- Java TreeSet
- Java LinkedHashSet
- Java Stack & Queue
- Java Iterator
- Java Collections Class
- Java List Sorting