This topic discusses how toString() generator lists contents of arrays and how it limits number of items listed in Collections and Maps. Used method depends not only on the member type, but also on selected JDK compatibility of the project.
Arrays.toString(). As this method is not available in JDK1.4, Arrays.asList() is used instead in this case, but only for non-primitive arrays. Primitive arrays are handled with a helper method, showed below.Arrays.asList() with List.subList(). For example, for a member named anArray, generated code looks like this: Arrays.asList(anArray).subList(0, Math.min(anArray.length, maxLen))Arrays.asList() cannot be used so in JDK1.5 or lower a helper method is used instead. In JDK1.6 there's another solution: Arrays.toString(Arrays.copyOf(anArray, Math.min(anArray.length, maxLen)). Although copying an array is not an optimal solution, the copied part is usually rather small and efficiency is not affected. A good thing is that a helper method can be avoided.[1, 2, 3]. The method iterates over the array and uses instanceof to determine its enclosing type. To make the code shorter, it checks only for types that can actually be passed to it.
private String arrayToString(Object array, int len, int maxLen) {
StringBuffer stringBuffer = new StringBuffer();
len = Math.min(len, maxLen);
stringBuffer.append("[");
for (int i = 0; i < len; i++) {
if (i > 0)
stringBuffer.append(", ");
if (array instanceof float[])
stringBuffer.append(((float[]) array)[i]);
if (array instanceof int[])
stringBuffer.append(((int[]) array)[i]);
if (array instanceof Object[])
stringBuffer.append(((Object[]) array)[i]);
}
stringBuffer.append("]");
return stringBuffer.toString();
}
NOTES:maxLen argument and doesn't use Math.min()ListsThe same solution is used for all JDK versions: aList.subList(0, Math.min(aList.size(), maxLen))
Collections (helper method)A Collection cannot be turned into a List without copying its contents (assuming it isn't a List already), so a helper method is used to iterate over first maxLen elements and build a string out of them:
private String toString(Collection collection, int maxLen) {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("[");
int i = 0;
for (Iterator iterator = collection.iterator(); iterator.hasNext() && i < maxLen; i++) {
if (i > 0)
stringBuffer.append(", ");
stringBuffer.append(iterator.next());
}
stringBuffer.append("]");
return stringBuffer.toString();
}
NOTES:MapsIn case of Maps, the same helper method is used as for Collections only that map.entrySet() is passed to it.
This table sums up what methods are used in different conditions:
java.util.List |
java.util.Collection |
java.util.Map |
Array of primitive type | Array of non-primitive type | |
|---|---|---|---|---|---|
| jdk 1.4 | - | - | - | helper method arrayToString(array, len) |
Arrays.asList(array) |
| jdk 1.4/1.5, limit elements | member.subList() |
helper method toSting(collection, maxLen) |
helper method toString(collection, maxLen) with map.entrySet() |
helper method arrayToString(array, len, maxLen) |
Arrays.asList(array).subList() |
| jdk 1.5 | - | - | - | Arrays.toString() |
Arrays.asList(array) |
| jdk 1.6 | - | - | - | Arrays.toString() |
Arrays.toString() |
| jdk 1.6, limit elements | member.subList() |
helper method toString(Collection) |
helper method toString(Collection) with map.entrySet() |
Arrays.toString(Arrays.copyOf(member, ...)) |
Arrays.asList(array).subList() |
List is usually handled with subList() method, but if there's another member of Collection (not List) type and toString(collection) is generated, Lists are also passed to it. This way the code is shorter and more consistent.array != null ? Arrays.asList(array) : null"[]") instead of methods described above.
Generate toString() dialog
toString() Generator: Format Templates
toString() Generator: Code Styles