Idiom for generics on static methods

less than 1 minute read

I always forget this idiom: [sourcecode language=”java”] public static Collection getFirstN(int numRecs, Collection records) { List rtn = new ArrayList(numRecs); if (records == null || numRecs <= 0) { return rtn; } int i=0; for (T obj : records) { rtn.add(obj); if (i++ >= numRecs) break; } return rtn; } [/sourcecode]