Java Devs aye? Before Java 21, there was no common way to access the first or last element in a List, Set, or Map. can you remember? you either converted it to a list... or wrote a custom utility. Ugly, right? Java says enough! Now Java lets you get the first and last element without a hack. They introduced SequencedCollection, SequencedSet, and SequencedMap. List- SequencedCollection<String> list = new ArrayList<>(List.of("A", "B", "C")); list.getFirst(); // A list.getLast(); // C list.reversed(); // [C, B, A] Set- SequencedSet<Integer> ids = new LinkedHashSet<>(List.of(1,2,3)); System.out.println(ids.getFirst()); // 1 System.out.println(ids.getLast()); // 3 ids.addFirst(0); System.out.println(ids); // [0,1,2,3] Map - SequencedMap<String,Integer> map = new LinkedHashMap<>(); map.put("A",1); map.putLast("B",2); map.putFirst("Z",0); System.out.println(map.firstEntry()); // Z=0 System.out.println(map.lastEntry()); // B=2 map.reversed().forEach((k,v)-> System.out.println(k+"="+v)); Small feature. Big deal. Finally, order-aware collections are part of the language. So java be like ->
Helpful 👏
Very helpful
Very Helpful
Nice
Thankyou for the knowledge