Convert a List to a Comma-Separated String in Java 8
2017-11-27 19:49
567 查看
Converting a List to a String with all the values of the List comma separated in Java 8 is really straightforward. Let’s have a look how to do that.
In Java 8
We can simply write String.join(..), pass a delimiter and an Iterable and the new StringJoiner will do the rest:
//Output: Milan,London,New York,San Francisco
If we are working with stream we can write as follow and still have the same result:
//Output: Milan,London,New York,San Francisco
Note: you can statically import java.util.stream.Collectors.joining if you prefer just typing “joining”.
//OUTPUT: Milan,London,New York,San Francisco,
//Remove last comma
//OUTPUT: Milan,London,New York,San Francisco
As you can see it’s much more verbose and easier to make mistakes like forgetting to remove the last comma. You can implement this in several ways—for example by moving the logic that removes the last comma to inside the for-loop—but no implementation will be so explicative and easy to understand as the declarative solution expressed in Java 8.
Focus should be on what you want to do—joining a List of String—not on how.
//Output: MILAN,LONDON,NEW YORK,SAN FRANCISCO
https://dzone.com/articles/java-8-convert-list-to-string-comma-separated
In Java 8
We can simply write String.join(..), pass a delimiter and an Iterable and the new StringJoiner will do the rest:
List<String> cities = Arrays.asList("Milan", "London", "New York", "San Francisco"); String citiesCommaSeparated = String.join(",", cities); System.out.println(citiesCommaSeparated);
//Output: Milan,London,New York,San Francisco
If we are working with stream we can write as follow and still have the same result:
String citiesCommaSeparated = cities.stream() .collect(Collectors.joining(",")); System.out.println(citiesCommaSeparated);
//Output: Milan,London,New York,San Francisco
Note: you can statically import java.util.stream.Collectors.joining if you prefer just typing “joining”.
In Java 7
For old times’ sake, let’s have a look at the Java 7 implementation:private static final String SEPARATOR = ","; public static void main(String[] args) { List<String> cities = Arrays.asList( "Milan", "London", "New York", "San Francisco"); StringBuilder csvBuilder = new StringBuilder(); for(String city : cities){ csvBuilder.append(city); csvBuilder.append(SEPARATOR); } String csv = csvBuilder.toString(); System.out.println(csv);
//OUTPUT: Milan,London,New York,San Francisco,
//Remove last comma
csv = csv.substring(0, csv.length() - SEPARATOR.length()); System.out.println(csv);
//OUTPUT: Milan,London,New York,San Francisco
As you can see it’s much more verbose and easier to make mistakes like forgetting to remove the last comma. You can implement this in several ways—for example by moving the logic that removes the last comma to inside the for-loop—but no implementation will be so explicative and easy to understand as the declarative solution expressed in Java 8.
Focus should be on what you want to do—joining a List of String—not on how.
Java 8: Manipulate String Before Joining
If you are using Stream, it’s really straightforward manipulate your String as you prefer by using map() or cutting some String out by using filter(). I’ll cover those topics in future articles. Meanwhile, this a straightforward example on how to transform the whole String to upper-case before joining.Java 8: From List to Upper-Case String Comma Separated
String citiesCommaSeparated = cities.stream() .map(String::toUpperCase) .collect(Collectors.joining(","));
//Output: MILAN,LONDON,NEW YORK,SAN FRANCISCO
https://dzone.com/articles/java-8-convert-list-to-string-comma-separated
相关文章推荐
- Convert a List to a Comma-Separated String in Java 8
- How to convert from string to datetime in sql server? - Sealyu - BlogJava
- How to convert a Map to List in Java?
- Easiest way to convert a List to a Set in Java?
- How to convert char array to String in Java
- In Java, how do I read/convert an InputStream to a String? - Stack Overflow
- How to convert List<Integer> to int[] in Java?
- Failed to convert from type java.lang.String to type java.util.Date for value………解决办法
- How to convert ArrayList to Set in Java with Example
- Java: Best way of converting List<Integer> to List<String>
- Simple way to repeat a String in java
- ResultCode() in ResultCode cannot be applied to (java.lang.String,java.lang.String)
- Convert Datetime to String in Sql Server
- Convert Datetime to String in Sql Server (转)
- Java for LeetCode 109 Convert Sorted List to Binary Search Tree
- spring mvc 数据绑定报错 Failed to convert property value of type 'java.lang.String' to required type 'int'
- springmvc-- Failed to convert property value of type 'java.lang.String' to required type
- Python convert list string to list
- Spring3MVC 提交请求参数中的日期问题(java.lang.String cant not convert to java.util.Date)
- Err:The "." operator was supplied with an index value of type "java.lang.String" to be applied to a List or array