Convert a List to a Comma-Separated String in Java 8
来源:互联网 发布:父母的神逻辑知乎 编辑:程序博客网 时间:2024/05/29 08:50
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:
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
- Convert a List to a Comma-Separated String in Java 8
- How to convert a Map to List in Java?
- convert List<String> to a String
- Python convert a list to String
- Easiest way to convert a List to a Set in Java?
- Convert a String In C++ To Upper or Lower Case
- Convert a String In C++ To Upper or Lower Case
- convert a string to XmlElement
- Convert a String to a Number
- How to convert a String to DateTime
- Convert a BST to a sorted doubly-linked list in-place
- Convert a string to a byte array and vica-versa in C#
- Find All Anagrams in a String Add to List
- convert a hexadecimal string to a byte string
- Convert a string to an enum
- experiment : convert a string to crc32
- Read/convert an InputStream to a String
- mysql关于null值的使用
- hibernate_day04_12_HQL多表查询介绍
- 【提醒自己】CodeForces
- 集合collection
- python3学习-lxml模块
- Convert a List to a Comma-Separated String in Java 8
- laserscan数据格式
- 关于MySQL中的information_schema
- 1045. 快速排序(25)--Python
- Java学习13 GUI程序设计(Swing)
- uC/OS-II中OSUnMapTbl数组的推导
- Android 6.0的http
- 掌柜大作战(21):京东线上数据库管理
- 自话粒子群算法(超简单实例)