SequencedCollection AddFirst And AddLast
Insert at both ends with addFirst and addLast from the JDK 21 SequencedCollection interface, then poll from either side without index arithmetic.
Code
import java.util.ArrayList;
import java.util.List;
import java.util.SequencedCollection;
public class JobLane {
public static void main(String[] args) {
// ArrayList 也实现了 SequencedCollection:两端操作直接可用
SequencedCollection<String> lane = new ArrayList<>();
lane.addLast("batch-1"); // 常规入队:排在末尾
lane.addLast("batch-2");
lane.addFirst("urgent-0"); // 插队:排在最前
System.out.println("lane: " + lane);
System.out.println("first: " + lane.getFirst());
System.out.println("last: " + lane.getLast());
// removeLast 从末尾取走任务;removeFirst 从队头取走加急任务
System.out.println("pulled head: " + lane.removeFirst());
System.out.println("pulled tail: " + lane.removeLast());
System.out.println("remaining: " + lane);
}
}The steps in order
The working surface is the class JobLane and the function main — everything else is wiring.
Watch out for
- The dependency table has no rows; the language runtime provides every capability used.
Applying it
The primary signature is public static void main(String[] args)
The run prints lane:, first:, last: — each prefix names the step that produced it.
Statements that matter
SequencedCollection<String> lane = new ArrayList<>();
lane.addLast("batch-1"); // 常规入队:排在末尾
lane.addLast("batch-2");
lane.addFirst("urgent-0"); // 插队:排在最前
System.out.println("lane: " + lane);
Interactive Tools for This Topic
Git Conventional Commit BuilderBuild and validate Conventional Commits v1.0.0 messages with type, scope, breaking markers, 72-char subject counter, body, footers, and git CLI command.Glob Pattern TesterCompile glob patterns with *, **, ?, character classes, and braces into regex and test them against a list of candidate file paths.Tar Archive Command BuilderBuild safe tar create, extract, and list commands for gzip, xz, zstd, and bzip2 archives with excludes, target directory (-C), and flag explanations.
FAQ
- What does SequencedCollection AddFirst And AddLast print when run?
- Run it as a complete program — the output lines carry labels such as `lane:`, `first:`, `last:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.