假设一个类(例如URI)可以使用构造函数和toString()方法转换为String或从String转换.
我有一个ArrayList<URI>,我想把它复制到一个ArrayList<String>,或者反过来.
Java标准库中是否有实用程序功能可以执行此操作?就像是:
java.util.collections.copy(urlArray,stringArray);
我知道有实用程序库提供该功能,但我不想添加不必要的库.
我也知道如何编写这样的函数,但是阅读代码并发现有人编写了标准库中已存在的函数,这很烦人.
我知道您不想添加其他库,但对于从搜索引擎中找到此内容的任何人,您可以使用google-collections:
List<String> strings = Lists.transform(uris, Functions.toStringFunction());
Run Code Online (Sandbox Code Playgroud)
一种方式,和
List<String> uris = Lists.transform(strings, new Function<String, URI>() {
public URI apply(String from) {
try {
return new URI(from);
} catch (URISyntaxException e) {
// whatever you need to do here
}
}
});
Run Code Online (Sandbox Code Playgroud)
另一个.
| 归档时间: |
|
| 查看次数: |
4154 次 |
| 最近记录: |