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.

JavaCli Tooling

Code

snippet
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

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:`.