gpt4 book ai didi

java - 我可以装饰 Guava 的 Joiner 类吗

转载 作者:搜寻专家 更新时间:2023-10-30 19:50:05 25 4
gpt4 key购买 nike

我有一个 List<String>我们正在使用 Joiner 来获取该列表的逗号分隔表示,但现在我们需要做一些改进,我们需要将列表中的值大写。现在代码是 -

String str = Joiner.on(',').skipNulls().join(myValueList);

但是现在因为我需要将值中存在的字符串大写,所以我需要先迭代它以大写然后传递给 Joiner 加入,但我认为这不是一个好方法,因为它会迭代列表两次, one to capitalize 然后 Joiner 会迭代到 Join.
是否有任何其他我缺少的实用方法可以在一次迭代中执行此操作。

你将如何使用 Guava 来实现?

最佳答案

您可以使用 Iterables.transform()

Iterable<String> upperStrings = Iterables.transform(myValueList, new Function<String,String>() {
public String apply(String input) {
// any transformation possible here.
return (input == null) ? null : input.toUpperCase();
}
});
Str str = Joiner.on(',').skipNulls().join(upperStrings);

关于java - 我可以装饰 Guava 的 Joiner 类吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5040886/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com