Java LinkedList toString() Method with Examples

In the previous article, we have discussed about Java LinkedList pop() Method with Examples

In this article we are going to see the use of Java LinkedList toString() method along with suitable examples.

Java LinkedList toString() Method with Examples

This java.util.LinkedList.toString() method is used to convert a LinkedList into a String. Means now all the elements in the LinkedList will be represented as String.

It returns the same LinkedList elements but in the form of String.

Syntax:

LinkedListName.toString()

Where,

  • LinkedListName refers to the name of your LinkedList.

Let’s see different examples to understand it more clearly.

Method-1: Java LinkedList toString() Method – Example with String Type LinkedList

Approach:

  • Create a new LinkedList of type String.
  • Add string elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Then convert the LinkedList to String using toString() method.
  • Print the elements.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a LinkedList of string datatype
        LinkedList<String> l = new LinkedList<String>();
        // Adding some elements to the LinkedList
        l.add("rose");
        l.add("lily");
        l.add("mlygold");
        l.add("jasmine");
        l.add("tulip");
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList are: "+l);
        // convert LinkedList to String
        String str = l.toString();
        // Prints the elements
        System.out.println("String elements are: " +str);
    }
}

Output:

The elements of LinkedList are: [rose, lily, mlygold, jasmine, tulip]
String elements are: [rose, lily, mlygold, jasmine, tulip]

Method-2: Java LinkedList toString() Method – Example with Integer Type LinkedList

Approach:

  • Create a new LinkedList of type Integer.
  • Add integer elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Then convert the LinkedList to String using toString() method.
  • Print the elements.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {        
        // Create a LinkedList of Integer datatype
        LinkedList<Integer> l = new LinkedList<Integer>();
        // Adding some elements to the LinkedList
        l.add(2);
        l.add(52);
        l.add(13);
        l.add(17);
        l.add(1);
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList are: "+l);
        // convert LinkedList to String
        String str = l.toString();
        // Prints the elements
        System.out.println("String elements are: " +str);
    }
}

Output:

The elements of LinkedList are: [2, 52, 13, 17, 1]
String elements are: [2, 52, 13, 17, 1]

Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language.

Related Java Programs: