← Back to Core Java Course | Chapter 11: Collections | Lesson 1 of 17

Java के Data Structures

एक data structure data को organize और store करता है ताकि इसे efficiently access और modify किया जा सके, और Java के built-in options एक simple fixed-size array से लेकर Collections Framework में flexible List, Set, और Map types तक range करते हैं।

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?

java
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);
	}
}

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

java
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);
	}
}

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

java
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);
	}
}

Choosing the Right Structure

सही data structure चुनना इस पर निर्भर करता है कि data कैसे उपयोग होगा: एक List position से accessed ordered data को सूट करता है, एक Set उस data को सूट करता है जिसमें कभी duplicates नहीं होने चाहिए, और एक Map उस data को सूट करता है जिसे एक unique key से lookup करना है।

उदाहरण: Choosing the Right Structure

java
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());
	}
}

Big O Intuition

Big O notation describe करता है कि data की मात्रा बढ़ने के साथ एक structure की performance कैसे बदलती है -- index से एक ArrayList access करना इसके size की परवाह किए बिना constant time रहता है, जबकि index से एक LinkedList access करना ज़्यादा elements जुड़ने पर धीमा होता जाता है।

उदाहरण: Big O Intuition

java
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
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Random access के लिए LinkedList चुनना, जब ArrayList get(index) के लिए तेज़ है।
  2. एक array उपयोग करना जब size पहले से पता नहीं।
  3. Operations की cost नज़रअंदाज़ करना, जैसे एक list पर contains() का O(n) होना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.