在Java中显示列表与Python中一样优雅 [英] Displaying list in Java as elegant as in Python

查看:141
本文介绍了在Java中显示列表与Python中一样优雅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,将逗号显示为逗号分隔列表非常容易:

In Python it is pretty easy to display an iterable as comma separated list:

>>> iterable = ["a", "b", "c"]
>>> ", ".join(iterable)
'a, b, c'

有吗一种接近这种简洁的Java方式? (注意最后没有,。)

Is there a Java way that comes close to this conciseness? (Notice that there is no "," at the end.)

推荐答案

以下是使用番石榴 Commons / Lang 迈克尔提到

List<String> items = Arrays.asList("a","b","c");

// Using Guava
String guavaVersion = Joiner.on(", ").join(items);

// Using Commons / Lang
String commonsLangVersion = StringUtils.join(items, ", ");

// both versions produce equal output
assertEquals(guavaVersion, commonsLangVersion);

参考:

Commons / Lang: StringUtils.join(Collection,String)

Commons / Lang: StringUtils.join(Collection, String)

这篇关于在Java中显示列表与Python中一样优雅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