如何将这段代码转换为Java 8流?

布鲁诺·罗德里格斯(Bruno Rodrigues):

我想将其转换为循环流线

List<String> newLore = new ArrayList<>();
for (String str : description) {
    newLore.add(ChatColor.translateAlternateColorCodes('&', str));
}
itemMeta.setLore(newLore);
尼古拉斯:

使用以下方法链。关键是将map每个项目转移到一个新项目,最后收集到一个项目List

List<String> newLore = description.stream()                       // Stream<String> with old elements
    .map(str -> ChatColor.translateAlternateColorCodes('&', str)) // Stream<String> with the new ones
    .collect(Collectors.toList());                                // List<String>
itemMeta.setLore(newLore);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章