Java में Collections Framework एक powerful tool है जिससे हम data (objects) को store और manage करते हैं। इसका इस्तेमाल तब होता है जब हमें एक ही type के कई values एक साथ संभालने होते हैं।
Interface | Description |
---|---|
List | Ordered collection, duplicate values allow करता है |
Set | Unordered collection, duplicate values allow नहीं करता |
Map | Key-Value pairs में data store करता है |
import java.util.ArrayList;
public class Example {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("Amit");
names.add("Rahul");
names.add("Amit");
System.out.println(names);
}
}
import java.util.HashSet;
public class Example {
public static void main(String[] args) {
HashSet<String> items = new HashSet<>();
items.add("Pen");
items.add("Book");
items.add("Pen");
System.out.println(items);
}
}
import java.util.HashMap;
public class Example {
public static void main(String[] args) {
HashMap<Integer, String> students = new HashMap<>();
students.put(1, "Amit");
students.put(2, "Suman");
students.put(3, "Amit");
System.out.println(students);
}
}
Collection Type | Class | Duplicates Allowed? | Order Maintained? |
---|---|---|---|
List | ArrayList | Yes | Yes |
Set | HashSet | No | No |
Map | HashMap | Values: Yes, Keys: No | No |
Java Collections Framework Java developers के लिए एक ज़रूरी टूल है। List, Set, और Map जैसे structures आपको data को efficiently manage करने में मदद करते हैं। अगर आप Java सीख रहे हैं, तो collections समझना बहुत जरूरी है।