youxinren / kryo

Automatically exported from code.google.com/p/kryo
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

IntMap toString should not ignore 0 as a key #145

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. intMap.put(0, "valueFor0Key-zeroIsAValidKeyToo"
intMap.toString()

What is the expected output? 
[0, valueFor0Key-zeroIsAValidKeyToo]

What do you see instead?
[]

What version of the Kryo are you using?

Please provide any additional information below.

Proposed fix:

    public String toString() {
        if (size == 0) return "[]";
        StringBuilder buffer = new StringBuilder(32);
        buffer.append('[');
        int[] keyTable = this.keyTable;
        V[] valueTable = this.valueTable;
        int i = keyTable.length;
        while (i-- > 0) {
            int key = keyTable[i];
            if (key == EMPTY) continue;
            buffer.append(key);
            buffer.append('=');
            buffer.append(valueTable[i]);
            break;
        }
        while (i-- > 0) {
            int key = keyTable[i];
            if (key == EMPTY) continue;
            buffer.append(", ");
            buffer.append(key);
            buffer.append('=');
            buffer.append(valueTable[i]);
        }

        if (hasZeroValue) {
            if (size > 1)
                buffer.append(", ");

            buffer.append(EMPTY);
            buffer.append('=');
            buffer.append(zeroValue);
        }

        buffer.append(']');
        return buffer.toString();
    }

Original issue reported on code.google.com by Vladisla...@gmail.com on 3 Nov 2013 at 7:45

GoogleCodeExporter commented 9 years ago

Original comment by nathan.s...@gmail.com on 3 Nov 2013 at 9:38