Mr Baumgarten

Computer Science teacher and self confessed geek

Java: Linked lists

For some bizarre reason, the built in LinkedList class in Java does not actually implement a pure Linked List structure. If you require an actual linked list with .next() style functionality, you'll need to roll your own.

Using the same personDemo class we used in the HashMapDemo.

package ch.isl.beyondbasics;
import java.util.LinkedList;

public class LinkedListDemo {
    public static void main(String[] args) {
        LinkedList<PersonDemo> people = new LinkedList<PersonDemo>();

        PersonDemo me = new PersonDemo("Paul","Baumgarten","");
        people.push(new PersonDemo("Bruce","Wayne",""));
        people.push(new PersonDemo("Clark","Kent",""));
        people.push(new PersonDemo("Diana","Prince",""));

        while (! people.isEmpty() ) {
            PersonDemo person = people.pop();
            System.out.println( person.toString() );

To create your own actual LinkedList, see my teaching notes for the DP Computer Science course introducing the LinkedList.

More information on the built in Java object: