This example shows how to print LinkedList elements in Java. This example also shows how to print LinkedList of custom class objects using the toString method.
How to print LinkedList elements in Java?
There are several ways using which you can print LinkedList object in Java as given below.
1. Using for loop or enhanced for loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.util.LinkedList; public class PrintLinkedListExample { public static void main(String[] args) { LinkedList<String> linkedListColors = new LinkedList<String>(); linkedListColors.add("Red"); linkedListColors.add("Green"); linkedListColors.add("Blue"); linkedListColors.add("Yellow"); System.out.println("Printing elements using for loop"); //using for loop for(int i = 0; i < linkedListColors.size(); i++) System.out.println( linkedListColors.get(i) ); System.out.println("Printing elements using enhanced for loop"); //using enhanced for loop for(String strColor : linkedListColors) System.out.println(strColor); } } |
Output
1 2 3 4 5 6 7 8 9 10 |
Printing elements using for loop Red Green Blue Yellow Printing elements using enhanced for loop Red Green Blue Yellow |
2. Using Java 8 Stream
If you are using Java version 8 or later, you can use the stream to print linked list elements as given below.
1 2 3 4 5 6 7 8 |
LinkedList<String> linkedListColors = new LinkedList<String>(); linkedListColors.add("Red"); linkedListColors.add("Green"); linkedListColors.add("Blue"); linkedListColors.add("Yellow"); linkedListColors.stream().forEach(System.out::println); |
Output
1 2 3 4 |
Red Green Blue Yellow |
3. Using the Arrays class
First, convert the LinkedList object to an array using the toArray
method of the LinkedList class and then print the array using the toString
method of the Arrays class as given below.
1 2 3 4 5 6 7 8 |
LinkedList<String> linkedListColors = new LinkedList<String>(); linkedListColors.add("Red"); linkedListColors.add("Green"); linkedListColors.add("Blue"); linkedListColors.add("Yellow"); System.out.println( Arrays.toString( linkedListColors.toArray() ) ); |
Output
1 |
[Red, Green, Blue, Yellow] |
As you may have noticed from the output, the linked list elements are enclosed in the square brackets. If you want to remove them, you can use the replace method of the String class as given below.
1 2 3 |
System.out.println( Arrays.toString( linkedListColors.toArray() ).replace("[", "").replace("]", "") ); |
Output
1 |
Red, Green, Blue, Yellow |
How to print LinkedList of custom class objects?
If the LinkedList you want to print contains objects of a custom class, then the class must implement the toString
method.
The toString
method should return a string representation of the object and it gets called automatically when the object is printed using the System.out.println statement.
The below example shows a custom Emp class that has overridden the toString
method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import java.util.LinkedList; class Emp{ private Integer id; private String name; public Emp(Integer id, String name){ this.id = id; this.name = name; } public String toString(){ return "[" + this.id + " => " + this.name + "]"; } } public class PrintLinkedListExample { public static void main(String[] args) { LinkedList<Emp> linkedListEmployees = new LinkedList<Emp>(); linkedListEmployees.add(new Emp(1, "Ryan")); linkedListEmployees.add(new Emp(2, "John")); linkedListEmployees.add(new Emp(3, "Emily")); linkedListEmployees.add(new Emp(4, "Maria")); for(Emp employee : linkedListEmployees) System.out.println( employee ); } } |
Output
1 2 3 4 |
[1 => Ryan] [2 => John] [3 => Emily] [4 => Maria] |
This example is a part of the Java LinkedList tutorial with examples.
Please let me know your views in the comments section below.