Assume a class (for instance URI) that is convertable to and from a String using the constructor and toString() method.
I have an ArrayList<URI>
and I want to copy it to an ArrayList<String>
, or the other way around.
Is there a utility function in the Java standard library that will do it? Something like:
java.util.collections.copy(urlArray,stringArray);
I know there are utility libraries that provide that function, but I don't want to add an unnecessary library.
I also know how to write such a function, but it's annoying to read code and find that someone has written functions that already exist in the standard library.
I know you don't want to add additional libraries, but for anyone who finds this from a search engine, in google-collections you might use:
List<String> strings = Lists.transform(uris, Functions.toStringFunction());
one way, and
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
}
}
});
the other.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With