Use Collection#toArray(new T[0]) instead of a presized array as it is faster on modern JVMs.

This commit is contained in:
pascalschumacher 2019-12-26 22:48:12 +01:00
parent fe44a99852
commit 84668a2d98
10 changed files with 18 additions and 18 deletions

View File

@ -225,7 +225,7 @@ public class CharSet implements Serializable {
// NOTE: This is no longer public as CharRange is no longer a public class. // NOTE: This is no longer public as CharRange is no longer a public class.
// It may be replaced when CharSet moves to Range. // It may be replaced when CharSet moves to Range.
/*public*/ CharRange[] getCharRanges() { /*public*/ CharRange[] getCharRanges() {
return set.toArray(new CharRange[set.size()]); return set.toArray(new CharRange[0]);
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------

View File

@ -7507,7 +7507,7 @@ public class StringUtils {
currentType = type; currentType = type;
} }
list.add(new String(c, tokenStart, c.length - tokenStart)); list.add(new String(c, tokenStart, c.length - tokenStart));
return list.toArray(new String[list.size()]); return list.toArray(new String[0]);
} }
/** /**
@ -7735,7 +7735,7 @@ public class StringUtils {
} }
} }
return substrings.toArray(new String[substrings.size()]); return substrings.toArray(new String[0]);
} }
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
@ -7923,7 +7923,7 @@ public class StringUtils {
if (match || preserveAllTokens && lastMatch) { if (match || preserveAllTokens && lastMatch) {
list.add(str.substring(start, i)); list.add(str.substring(start, i));
} }
return list.toArray(new String[list.size()]); return list.toArray(new String[0]);
} }
/** /**
@ -8022,7 +8022,7 @@ public class StringUtils {
if (match || preserveAllTokens && lastMatch) { if (match || preserveAllTokens && lastMatch) {
list.add(str.substring(start, i)); list.add(str.substring(start, i));
} }
return list.toArray(new String[list.size()]); return list.toArray(new String[0]);
} }
/** /**
@ -8835,7 +8835,7 @@ public class StringUtils {
if (list.isEmpty()) { if (list.isEmpty()) {
return null; return null;
} }
return list.toArray(new String[list.size()]); return list.toArray(new String[0]);
} }
/** /**

View File

@ -274,7 +274,7 @@ public class ExceptionUtils {
} }
frames.addAll(trace); frames.addAll(trace);
} }
return frames.toArray(new String[frames.size()]); return frames.toArray(new String[0]);
} }
/** /**
@ -325,7 +325,7 @@ public class ExceptionUtils {
while (frames.hasMoreTokens()) { while (frames.hasMoreTokens()) {
list.add(frames.nextToken()); list.add(frames.nextToken());
} }
return list.toArray(new String[list.size()]); return list.toArray(new String[0]);
} }
/** /**
@ -438,7 +438,7 @@ public class ExceptionUtils {
*/ */
public static Throwable[] getThrowables(final Throwable throwable) { public static Throwable[] getThrowables(final Throwable throwable) {
final List<Throwable> list = getThrowableList(throwable); final List<Throwable> list = getThrowableList(throwable);
return list.toArray(new Throwable[list.size()]); return list.toArray(new Throwable[0]);
} }
/** /**

View File

@ -199,7 +199,7 @@ public class FieldUtils {
*/ */
public static Field[] getAllFields(final Class<?> cls) { public static Field[] getAllFields(final Class<?> cls) {
final List<Field> allFieldsList = getAllFieldsList(cls); final List<Field> allFieldsList = getAllFieldsList(cls);
return allFieldsList.toArray(new Field[allFieldsList.size()]); return allFieldsList.toArray(new Field[0]);
} }
/** /**
@ -237,7 +237,7 @@ public class FieldUtils {
*/ */
public static Field[] getFieldsWithAnnotation(final Class<?> cls, final Class<? extends Annotation> annotationCls) { public static Field[] getFieldsWithAnnotation(final Class<?> cls, final Class<? extends Annotation> annotationCls) {
final List<Field> annotatedFieldsList = getFieldsListWithAnnotation(cls, annotationCls); final List<Field> annotatedFieldsList = getFieldsListWithAnnotation(cls, annotationCls);
return annotatedFieldsList.toArray(new Field[annotatedFieldsList.size()]); return annotatedFieldsList.toArray(new Field[0]);
} }
/** /**

View File

@ -878,7 +878,7 @@ public class MethodUtils {
final boolean searchSupers, final boolean ignoreAccess) { final boolean searchSupers, final boolean ignoreAccess) {
final List<Method> annotatedMethodsList = getMethodsListWithAnnotation(cls, annotationCls, searchSupers, final List<Method> annotatedMethodsList = getMethodsListWithAnnotation(cls, annotationCls, searchSupers,
ignoreAccess); ignoreAccess);
return annotatedMethodsList.toArray(new Method[annotatedMethodsList.size()]); return annotatedMethodsList.toArray(new Method[0]);
} }
/** /**

View File

@ -1149,7 +1149,7 @@ public class TypeUtils {
} }
} }
return types.toArray(new Type[types.size()]); return types.toArray(new Type[0]);
} }
/** /**

View File

@ -604,10 +604,10 @@ public class StrTokenizer implements ListIterator<String>, Cloneable {
if (chars == null) { if (chars == null) {
// still call tokenize as subclass may do some work // still call tokenize as subclass may do some work
final List<String> split = tokenize(null, 0, 0); final List<String> split = tokenize(null, 0, 0);
tokens = split.toArray(new String[split.size()]); tokens = split.toArray(new String[0]);
} else { } else {
final List<String> split = tokenize(chars, 0, chars.length); final List<String> split = tokenize(chars, 0, chars.length);
tokens = split.toArray(new String[split.size()]); tokens = split.toArray(new String[0]);
} }
} }
} }

View File

@ -564,7 +564,7 @@ public class DurationFormatUtils {
if (inLiteral) { // i.e. we have not found the end of the literal if (inLiteral) { // i.e. we have not found the end of the literal
throw new IllegalArgumentException("Unmatched quote in format: " + format); throw new IllegalArgumentException("Unmatched quote in format: " + format);
} }
return list.toArray(new Token[list.size()]); return list.toArray(new Token[0]);
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------

View File

@ -160,7 +160,7 @@ public class FastDatePrinter implements DatePrinter, Serializable {
*/ */
private void init() { private void init() {
final List<Rule> rulesList = parsePattern(); final List<Rule> rulesList = parsePattern();
mRules = rulesList.toArray(new Rule[rulesList.size()]); mRules = rulesList.toArray(new Rule[0]);
int len = 0; int len = 0;
for (int i=mRules.length; --i >= 0; ) { for (int i=mRules.length; --i >= 0; ) {

View File

@ -407,7 +407,7 @@ public class EventCountCircuitBreakerTest {
*/ */
public void verify(final Boolean... values) { public void verify(final Boolean... values) {
assertArrayEquals(values, assertArrayEquals(values,
changedValues.toArray(new Boolean[changedValues.size()])); changedValues.toArray(new Boolean[0]));
} }
} }
} }