Is there a pattern, or built in function I am missing or shall I just loop through like so
public List<MyObject> convert(List<String> myStrings){
List<MyObject> myObjects = new ArrayList<MyObject>(myStrings.size());
Integer i = 0;
for(String string : myStrings){
MyObject myObject = new myObject(i, string);
myObjects.add(object);
i++;
}
return myObjects;
}
Its because I need to persist the list to a database and retain the ordering.
You can use Guava:
Really it just brings the iteration into the library though. In terms of actual code written, it will be about the same until closures are introduced with Java 8.
However, you should know that making the function stateful like this (with
i) is bad form since now the order in which it’s applied to the list is important.