1 /*
2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /**
25  * @test
26  * @bug 8046085
27  * @summary Ensure that when trees are being used for collisions that null key
28  * insertion still works.
29  */
30 
31 import java.util.*;
32 import java.util.stream.IntStream;
33 
34 public class PutNullKey {
35 
36     // Initial capacity of map
37     // Should be >= the map capacity for treeifying, see HashMap/ConcurrentMap.MIN_TREEIFY_CAPACITY
38     static final int INITIAL_CAPACITY = 64;
39 
40     // Maximum size of map
41     // Should be > the treeify threshold, see HashMap/ConcurrentMap.TREEIFY_THRESHOLD
42     static final int SIZE = 256;
43 
44     // Load factor of map
45     // A value 1.0 will ensure that a new threshold == capacity
46     static final float LOAD_FACTOR = 1.0f;
47 
48     public static class CollidingHash implements Comparable<CollidingHash> {
49 
50         private final int value;
51 
CollidingHash(int value)52         public CollidingHash(int value) {
53             this.value = value;
54         }
55 
56         @Override
hashCode()57         public int hashCode() {
58             // intentionally bad hashcode. Force into first bin.
59             return 0;
60         }
61 
62         @Override
equals(Object o)63         public boolean equals(Object o) {
64             if (null == o) {
65                 return false;
66             }
67 
68             if (o.getClass() != CollidingHash.class) {
69                 return false;
70             }
71 
72             return value == ((CollidingHash) o).value;
73         }
74 
75         @Override
compareTo(CollidingHash o)76         public int compareTo(CollidingHash o) {
77             return value - o.value;
78         }
79     }
80 
main(String[] args)81     public static void main(String[] args) throws Exception {
82         Map<Object,Object> m = new HashMap<>(INITIAL_CAPACITY, LOAD_FACTOR);
83         IntStream.range(0, SIZE)
84                 .mapToObj(CollidingHash::new)
85                 .forEach(e -> { m.put(e, e); });
86 
87         // kaboom?
88         m.put(null, null);
89     }
90 }
91