➤ How to Code a Game
➤ Array Programs in Java
➤ Java Inline Thread Creation
➤ Java Custom Exception
➤ Hibernate vs JDBC
➤ Object Relational Mapping
➤ Check Oracle DB Size
➤ Check Oracle DB Version
➤ Generation of Computers
➤ XML Pros & Cons
➤ Git Analytics & Its Uses
➤ Top Skills for Cloud Professional
➤ How to Hire Best Candidates
➤ Scrum Master Roles & Work
➤ CyberSecurity in Python
➤ Protect from Cyber-Attack
➤ Solve App Development Challenges
➤ Top Chrome Extensions for Twitch Users
➤ Mistakes That Can Ruin Your Test Metric Program
Convert Comma Separated String to List Java | In this section, we will discuss how to convert comma separated string to list java. The string can contain commas, so based on the comma we have to split the string and add each element to the list.
For example:-
String = “Java, Programming, Python”
Then the list will be = [“Java”, “Programming”, “Python”]
Program to Convert Comma Separated String to List Java
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
String string = "Java,Programming,Language,Python,C++";
String values[] = string.split(",");
List<String> list = Arrays.asList(values);
System.out.println("List: " + list);
System.out.println("Size of the list: " + list.size());
}
}
Output:-
List: [Java, Programming, Language, Python, C++]
Size of the list: 5
The string can be splitter and added to the list<String> as follows:-
List<String> list = Arrays.asList(string.split(","));
How To Add Comma Separated Values In List In Java using Stream
Let us see another program to convert comma separated string to list Java. This time instead of using Arrays.aslist() we are going to use Java streams.
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
String string = "Java,Programming,Language,Python,C++";
List<String> list = Stream.of(string.split(","))
.collect(Collectors.toList());
System.out.println("List: " + list);
System.out.println("Size of the list: " + list.size());
}
}
Output:-
List: [Java, Programming, Language, Python, C++]
Size of the list: 5
Program to Convert Comma Separated String to List Java – Integer values
If the string contains numbers then before adding it to the list we should convert the string to a number. A numeric string can be converted to an Integer value by using Integer.parseInt() method or by using the Integer.valueOf() method. The Integer.valueOf() method internally using Integer.parseInt() method.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
String string = "10,20,30,40,50,60,70,80,90";
List<Integer> list = new ArrayList<>();
for(String str: string.split(",")) {
list.add(Integer.valueOf(str));
}
System.out.println("List: " + list);
System.out.println("Size of the list: " + list.size());
}
}
Output:-
List: [10, 20, 30, 40, 50, 60, 70, 80, 90]
Size of the list: 9
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!