logo

Konvertera lista till sträng i Java

Ibland måste vi konvertera en lista med tecken till en sträng. En sträng är en sekvens av tecken, så vi kan enkelt bilda en sträng från en teckenuppsättning. Vi har några speciella strängar, till exempel en palindromsträng (en sträng som är samma som den ursprungliga strängen efter att ha vänt om den). För att vända en sträng måste vi konvertera strängen till en char-array för att vända alla tecken i strängen, och efter att ha vänt om char-arrayen måste vi konvertera den till en sträng.

I Java har vi flera sätt genom vilka vi kan konvertera en lista till en sträng är följande:

1. Använda StringBuilder-klassen

Vi har ett mycket enkelt sätt att konvertera en lista till en sträng, d.v.s. genom att använda StringBuilder klass. Vi itererar listan över char som använder for loop och genererar en ny sträng med hjälp av StringBuilder.

Låt oss implementera koden för att konvertera en lista till en sträng genom att använda klassen StringBuilder:

ConvertListToStringExample1.java

 // import required classes and packages if any package javaTpoint.JavaExample; import java.util.ArrayList; import java.util.List; import java.util.Scanner; // create class ConvertListToStringExample1 that will convert user given char list into a string class ConvertListToStringExample1 { // main() method start public static void main(String[] args) { // declare variables and list int size; String str; List charList = new ArrayList(); // create Scanner class object to take input from user Scanner sc = new Scanner(System.in); System.out.println(&apos;Enter size of char list.&apos;); size = sc.nextInt(); for(int i = 0; i <size; i++) { system.out.println('enter char['+i+']: '); charlist.add(sc.next().charat(0)); } close scanner class object sc.close(); display chrlist elements system.out.println('list: ' + charlist); create an of stringbuilder builder="new" stringbuilder(); iterate charlist and append each char to one by for (character ch : charlist) builder.append(ch); convert into string str="builder.toString();" system.out.println('string after converting list: str); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/39/convert-list-string-java.webp" alt="Convert List to String in Java"> <h3>2. Using Joiner class</h3> <p>Like <strong>StringBuilder</strong> , we have one more class, i.e., <strong>Joiner</strong> class, through which we can convert a list into a string. We use the <strong>join()</strong> method of the Joiner class, which is also known as the <strong>Guava</strong> method. The join() method joins the pieces into the specified text as an array and returns the results as a string.</p> <p>Let&apos;s implement the code for converting a list into a string by using the Joiner class:</p> <p>We are using the maven project, so it is required to add the following dependency in our POM.xml file.</p> <pre> com.google.guava guava r05 </pre> <p> <strong>ConvertListToStringExample2.java</strong> </p> <pre> // import required classes and packages if any import java.util.ArrayList; import java.util.List; import java.util.Scanner; import com.google.common.base.Joiner; // create class ConvertListToStringExample2 that will convert user given char list into a string using Joiner class class ConvertListToStringExample2 { // main() method start public static void main(String[] args) { // declare variables and list int size; String str; List charList = new ArrayList(); // create Scanner class object to take input from user Scanner sc = new Scanner(System.in); System.out.println(&apos;Enter size of char list.&apos;); size = sc.nextInt(); for(int i = 0; i <size; i++) { system.out.println('enter char['+i+']: '); charlist.add(sc.next().charat(0)); } close scanner class object sc.close(); display chrlist elements system.out.println('list: ' + charlist); convert list into string by using joiner str="Joiner.on(&apos;&apos;).join(charList);" system.out.println('string after converting list: str); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/39/convert-list-string-java-2.webp" alt="Convert List to String in Java"> <h3>3. Using List.toString(), String.substring() and String.replaceAll() methods</h3> <p>In this way of converting a list into a string, we use three methods of List and String classes. We use three methods to perform the following operations:</p> <ol class="points"> <li>We use the toString() method of the list to convert the list into a string. The return string will also contain opening/closing square brackets and commas.</li> <li>We use the substring() method to get rid of opening/closing square brackets.</li> <li>We use the replaceAll() method to replace all the commas and spaces with blank or null.</li> </ol> <p>Let&apos;s implement the code for converting a list into a string by using the toString(), substring(), and replaceAll() methods:</p> <p> <strong>ConvertListToStringExample3.java</strong> </p> <pre> // import required classes and packages if any package javaTpoint.JavaExample; import java.util.ArrayList; import java.util.List; import java.util.Scanner; // create class ConvertListToStringExample2 that will convert user given char list into a string using toString(), substring() and replaceAll() methods class ConvertListToStringExample3 { // main() method start public static void main(String[] args) { // declare variables and list int size; String str; List charList = new ArrayList(); // create Scanner class object to take input from user Scanner sc = new Scanner(System.in); System.out.println(&apos;Enter size of char list.&apos;); size = sc.nextInt(); for(int i = 0; i <size; 3 i++) { system.out.println('enter char['+i+']: '); charlist.add(sc.next().charat(0)); } close scanner class object sc.close(); display chrlist elements system.out.println('list: ' + charlist); convert list into string by using all the three methods of and str="charList.toString()" .substring(1, * charlist.size() - 1) .replaceall(', ', ''); system.out.println('string after converting list: str); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/39/convert-list-string-java-3.webp" alt="Convert List to String in Java"> <h3>4. Using Collectors in Java</h3> <p>It is the last way to convert a list into a string. We use it rarely because here, we use stream API with collectors, which is available only in Java8.</p> <p>Let&apos;s implement the code for converting a list into string by using the stream API with Collectors.</p> <p> <strong>ConvertListToStringExample3.java</strong> </p> <pre> // import required classes and packages if any import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.stream.Collectors; // create class ConvertListToStringExample4 that will convert user given char list into a string using Collectors class ConvertListToStringExample4 { // main() method start public static void main(String[] args) { // declare variables and list int size; String str; List charList = new ArrayList(); // create Scanner class object to take input from user Scanner sc = new Scanner(System.in); System.out.println(&apos;Enter size of char list.&apos;); size = sc.nextInt(); for(int i = 0; i <size; i++) { system.out.println('enter char['+i+']: '); charlist.add(sc.next().charat(0)); } close scanner class object sc.close(); display chrlist elements system.out.println('list: ' + charlist); convert list into string by using collect() and joining() methods str="charList.stream()" .map(string::valueof) .collect(collectors.joining()); system.out.println('string after converting list: str); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/39/convert-list-string-java-4.webp" alt="Convert List to String in Java"> <p>In all the above-discussed ways, we have frequently used the StringBuilder class for converting a list into a string. </p> <hr></size;></pre></size;></pre></size;></pre></size;>

