java collection implode

Wesley13
• 阅读 413

With Java 8 you can do this without any third party library.

If you want to join a Collection of Strings you can use the new String.join() method:

List list = Arrays.asList("foo", "bar", "baz"); String joined = String.join(" and ", list); // "foo and bar and baz" If you have a Collection with another type than String you can use the Stream API with the joining Collector:

List list = Arrays.asList( new Person("John", "Smith"), new Person("Anna", "Martinez"), new Person("Paul", "Watson ") );

String joinedFirstNames = list.stream() .map(Person::getFirstName) .collect(Collectors.joining(", ")); // "John, Anna, Paul"

StringJoiner

点赞
收藏
评论区
推荐文章
技术小男生 技术小男生
2个月前
linux环境jdk环境变量配置
1:编辑系统配置文件vi /etc/profile2:按字母键i进入编辑模式,在最底部添加内容: JAVAHOME/opt/jdk1.8.0152 CLASSPATH.:$JAVAHOME/lib/dt.jar:$JAVAHOME/lib/tools.jar PATH$JAVAHOME/bin:$PATH3:生效配置
光头强的博客 光头强的博客
2个月前
Java面向对象试题
1、 请创建一个Animal动物类,要求有方法eat()方法,方法输出一条语句“吃东西”。 创建一个接口A,接口里有一个抽象方法fly()。创建一个Bird类继承Animal类并实现 接口A里的方法输出一条有语句“鸟儿飞翔”,重写eat()方法输出一条语句“鸟儿 吃虫”。在Test类中向上转型创建b对象,调用eat方法。然后向下转型调用eat()方
Stella981 Stella981
1年前
AndroChef Java Decompile
With AndroChef Java Decompiler you can decompile apk., dex, jar and java class-files. It's simple and easy. AndroChef Java Decompiler is Windows XP, Windows 2003, Windows Vista, W
Wesley13 Wesley13
1年前
ubuntu中安装openjdk与oracle jdk
Preparing your system First you need to remove openjdk for this run the following command from your terminal > sudo apt-get purge openjdk\* If you installed java 7 from any othe
Wesley13 Wesley13
1年前
Java NIO Channel
Java NIO Channels are similar to streams with a few differences: * You can both read and write to a Channels. Streams are typically one-way (read or write). * Channels can be
Stella981 Stella981
1年前
CentOS 6.5安装jdk1.8
By default, all Linux distributions comes with openjdk preinstalled. If you want to install and use Oracle Java JDK/JRE on your system, Just follow the steps below. In this tutoria
Wesley13 Wesley13
1年前
VSCode如何格式化所有文件
VSCode如何格式化所有文件 You can use a extension called ”Format Files”. ”Use: Open command pallette (Ctrl+Shift+P) & enter "Format Files" Create keybinding to 'editor.action.formatFiles'
Stella981 Stella981
1年前
Django模型迁移小错误
Django操作ORM小笔记 ============== 报错内容: ----- > You are trying to add a non-nullable field 'mobile' to user without a default; we can't do that (the database needs something to popul
Stella981 Stella981
1年前
Shield Your Kibana Dashboards
You work with sensitive data in Elasticsearch indices that you do not want everyone to see in their Kibana dashboards. Like a hospital with patient names. You could give each depar
Easter79 Easter79
1年前
The App Locker FAQ
**How to use The App Locker:** 1\. Start The App Locker, create a password. 2\. In the main console, click "+" and select the apps you want to lock. 3\. You can choose wheathe