This example Java source code file (Collisions.java) is included in the alvinalexander.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn
Java by Example" TM.
/*
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 7126277
* @run main Collisions -shortrun
* @summary Ensure Maps behave well with lots of hashCode() collisions.
* @author Mike Duigou
*/
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListMap;
public class Collisions {
/**
* Number of elements per map.
*/
private static final int TEST_SIZE = 5000;
final static class HashableInteger implements Comparable<HashableInteger> {
final int value;
final int hashmask; //yes duplication
HashableInteger(int value, int hashmask) {
this.value = value;
this.hashmask = hashmask;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof HashableInteger) {
HashableInteger other = (HashableInteger) obj;
return other.value == value;
}
return false;
}
@Override
public int hashCode() {
return value % hashmask;
}
@Override
public int compareTo(HashableInteger o) {
return value - o.value;
}
@Override
public String toString() {
return Integer.toString(value);
}
}
private static Object[][] makeTestData(int size) {
HashableInteger UNIQUE_OBJECTS[] = new HashableInteger[size];
HashableInteger COLLIDING_OBJECTS[] = new HashableInteger[size];
String UNIQUE_STRINGS[] = new String[size];
String COLLIDING_STRINGS[] = new String[size];
for (int i = 0; i < size; i++) {
UNIQUE_OBJECTS[i] = new HashableInteger(i, Integer.MAX_VALUE);
COLLIDING_OBJECTS[i] = new HashableInteger(i, 10);
UNIQUE_STRINGS[i] = unhash(i);
COLLIDING_STRINGS[i] = (0 == i % 2)
? UNIQUE_STRINGS[i / 2]
: "\u0000\u0000\u0000\u0000\u0000" + COLLIDING_STRINGS[i - 1];
}
return new Object[][] {
new Object[]{"Unique Objects", UNIQUE_OBJECTS},
new Object[]{"Colliding Objects", COLLIDING_OBJECTS},
new Object[]{"Unique Strings", UNIQUE_STRINGS},
new Object[]{"Colliding Strings", COLLIDING_STRINGS}
};
}
/**
* Returns a string with a hash equal to the argument.
*
* @return string with a hash equal to the argument.
*/
public static String unhash(int target) {
StringBuilder answer = new StringBuilder();
if (target < 0) {
// String with hash of Integer.MIN_VALUE, 0x80000000
answer.append("\\u0915\\u0009\\u001e\\u000c\\u0002");
if (target == Integer.MIN_VALUE) {
return answer.toString();
}
// Find target without sign bit set
target = target & Integer.MAX_VALUE;
}
unhash0(answer, target);
return answer.toString();
}
private static void unhash0(StringBuilder partial, int target) {
int div = target / 31;
int rem = target % 31;
if (div <= Character.MAX_VALUE) {
if (div != 0) {
partial.append((char) div);
}
partial.append((char) rem);
} else {
unhash0(partial, div);
partial.append((char) rem);
}
}
private static void realMain(String[] args) throws Throwable {
boolean shortRun = args.length > 0 && args[0].equals("-shortrun");
Object[][] mapKeys = makeTestData(shortRun ? (TEST_SIZE / 2) : TEST_SIZE);
// loop through data sets
for (Object[] keys_desc : mapKeys) {
Map<Object, Object>[] maps = (Map
Other Java examples (source code examples)
Here is a short list of links related to this Java Collisions.java source code file: