Java की Sequenced Collections
In this page:
SequencedCollection<Type> collection = new ArrayList<>();
collection.getFirst();
collection.getLast();
collection.reversed();
What are Sequenced Collections?
Sequenced Collections, जो Java 21 में जोड़ी गईं, किसी ordered collection के पहले और आखिरी elements को access करने का एक समान तरीका introduce करती हैं -- getFirst, getLast, addFirst, और addLast जैसे methods अब List, Deque, और LinkedHashSet में consistently काम करते हैं।
उदाहरण: What are Sequenced Collections?
import java.util.*;
public class Main {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>(List.of(1, 2, 3));
System.out.println(list.getFirst() + " " + list.getLast()); // uniform across List, Deque, LinkedHashSet
}
}
Login to try C/C++/Java/PHP code in the editor
SequencedCollection Interface
SequencedCollection एक नया interface है जिसे अब List और Deque extend करते हैं, जो methods का एक consistent set define करता है -- getFirst, getLast, addFirst, addLast, removeFirst, removeLast, और reversed -- किसी भी ऐसे collection के लिए जिसका अच्छी तरह परिभाषित encounter order हो।
उदाहरण: SequencedCollection Interface
// Import java.util.* so it can be used by its short name
import java.util.*;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new ArrayDeque object and store it in deque
Deque<String> deque = new ArrayDeque<>(List.of("a", "b", "c"));
deque.addFirst("start");
deque.addLast("end");
// Print a line to the console
System.out.println(deque);
}
}
Login to try C/C++/Java/PHP code in the editor
SequencedSet and SequencedMap
SequencedSet Set और SequencedCollection दोनों को extend करता है, जिसे LinkedHashSet और TreeSet implement करते हैं, जबकि SequencedMap LinkedHashMap और TreeMap जैसे ordered maps में firstEntry, lastEntry, और reversed methods जोड़ता है।
उदाहरण: SequencedSet and SequencedMap
// Import java.util.* so it can be used by its short name
import java.util.*;
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Create a new LinkedHashMap object and store it in map
LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
map.put("a", 1); map.put("b", 2);
// Print a line to the console
System.out.println(map.firstEntry() + " " + map.lastEntry());
}
}
Login to try C/C++/Java/PHP code in the editor
Reversed Views
reversed() method एक अलग copy के बजाय original collection का एक reverse-ordered view return करता है, यानी original collection में किए गए बदलाव reversed view पर iterate करते समय तुरंत दिखाई देते हैं।
उदाहरण: Reversed Views
import java.util.*;
public class Main {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>(List.of(1, 2, 3));
List<Integer> reversed = list.reversed(); // a view, not a copy
list.add(4);
System.out.println(reversed); // sees the update
}
}
Login to try C/C++/Java/PHP code in the editor
Retrofitted Existing Collections
ArrayList, ArrayDeque, LinkedHashSet, TreeSet, LinkedHashMap, और TreeMap जैसी मौजूदा collection classes को नए sequenced interfaces implement करने के लिए retrofit किया गया, इसलिए यह consistent first/last API बिना किसी नए collection types की ज़रूरत के उपलब्ध हो गई।
उदाहरण: Retrofitted Existing Collections
import java.util.*;
public class Main {
public static void main(String[] args) {
TreeSet<Integer> set = new TreeSet<>(List.of(3, 1, 2)); // retrofitted, no new collection type needed
System.out.println(set.getFirst() + " " + set.getLast());
}
}
Login to try C/C++/Java/PHP code in the editor
- किसी खाली collection पर
getFirst()याgetLast()call करना, जोNoSuchElementExceptionफेंकता है। - यह सोचना कि
reversed()एक स्वतंत्र copy देता है, जबकि यह एक view है, इसलिए एक में बदलाव दूसरे में दिखते हैं। - यह उम्मीद करना कि
HashSetयाHashMapमेंgetFirst()होगा, जबकि सिर्फLinkedHashSetऔरTreeSetजैसी ordered collections sequenced हैं।
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: