Java का List Interface
In this page:
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?
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
}
}
Login to try C/C++/Java/PHP code in the editor
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
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
Common List Methods
List interface add, remove, get, set, size, और contains जैसे common methods define करता है, जो सभी identically काम करते हैं चाहे underneath असल में कौन सा concrete List implementation उपयोग हो रहा हो।
उदाहरण: Common List Methods
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
Ordered and Indexed Access
एक List हमेशा elements insert होने का order संरक्षित रखता है और किसी भी element को इसके zero-based index से get और set उपयोग करके सीधे retrieve या replace होने देता है, एक Set के विपरीत जिसमें position की कोई concept नहीं।
उदाहरण: Ordered and Indexed Access
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));
}
}
Login to try C/C++/Java/PHP code in the editor
Allowing Duplicates
एक List explicitly duplicate values को store होने देता है, हर occurrence को इसके अपने index पर एक अलग separate entry के रूप में रखते हुए, जो key behavioral difference है जो इसे Set interface से distinguish करता है।
उदाहरण: Allowing Duplicates
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);
}
}
Login to try C/C++/Java/PHP code in the editor
list.remove(int)call करना जब आपका मतलब एकIntegerobject हटाना था, इसलिए यह index से हटाता है।List.of()उपयोग करना और फिर इसे modify करना, जोUnsupportedOperationExceptionthrow करता है।- Size से आगे एक index पढ़ना, जो
IndexOutOfBoundsExceptionthrow करता है।
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