Ads by ProfitSence
Close

Cómo convertir una arraylist en una cadena java

Last Updated on domingo 9º oct. 2022

list to string java

La clase ArrayList es una matriz de tamaño variable, que se puede encontrar en el paquete java.util.

Usando + Operador

  • La forma más fácil de convertir un ArrayList a String es usar el operador plus (+)
			
					import java.util.ArrayList;
import java.util.List;

class Example {
  public static void main(String[] args) {
    List<String> countries = new ArrayList<>();
    countries.add("Germany");
    countries.add("Italy");
    countries.add("Spain");
    System.out.println(countries);
    StringBuilder str = new StringBuilder();
    for (String country: countries) {
      str.append(country);
      str.append(" ");
    }
    System.out.println(str);
  }
}

			
	

Salida

			
					[Germany, Italy, Spain]
Germany,Italy,Spain,

			
	

arraylist to string

Usando append() Método

  • Podemos usar el método append() de la clase StringBuilder para convertir ArrayList en cadena.
			
					import java.util.ArrayList;
import java.util.List;

class Example {
  public static void main(String[] args) {
    List<String> countries = new ArrayList<>();
    countries.add("Germany");
    countries.add("Italy");
    countries.add("Spain");
    System.out.println(countries);
    StringBuilder str = new StringBuilder();
    for (String country: countries) {
      str.append(country);
      str.append(" ");
    }
    System.out.println(str);
  }
}

			
	

Salida

			
					[Germany, Italy, Spain]
Germany Italy Spain

			
	

convert arraylist to string java

Usando join() Método

  • join() Método devuelve una cadena y se puede utilizar para convertir ArrayList a String en Java.
			
					import java.util.ArrayList;
import java.util.List;

class Example {
  public static void main(String[] args) {
    List<String> countries = new ArrayList<>();
    countries.add("Germany");
    countries.add("Italy");
    countries.add("Spain");
    System.out.println(countries);
    String str = String.join(",", countries);
    System.out.println(str);
  }
}

			
	

Salida

			
					[Germany, Italy, Spain]
Germany,Italy,Spain

			
	

Usando replaceAll() Método

  • Con replaceAll() Método para sustituir corchetes de la Lista y luego convertir todos los elementos en String Usando el toString() Método.
  • Difícil de usar
			
					import java.util.ArrayList;
import java.util.List;

class Example {
  public static void main(String[] args) {
    List<String> countries = new ArrayList<>();
    countries.add("Germany");
    countries.add("Italy");
    countries.add("Spain");
    System.out.println(countries);
    String str = countries.toString().replaceAll("\\[|\\]", "").replaceAll(", ", ", ");
    System.out.println(str);
  }
}

			
	

Salida

			
					[Germany, Italy, Spain]
Germany, Italy, Spain

			
	

Usando joining() Método

  • Puede utilizar el método joining() de la clase Collectors para recopilar todos los elementos en un solo objeto String.
  • Esto añadido en la versión de Java 8
			
					import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

class Example {
  public static void main(String[] args) {
    List<String> countries = new ArrayList<>();
    countries.add("Germany");
    countries.add("Italy");
    countries.add("Spain");
    System.out.println(countries);
    String str = Arrays.asList(countries).stream()
      .map(Object::toString)
      .collect(Collectors.joining(", "));
    System.out.println(str);
  }
}

			
	

Salida

			
					[Germany, Italy, Spain]
[Germany, Italy, Spain]

			
	

Usando deepToString() Método

			
					import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

class Example {
  public static void main(String[] args) {
    List<String> countries = new ArrayList<>();
    countries.add("Germany");
    countries.add("Italy");
    countries.add("Spain");
    System.out.println(countries);
    String str = Arrays.deepToString(countries.toArray());
    System.out.println(str);
  }
}

			
	

Salida

			
					[Germany, Italy, Spain]
[Germany, Italy, Spain]