Convertir LinkedList a String en Java

Una LinkedList es una estructura de datos lineal, en la que los elementos no se almacenan en ubicaciones de memoria contiguas.

Para convertir una lista vinculada en una string, debemos recorrer la lista vinculada y, después de eso, debemos agregar el elemento de la lista vinculada a la variable de string.

Podemos usar la clase String, StringBuilder o StringBuffer para convertir una LinkedList en una string.

Para agregar una string, podemos usar el operador «+» para la clase String y el método de agregar para StringBuffer y StringBuilder.

Ejemplo 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 linkedlist
        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);
    }
}
Producción

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

Ejemplo 2: En este ejemplo, usaremos una clase definida por el usuario

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 string 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);
    }
}
Producción

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

Publicación traducida automáticamente

Artículo escrito por le0 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *