How do I create a List in iText?

You can create a list in iText using the com.itextpdf.text.List. This class represent a list. The list item is created using the com.itextpdf.text.ListItem. You can create an ordered list or unordered list. To create ordered list pass the List.ORDERED as the parameter to class List. To create an unordered list pass the List.UNORDERED.

Let’s see an example below:

package org.kodejava.itextpdf;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class ListDemo {
    public static void main(String[] args) {
        Document document = new Document();
        try {
            PdfWriter.getInstance(document, new FileOutputStream("ListDemo.pdf"));
            document.open();

            List ordered = new List(List.ORDERED);
            ordered.add(new ListItem("Item 1"));
            ordered.add(new ListItem("Item 2"));
            ordered.add(new ListItem("Item 3"));
            document.add(ordered);

            List unordered = new List(List.UNORDERED);
            unordered.add(new ListItem("Item 1"));
            unordered.add(new ListItem("Item 2"));
            unordered.add(new ListItem("Item 3"));
            document.add(unordered);
        } catch (DocumentException | FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
    }
}

Maven Dependencies

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13.3</version>
</dependency>

Maven Central

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