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

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 宝宝吃了就吐怎么办 图书借阅证丢了怎么办 苹果6手机掌阅怎么办 厨房墙砖颜色深怎么办 炉石传说被盗号怎么办 炉石传说号忘了怎么办 手机丢失了微信怎么办 眼睛疼红血丝多怎么办 lol更新速度3kb怎么办 苹果6s玩游戏卡怎么办 微博手机号换了怎么办 微博字数超了怎么办 海外玩传奇很卡怎么办 再审期限超6个月怎么办 肠粉蒸出来太粘怎么办 微信订阅号没了怎么办 映美620k不进纸怎么办 属虎的带了貔貅怎么办 属龙的不能带金怎么办 这段时间运气不好怎么办 两年运气特别差怎么办 玩手机麻将老输怎么办 打四川麻将老输怎么办 网上打麻将老输怎么办 手机打麻将老输怎么办 近来打麻将老输怎么办 最近手气不好老输钱怎么办 头被风吹了头痛怎么办 打麻将老输怎么办转运 外出时家里的花怎么办 放假了家里的花怎么办 老是怀疑老婆有外遇怎么办 老婆出轨我该怎么办呢 老公爱爱时间短怎么办 老婆离家出走了怎么办离婚呢 4g网络变成h了怎么办 打麻药伤到神经怎么办 40多岁乳房下垂怎么办 手冻了怎么办 小妙招 脸过敏好了还红怎么办 脸过过敏红肿痒怎么办