Skip to content
Related Articles

Related Articles

Improve Article

Convert LinkedList to String in Java

  • Last Updated : 26 Mar, 2021

A LinkedList is a linear data structure, in which the elements are not stored at contiguous memory locations.

For converting a linked list to a string we need to traverse the linked list and after that, we need to append the element of the linked list to the string variable.

We can use String class, StringBuilder, or StringBuffer for converting a LinkedList to string.

For appending string we can use the “+” operator for the String class and append method for StringBuffer and StringBuilder.

Example 1:



Java




// Java program to Convert LinkedList to String in Java
 
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
 
class GFG {
    public static void main(String[] args)
    {
 
        // creating a linkedlist
        LinkedList<Integer> list = new LinkedList<>();
 
        // adding elements to linkedlist
        list.add(100);
        list.add(200);
        list.add(300);
        list.add(400);
 
        // By using String Class
        String s = "";
 
        // using iterator for traversing a linkedllist
        Iterator<Integer> iterator = list.iterator();
 
        while (iterator.hasNext()) {
            // appending using "+" operator
            s = s + iterator.next() + " ";
        }
 
        // Displaying output
        System.out.println(s);
 
        // By using String Buffer
        StringBuffer stringBuffer = new StringBuffer();
 
        // using enhanced for loop for traversing a
        // linkedlist
        for (Integer integer : list) {
           
            // using append method for appending string
            stringBuffer.append(integer).append(" ");
        }
 
        // displaying output
        System.out.println(stringBuffer);
 
        // By using String Builder
        StringBuilder stringBuilder = new StringBuilder();
 
        // using ListIterator for traversing a linked list
        ListIterator<Integer> listIterator
            = list.listIterator();
 
        while (listIterator.hasNext()) {
           
            // using append method for appending string
            stringBuilder.append(listIterator.next())
                .append(" ");
        }
 
        // displaying output
        System.out.println(stringBuilder);
    }
}

 
 

Output
100 200 300 400 
100 200 300 400 
100 200 300 400 

 

Example 2:

 

In this example, we will use a user-defined class

 

Java




// Java program to Convert LinkedList to String in Java
 
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
 
class friendsDetail {
 
    // class field
    private String name;
    private String nickName;
    private int id;
 
    // parameterised constructor
    public friendsDetail(String name, String nickName,
                         int id)
    {
        this.name = name;
        this.nickName = nickName;
        this.id = id;
    }
 
    // getter for name
    public String getName() { return name; }
 
    // setter for name
    public void setName(String name) { this.name = name; }
 
    // getter for marks
    public int getID() { return id; }
 
    // setter for marks
    public void setID(int id) { this.id = id; }
 
    // getter for nickname
    public String getNickName() { return nickName; }
 
    // setter for nickname
    public void setNickName(String nickName)
    {
        this.nickName = nickName;
    }
 
    //@Override toString method
    public String toString()
    {
        return "name='" + name + '\'' + ", nickName='"
            + nickName + '\'' + ", ID=" + id;
    }
}
 
class Main {
    public static void main(String[] args)
    {
 
        LinkedList<friendsDetail> list = new LinkedList<>();
 
        // adding elements to linked list
        list.add(
            new friendsDetail("Ridhi", "Goda", 202199));
        list.add(
            new friendsDetail("Ananya", "Natki", 202195));
        list.add(new friendsDetail("Ankit", "Chota Don",
                                   202192));
        list.add(
            new friendsDetail("Suraj", "Bhindi", 202147));
        list.add(
            new friendsDetail("Sarthak", "Nagin", 202178));
        list.add(new friendsDetail("Sonika", "Chipkali",
                                   202167));
        list.add(new friendsDetail("Shishya", "Gorilla",
                                   202157));
 
        System.out.println("Using String Class\n");
 
        // using string class
        String s = "";
 
        // using iterator for traversing a linked list
        Iterator<friendsDetail> itr = list.iterator();
 
        while (itr.hasNext()) {
            // appending atring using "+" operator
            s = s + itr.next() + "\n";
        }
 
        // Displaying output
        System.out.println(s);
 
        System.out.println("\nUsing String Buffer\n");
 
        // using string buffer
        StringBuffer stringBuffer = new StringBuffer();
 
        // using enhanced for loop for linked list traversal
        for (friendsDetail friendmarks : list) {
            // using append method for appending
            stringBuffer.append(friendmarks).append("\n");
        }
 
        // displaying output
        System.out.println(stringBuffer);
 
        // using string builder
        System.out.println("\nUsing String Builder\n");
 
        // using list iterator for traversing a linked list
        ListIterator<friendsDetail> itr2
            = list.listIterator();
 
        StringBuilder stringBuilder = new StringBuilder();
 
        while (itr2.hasNext()) {
            // using append method for appending
            stringBuilder.append(itr2.next()).append("\n");
        }
 
        // displaying output
        System.out.println(stringBuilder);
    }
}

 
 

Output
Using String Class

name='Ridhi', nickName='Goda', ID=202199
name='Ananya', nickName='Natki', ID=202195
name='Ankit', nickName='Chota Don', ID=202192
name='Suraj', nickName='Bhindi', ID=202147
name='Sarthak', nickName='Nagin', ID=202178
name='Sonika', nickName='Chipkali', ID=202167
name='Shishya', nickName='Gorilla', ID=202157


Using String Buffer

name='Ridhi', nickName='Goda', ID=202199
name='Ananya', nickName='Natki', ID=202195
name='Ankit', nickName='Chota Don', ID=202192
name='Suraj', nickName='Bhindi', ID=202147
name='Sarthak', nickName='Nagin', ID=202178
name='Sonika', nickName='Chipkali', ID=202167
name='Shishya', nickName='Gorilla', ID=202157


Using String Builder

name='Ridhi', nickName='Goda', ID=202199
name='Ananya', nickName='Natki', ID=202195
name='Ankit', nickName='Chota Don', ID=202192
name='Suraj', nickName='Bhindi', ID=202147
name='Sarthak', nickName='Nagin', ID=202178
name='Sonika', nickName='Chipkali', ID=202167
name='Shishya', nickName='Gorilla', ID=202157

 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :