What's New in Java 21 to 25: Compact Source Files and Sequenced Collections
A whole Java 25 program with no class, no static and no String[] args: void main and IO.println greet Jack, Liam and Kate. Then sequenced collections: addFirst puts Lucy at the front, getLast reads Paul, and a LinkedHashMap answers firstEntry Kate=88.
What you will learn
- Write a compact source file with void main, no class and no static
- Print with IO.println and explain what the compiler adds behind the scenes
- Use addFirst, getFirst, getLast and reversed on an ordered list
- Use putFirst, firstEntry and lastEntry on a SequencedMap, and know which collections have an order
Before you start
- Java Hello World with Maven and the JDK
- Java maps: HashMap, LinkedHashMap and TreeMap
Think of a car's new model: the same car, but the dashboard got a little nicer. Java 21 to 25 added a few changes like that. This tour covers two you'll meet early: compact source files and sequenced collections.
No class, no static
// A compact source file: no class, no static, no String[] args
String school = "StudyEasy";
void main() {
// java.base is imported for you, so List needs no import
List<String> students = List.of("Jack", "Liam", "Kate");
IO.println("Welcome to " + school);
for (String s : students) {
IO.println(greet(s));
}
}
String greet(String name) {
return "Hi " + name + ", your first class starts at 9";
}
Welcome to StudyEasy
Hi Jack, your first class starts at 9
Hi Liam, your first class starts at 9
Hi Kate, your first class starts at 9
This is the whole file, Ex01_CompactHello.java. A String field sits at the top with no class around it. main has no public, no static and no String[] args: just void main(). List needs no import, because the common packages of java.base come in for free. And IO.println replaces System.out.println. Compare the classic Hello World: the import, the class line, the closing brace and every static are gone, and the output is the same.
A compact source file can't have a package line: adding package org.studyeasy.s05l11; gives "compact source file should not have package declaration".
What the compiler writes for you
Java still needs an object to call a method on. When the file compiles, Java creates a final class with the file's name and puts your field and methods inside. Then it creates an object of that class and calls main on it. That's why main doesn't need static.
First, last and reversed
List<String> line = new ArrayList<>(List.of("Ben", "Paul"));
line.addFirst("Lucy"); // goes in position zero
System.out.println("First: " + line.getFirst());
System.out.println("Last: " + line.getLast());
System.out.println("Line: " + line);
System.out.println("Back: " + line.reversed());
First: Lucy
Last: Paul
Line: [Lucy, Ben, Paul]
Back: [Paul, Ben, Lucy]
Since Java 21, collections that keep an order are sequenced. addFirst drops Lucy into position zero, and Ben and Paul slide over. getFirst and getLast read the two ends, and reversed gives the same list back to front. (They also work on a fixed List.of list for reading; see choosing a collection.)
A map has a first and a last too
SequencedMap<String, Integer> marks = new LinkedHashMap<>();
marks.put("Jack", 91);
marks.put("David", 78);
System.out.println("Marks: " + marks);
marks.putFirst("Kate", 88); // a map can do it too
System.out.println("Marks: " + marks);
System.out.println("First: " + marks.firstEntry());
System.out.println("Last: " + marks.lastEntry());
System.out.println("Back: " + marks.reversed());
Marks: {Jack=91, David=78}
Marks: {Kate=88, Jack=91, David=78}
First: Kate=88
Last: David=78
Back: {David=78, Jack=91, Kate=88}
A LinkedHashMap remembers the order you put things in, so first and last make sense for pairs. putFirst puts Kate at the front, firstEntry and lastEntry return the end pairs, and reversed flips the map.
Only collections with an order
| Collection | Has an order? | First and last |
|---|---|---|
| ArrayList, LinkedHashSet, TreeSet | yes | getFirst(), getLast() |
| LinkedHashMap | yes | firstEntry(), lastEntry() |
| HashSet, HashMap | no | none: cannot find symbol |
Same Java, less ceremony. These are small, handy additions, nothing fancy.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Install Java 25 (JDK) and IntelliJ IDEA, Then Run Your First Program
Install JDK 25 (Microsoft Build of OpenJDK) with JAVA_HOME, install IntelliJ IDEA, then point a run configuration at the JDK and the main class and run Hello World.
Use Claude Agent, Copilot or Junie Inside IntelliJ IDEA
Open IntelliJ's AI Chat, pick Claude Agent (the agent behind Claude Code) or Copilot and a model, ask in plain words for Main.java to print the date and time, review the change, and run it.
Java Hello World with Maven: What the JDK Does and What Maven Adds
javac compiles HelloWorld.java into bytecode and java runs it on the JVM. Maven adds one pom.xml, the same folders in every project and one build command. Then a real Maven Hello World in IntelliJ.
Java Variables, Keywords and Naming Rules
A variable is a labelled jar that holds one value at a time. Four small programs show declaring, reassigning, the names Java accepts, the keywords it reserves, and why daysUntilLaunch beats d.
