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

Java का List Interface

List interface एक ordered collection represent करता है जो numeric index से accessed होता है, और Java के Collections Framework में Set और Map के साथ तीन core interfaces में से एक है।
Syntax
java
List<Type> list = new ArrayList<>();
list.add(item);
list.get(index);

What is the List Interface?

List interface elements का एक ordered collection represent करता है जो उनके numeric index से accessed हो सकते हैं, और Set और Map के साथ तीन core interfaces में से एक है जो Java के Collections Framework बनाते हैं।

उदाहरण: What is the List Interface?

java
import java.util.List;
import java.util.ArrayList;
public class Main {
	public static void main(String[] args) {
		List<String> names = new ArrayList<>();
		names.add("Alice");
		System.out.println(names.get(0)); // ordered, accessible by index
	}
}

List Implementations

ArrayList और LinkedList List interface के दो सबसे आम implementations हैं: ArrayList fast index access के लिए elements को एक resizable array में store करता है, जबकि LinkedList fast insertion और removal के लिए उन्हें linked nodes की एक chain के रूप में store करता है।

उदाहरण: List Implementations

java
// Import java.util.List so it can be used by its short name
import java.util.List;
// Import java.util.ArrayList so it can be used by its short name
import java.util.ArrayList;
import java.util.LinkedList;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new ArrayList object and store it in arrayList
		List<String> arrayList = new ArrayList<>();
		// Create a new LinkedList object and store it in linkedList
		List<String> linkedList = new LinkedList<>();
		arrayList.add("fast index access");
		linkedList.add("chain of nodes");
		// Print a line to the console
		System.out.println(arrayList);
		// Print a line to the console
		System.out.println(linkedList);
	}
}

Common List Methods

List interface add, remove, get, set, size, और contains जैसे common methods define करता है, जो सभी identically काम करते हैं चाहे underneath असल में कौन सा concrete List implementation उपयोग हो रहा हो।

उदाहरण: Common List Methods

java
// Import java.util.List so it can be used by its short name
import java.util.List;
// Import java.util.ArrayList so it can be used by its short name
import java.util.ArrayList;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new ArrayList object and store it in names
		List<String> names = new ArrayList<>();
		names.add("Alice");
		names.add("Bob");
		// Print a line to the console
		System.out.println(names.get(0));
		// Print a line to the console
		System.out.println(names.contains("Bob"));
		System.out.println(names.size());
		names.remove("Alice");
		System.out.println(names);
	}
}

Ordered and Indexed Access

एक List हमेशा elements insert होने का order संरक्षित रखता है और किसी भी element को इसके zero-based index से get और set उपयोग करके सीधे retrieve या replace होने देता है, एक Set के विपरीत जिसमें position की कोई concept नहीं।

उदाहरण: Ordered and Indexed Access

java
import java.util.List;
import java.util.ArrayList;
public class Main {
	public static void main(String[] args) {
		List<String> names = new ArrayList<>();
		names.add("Alice");
		names.add("Bob");
		names.set(0, "Charlie"); // replace by index
		System.out.println(names.get(0));
	}
}

Allowing Duplicates

एक List explicitly duplicate values को store होने देता है, हर occurrence को इसके अपने index पर एक अलग separate entry के रूप में रखते हुए, जो key behavioral difference है जो इसे Set interface से distinguish करता है।

उदाहरण: Allowing Duplicates

java
import java.util.List;
import java.util.ArrayList;
public class Main {
	public static void main(String[] args) {
		List<String> names = new ArrayList<>();
		names.add("Alice");
		names.add("Alice"); // duplicates allowed, kept as separate entries
		System.out.println(names);
	}
}
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. list.remove(int) call करना जब आपका मतलब एक Integer object हटाना था, इसलिए यह index से हटाता है।
  2. List.of() उपयोग करना और फिर इसे modify करना, जो UnsupportedOperationException throw करता है।
  3. Size से आगे एक index पढ़ना, जो IndexOutOfBoundsException throw करता है।

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.