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 package test.java.util.HashMap;
32 
33 import java.util.*;
34 import java.util.stream.IntStream;
35 
36 public class PutNullKey {
37 
38     // Initial capacity of map
39     // Should be >= the map capacity for treeifying, see HashMap/ConcurrentMap.MIN_TREEIFY_CAPACITY
40     static final int INITIAL_CAPACITY = 64;
41 
42     // Maximum size of map
43     // Should be > the treeify threshold, see HashMap/ConcurrentMap.TREEIFY_THRESHOLD
44     static final int SIZE = 256;
45 
46     // Load factor of map
47     // A value 1.0 will ensure that a new threshold == capacity
48     static final float LOAD_FACTOR = 1.0f;
49 
50     public static class CollidingHash implements Comparable<CollidingHash> {
51 
52         private final int value;
53 
CollidingHash(int value)54         public CollidingHash(int value) {
55             this.value = value;
56         }
57 
58         @Override
hashCode()59         public int hashCode() {
60             // intentionally bad hashcode. Force into first bin.
61             return 0;
62         }
63 
64         @Override
equals(Object o)65         public boolean equals(Object o) {
66             if (null == o) {
67                 return false;
68             }
69 
70             if (o.getClass() != CollidingHash.class) {
71                 return false;
72             }
73 
74             return value == ((CollidingHash) o).value;
75         }
76 
77         @Override
compareTo(CollidingHash o)78         public int compareTo(CollidingHash o) {
79             return value - o.value;
80         }
81     }
82 
main(String[] args)83     public static void main(String[] args) throws Exception {
84         Map<Object,Object> m = new HashMap<>(INITIAL_CAPACITY, LOAD_FACTOR);
85         IntStream.range(0, SIZE)
86                 .mapToObj(CollidingHash::new)
87                 .forEach(e -> { m.put(e, e); });
88 
89         // kaboom?
90         m.put(null, null);
91     }
92 }
93