ConvertListToStringExample2.java

 // import required classes and packages if any import java.util.ArrayList; import java.util.List; import java.util.Scanner; import com.google.common.base.Joiner; // create class ConvertListToStringExample2 that will convert user given char list into a string using Joiner class class ConvertListToStringExample2 { // main() method start public static void main(String[] args) { // declare variables and list int size; String str; List charList = new ArrayList(); // create Scanner class object to take input from user Scanner sc = new Scanner(System.in); System.out.println(&apos;Enter size of char list.&apos;); size = sc.nextInt(); for(int i = 0; i <size; i++) { system.out.println(\'enter char[\'+i+\']: \'); charlist.add(sc.next().charat(0)); } close scanner class object sc.close(); display chrlist elements system.out.println(\'list: \' + charlist); convert list into string by using joiner str="Joiner.on(&apos;&apos;).join(charList);" system.out.println(\'string after converting list: str); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/39/convert-list-string-java-2.webp" alt="Convert List to String in Java"> <h3>3. Using List.toString(), String.substring() and String.replaceAll() methods</h3> <p>In this way of converting a list into a string, we use three methods of List and String classes. We use three methods to perform the following operations:</p> <ol class="points"> <li>We use the toString() method of the list to convert the list into a string. The return string will also contain opening/closing square brackets and commas.</li> <li>We use the substring() method to get rid of opening/closing square brackets.</li> <li>We use the replaceAll() method to replace all the commas and spaces with blank or null.</li> </ol> <p>Let&apos;s implement the code for converting a list into a string by using the toString(), substring(), and replaceAll() methods:</p> <p> <strong>ConvertListToStringExample3.java</strong> </p> <pre> // import required classes and packages if any package javaTpoint.JavaExample; import java.util.ArrayList; import java.util.List; import java.util.Scanner; // create class ConvertListToStringExample2 that will convert user given char list into a string using toString(), substring() and replaceAll() methods class ConvertListToStringExample3 { // main() method start public static void main(String[] args) { // declare variables and list int size; String str; List charList = new ArrayList(); // create Scanner class object to take input from user Scanner sc = new Scanner(System.in); System.out.println(&apos;Enter size of char list.&apos;); size = sc.nextInt(); for(int i = 0; i <size; 3 i++) { system.out.println(\'enter char[\'+i+\']: \'); charlist.add(sc.next().charat(0)); } close scanner class object sc.close(); display chrlist elements system.out.println(\'list: \' + charlist); convert list into string by using all the three methods of and str="charList.toString()" .substring(1, * charlist.size() - 1) .replaceall(\', \', \'\'); system.out.println(\'string after converting list: str); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/39/convert-list-string-java-3.webp" alt="Convert List to String in Java"> <h3>4. Using Collectors in Java</h3> <p>It is the last way to convert a list into a string. We use it rarely because here, we use stream API with collectors, which is available only in Java8.</p> <p>Let&apos;s implement the code for converting a list into string by using the stream API with Collectors.</p> <p> <strong>ConvertListToStringExample3.java</strong> </p> <pre> // import required classes and packages if any import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.stream.Collectors; // create class ConvertListToStringExample4 that will convert user given char list into a string using Collectors class ConvertListToStringExample4 { // main() method start public static void main(String[] args) { // declare variables and list int size; String str; List charList = new ArrayList(); // create Scanner class object to take input from user Scanner sc = new Scanner(System.in); System.out.println(&apos;Enter size of char list.&apos;); size = sc.nextInt(); for(int i = 0; i <size; i++) { system.out.println(\'enter char[\'+i+\']: \'); charlist.add(sc.next().charat(0)); } close scanner class object sc.close(); display chrlist elements system.out.println(\'list: \' + charlist); convert list into string by using collect() and joining() methods str="charList.stream()" .map(string::valueof) .collect(collectors.joining()); system.out.println(\'string after converting list: str); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/39/convert-list-string-java-4.webp" alt="Convert List to String in Java"> <p>In all the above-discussed ways, we have frequently used the StringBuilder class for converting a list into a string. </p> <hr></size;></pre></size;></pre></size;>