How do I retrieve particular object from LinkedList?

The example show you how to retrieve particular object from LinkedList using the indexOf() method.

package org.kodejava.util;

import java.util.List;
import java.util.LinkedList;

public class LinkedListIndexOf {
    public static void main(String[] args) {
        List<String> names = new LinkedList<>();
        names.add("Alice");
        names.add("Bob");
        names.add("Carol");
        names.add("Mallory");

        // Search for Carol using the indexOf method. This method
        // returns the index of the object when found. If not found
        // -1 will be returned.
        int index = names.indexOf("Carol");
        System.out.println("Index = " + index);

        // We can check to see if the index returned is in the range
        // of the LinkedList element size.
        if (index > 0 && index < names.size()) {
            String name = names.get(index);
            System.out.println("Name  = " + name);
        }
    }
}

The program prints the following output:

Index = 2
Name  = Carol

How do I iterate LinkedList elements using ListIterator?

This example show you how to use the ListIterator interface to iterate elements of a LinkedList.

package org.kodejava.util;

import java.util.List;
import java.util.LinkedList;
import java.util.ListIterator;

public class LinkedListListIterator {
    public static void main(String[] args) {
        List<String> grades = new LinkedList<>();
        grades.add("A");
        grades.add("B");
        grades.add("C");
        grades.add("D");
        grades.add("E");

        // Retrieves object from LinkedList using the ListIterator
        // interface.
        ListIterator<String> iterator = grades.listIterator();
        while (iterator.hasNext()) {
            System.out.println("Grades: " + iterator.next());
            System.out.println("  hasPrevious  : " + iterator.hasPrevious());
            System.out.println("  hasNext      : " + iterator.hasNext());
            System.out.println("  previousIndex: " + iterator.previousIndex());
            System.out.println("  nextIndex    : " + iterator.nextIndex());
        }
    }
}

The program will print the following output:

Grades: A
  hasPrevious  : true
  hasNext      : true
  previousIndex: 0
  nextIndex    : 1
Grades: B
  hasPrevious  : true
  hasNext      : true
  previousIndex: 1
  nextIndex    : 2
Grades: C
  hasPrevious  : true
  hasNext      : true
  previousIndex: 2
  nextIndex    : 3
Grades: D
  hasPrevious  : true
  hasNext      : true
  previousIndex: 3
  nextIndex    : 4
Grades: E
  hasPrevious  : true
  hasNext      : false
  previousIndex: 4
  nextIndex    : 5

How do I iterate LinkedList elements using Iterator?

This example show you how to use the Iterator interface to iterate elements of a LinkedList.

package org.kodejava.util;

import java.util.List;
import java.util.LinkedList;
import java.util.Iterator;

public class LinkedListIterator {
    public static void main(String[] args) {
        List<String> grades = new LinkedList<>();
        grades.add("A");
        grades.add("B");
        grades.add("C");
        grades.add("D");
        grades.add("E");

        // Iterates elements in LinkedList using Iterator.
        Iterator<String> iterator = grades.iterator();
        while (iterator.hasNext()) {
            System.out.println("Grade: " + iterator.next());
        }
    }
}

This program prints the following output:

Grade: A
Grade: B
Grade: C
Grade: D
Grade: E

How do I create a LinkedList?

A linked list is a fundamental data structure in programming. It can store data and a references to the next and/or to the previous node. There can be a singly linked list, a doubly linked list and a circularly linked list.

The code below show you how to use the java.util.LinkedList class to create a linked list. In the example we create a LinkedList to store a string object using the add(Object o) method. After create the list we iterate the list and print out the contents.

package org.kodejava.util;

import java.util.List;
import java.util.LinkedList;

public class LinkedListCreate {
    public static void main(String[] args) {
        // Creates a new instance of java.util.LinkedList and
        // adds five string object into the list.
        List<String> grades = new LinkedList<>();
        grades.add("A");
        grades.add("B");
        grades.add("C");
        grades.add("E");
        grades.add("F");

        // Iterates the LinkedList object using the for each
        // statement.
        for (String grade : grades) {
            System.out.println("Grade: " + grade);
        }
    }
}

This program will produce the following output:

Grade: A
Grade: B
Grade: C
Grade: E
Grade: F

How do I add selection listener to JTree?

This example shows you how to use the TreeSelectionListener to add a tree selection listener to the JTree component. In the listener method below you can see how to get the selected path and print out the selected path to the console.

package org.kodejava.swing;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import java.awt.*;

public class JTreeSelectionListenerDemo extends JFrame {

    public JTreeSelectionListenerDemo() throws HeadlessException {
        initializeUI();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(
                () -> new JTreeSelectionListenerDemo().setVisible(true));
    }

    private void initializeUI() {
        setSize(500, 500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
        DefaultMutableTreeNode chapterOne = new DefaultMutableTreeNode("Chapter One");
        DefaultMutableTreeNode one = new DefaultMutableTreeNode("1.1");
        DefaultMutableTreeNode two = new DefaultMutableTreeNode("1.2");
        DefaultMutableTreeNode three = new DefaultMutableTreeNode("1.3");

        root.add(chapterOne);
        chapterOne.add(one);
        chapterOne.add(two);
        chapterOne.add(three);

        JTree tree = new JTree(root);
        tree.addTreeSelectionListener(e -> {
            TreePath path = e.getPath();
            int pathCount = path.getPathCount();

            for (int i = 0; i < pathCount; i++) {
                System.out.print(path.getPathComponent(i).toString());
                if (i + 1 != pathCount) {
                    System.out.print(" | ");
                }
            }
            System.out.println();
        });

        JScrollPane pane = new JScrollPane(tree);
        pane.setPreferredSize(new Dimension(200, 400));

        getContentPane().add(pane);
    }
}