java - 是否有更好的方法对由 128 个合法 ASCII 字符组成的字符串进行哈希排列?

标签 java hashmap runtime hash-code-uniqueness

假设任何两个彼此排列的字符串都被认为是相同的(例如,(ABACD,BDCAA)和(ABACD,DBACA)应该散列到 HashMap 的同一桶中)。该字符串仅包含 128 个合法 ASCII 字符中的任意一个。是否有更好的哈希函数可以在保持 HashMap 小的同时最大限度地减少冲突?

还有,有什么办法可以进一步优化代码吗?主要目标是尽可能减少运行时间。

该方法接受一个包含一组文本行的文件,每行代表一个条目。文件中的第一行表示条目总数。它将计算包含相同多重集的条目对的总数。

输入文件包含内容的示例: 7 BCDEFGH ABACD BDCEF BDCAA 十二烷基苯磺酸钠 达巴卡 达巴克

它应该输出: 6

这六对是: (ABACD、BDCAA) (ABACD、DBACA) (ABACD、DABAC) (BDCAA、DBACA) (BDCAA、DABAC) (DBACA、DABAC)

发生哈希的部分:

long hash = 1;
while (c != 10) {
    hash *= PRIMES[c];
    c = reader.read();
}
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;

public class Speed {
    private static final int[] PRIMES = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71,
            73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173,
            179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281,
            283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409,
            419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541,
            547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659,
            661, 673, 677, 683, 691, 701, 709, 719};

    public int processData(String filename){
        try {
            Reader reader = new Reader(filename);
            int size = Integer.parseInt(reader.readLine());
            HashMap<Long, Integer> hm = new HashMap<>(size / 2);
            int total = 0;
            while (true) {
                int c = reader.read();
                if (c == -1)
                    break;
                long hash = 1;
                while (c != 10) {
                    hash *= PRIMES[c];
                    c = reader.read();
                }
                if (hm.get(hash) == null) {
                    hm.put(hash, 1);
                } else {
                    int value = hm.get(hash);
                    total += value;
                    hm.put(hash, value + 1);
                }
            }
            return total;
        } catch (Exception e) {
            System.out.println(e);
        }
        return 0;
    }

    static class Reader
    {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Reader(String file_name) throws IOException
        {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String readLine() throws IOException
        {
            byte[] buf = new byte[64]; // line length
            int cnt = 0, c;
            while ((c = read()) != -1)
            {
                if (c == '\n')
                    break;
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }

        private void fillBuffer() throws IOException
        {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }

        private byte read() throws IOException
        {
            if (bufferPointer == bytesRead)
                fillBuffer();
            return buffer[bufferPointer++];
        }
    }

    public static void main(String[] args) {
        Speed dataProcessor = new Speed();
        int answer = dataProcessor.processData(args[0]);
        System.out.println(answer);
    }
}

最佳答案

你的素数列表应该从 3 开始,而不是 2。乘以 2 会导致冲突增加:每次都会丢失一位数据。在 64 个字符之后,无论字符串中还有哪些其他字符,哈希代码都是 0。

至于代码的其余部分,使用 BufferedReader 逐行读取文件要容易得多。它增加的开销是最小的。

关于java - 是否有更好的方法对由 128 个合法 ASCII 字符组成的字符串进行哈希排列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60917424/

相关文章:

java - 为什么操作系统使用 "path"以及为什么编译器和 JVM 使用 "classpath"?

java - 使用 Android Studio 转移计算机,现在出现 Gradle 错误

java - 使用 Jpa 和 Thymeleaf 在 Spring Boot 中重定向页面

java - 将 int 设置为数组中的空白

java - 替换 hashmap java 中的重复值字符串

java - HashMap 的自定义哈希码/等于操作

haskell - 使用 GHC API 评估 Haskell 语句/表达式

python - Kotlin:从文件加载包含字符串和元组的字典( HashMap )

java - 将类转换为不相关的接口(interface)

java - 使用 JNI 获取运行时进程的 PID