如何计算Java中字符串列表中的冲突

时间:2017-06-10 20:47:37

标签: java string hashmap hashcode

如何使用每个字符串的哈希码计算字符串列表中的所有碰撞对?

public class HashCollisions {
private static int strLength;
private static int colls;

public static void main(String[] args) {

    String[] strings ={"AaAaAa","AaAaBB","AaBBAa","AaBBBB"};

    strLength=strings.length;
    for (int i = 0; i < strLength - 1; i++) {
        for (int j = i + 1; j < strLength; j++) {
            if (hash(strings[i]) == hash(strings[j]) && !(strings[i].equals(strings[j]))) {
                    colls++;
            }
        }
    }

    System.out.println(colls);

}

private static byte hash(String s) {
    byte[] bytes = s.getBytes();
    byte result = bytes[0];

    for (int i = 1; i < bytes.length; i++) {
        result ^= bytes[i];
    }

    return result;
}

}

  • 使用给定的输入,我应该检测碰撞对的计数: {0 = [AaAaBB,AaBBAa],32 = [AaAaAa,AaBBBB]}将为2.
  • 任何其他比O(n ^ 2)更有效的解决方案?

2 个答案:

答案 0 :(得分:2)

您可以按照each_file_bench_results对字符串列表进行分组,然后使用生成的地图。只要您有一个给定密钥的值,就会有  碰撞:

hashCode

打印:

public static void main(String[] args) {
    List<String> strings = Arrays.asList("foo", "bar", "AaAa", "foobar",
            "BBBB", "AaBB", "FB", "Ea", "foo");
    Map<Integer, List<String>> stringsByHash = strings.stream()
            .collect(Collectors.groupingBy(String::hashCode));
    for (Entry<Integer, List<String>> entry : stringsByHash.entrySet()) {
        List<String> value = entry.getValue();
        int collisions = value.size() - 1;
        if (collisions > 0) {
            System.out.println(
                    "Got " + collisions + " collision(s) for strings "
                            + value + " (hash: " + entry.getKey() + ")");
        }
    }
}

答案 1 :(得分:-1)

为什么不使用Set,将List中的每个值放入Set中,并通过计算List.size() - Set.size()找到碰撞次数?