I call an API that returns a set of objects to me. I want to get a subset of objects. I think of two solutions. Which one will give me the best performance? Based on my understanding, calling toArray()
will basically go through the collection once. If true, would a solution be better?
Solution 1 -
public static List<String> get(UUID recordid, int start, int count) { List<String> names = new ArrayList<String>(); ... Collection<String> columnnames = result.getColumnNames(); int index = 0; for (UUID columnname : columnnames) { if ((index >= start) && (index - start < count)) { names.add(columnname); } index++; } return names; }
Solution 2 -
public static List<String> get(UUID recordid, int start, int count) { List<String> names = new ArrayList<String>(); ... Collection<String> columnnames = result.getColumnNames(); String[] nameArray = columnnames.toArray(new String(columnnames.size())); for (int index = 0; index < nameArray.length && count > 0; index++, count--) { names.add(nameArray[index]); } return names; }
source share