java - 为什么要对哈希表进行排序?

标签 java hashmap

为什么哈希表(java.util.HashMap)是按longintbyte ?

请看下面的代码:

public class Main {

    private static final int INITIAL_CAPACITY = 10;

    public static void main(String[] args) {

        final Map<Long, Long> longMap = new HashMap<>(INITIAL_CAPACITY);
        final Map<Integer, Integer> integerMap = new HashMap<>(INITIAL_CAPACITY);
        final Map<Byte, Byte> byteMap = new HashMap<>(INITIAL_CAPACITY);
        final Map<Short, Short> shortMap = new HashMap<>(INITIAL_CAPACITY);

        final Map<Double, Double> doubleMap = new HashMap<>(INITIAL_CAPACITY);
        final Map<Float, Float> floatMap = new HashMap<>(INITIAL_CAPACITY);
        final Map<BigDecimal, BigDecimal> bigDecimalMap = new HashMap<>(INITIAL_CAPACITY);
        final Map<String, String> stringMap = new HashMap<>(INITIAL_CAPACITY);

        final Random random = new Random();

        for(int i=0; i < 100; i++){

            int value = random.nextInt(10);
            longMap.put(Long.valueOf(value), Long.valueOf(value));
            integerMap.put(Integer.valueOf(value), Integer.valueOf(value));
            byteMap.put(Byte.valueOf((byte)value), Byte.valueOf((byte)value));
            shortMap.put(Short.valueOf((short)value), Short.valueOf((short)value));

            doubleMap.put(Double.valueOf(value), Double.valueOf(value));
            floatMap.put(Float.valueOf(value), Float.valueOf(value));
            bigDecimalMap.put(BigDecimal.valueOf(value), BigDecimal.valueOf(value));
            stringMap.put(String.valueOf(value), String.valueOf(value));
        }

        System.out.println("\n========== SORTED ==========\n");
        System.out.println("Map<Long, Long>:             " + longMap);
        System.out.println("Map<Integer, Integer>:       " + integerMap);
        System.out.println("Map<Byte, Byte>:             " + byteMap);
        System.out.println("Map<Short, Short>:           " + shortMap);
        System.out.println("\n======== NOT SORTED ========\n");
        System.out.println("Map<Double, Double>:         " + doubleMap);
        System.out.println("Map<Float, Float>:           " + floatMap);
        System.out.println("Map<BigDecimal, BigDecimal>: " + bigDecimalMap);
        System.out.println("Map<String, String>: " + stringMap);
    }

}

输出这个程序:

========== SORTED ==========

Map<Long, Long>             : {0=0, 1=1, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9}
Map<Integer, Integer>       : {0=0, 1=1, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9}
Map<Byte, Byte>             : {0=0, 1=1, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9}
Map<Short, Short>           : {0=0, 1=1, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9}

======== NOT SORTED ========

Map<Double, Double>         : {0.0=0.0, 3.0=3.0, 6.0=6.0, 7.0=7.0, 2.0=2.0, 1.0=1.0, 4.0=4.0, 9.0=9.0, 8.0=8.0, 5.0=5.0}
Map<Float, Float>           : {1.0=1.0, 0.0=0.0, 4.0=4.0, 3.0=3.0, 5.0=5.0, 2.0=2.0, 8.0=8.0, 9.0=9.0, 7.0=7.0, 6.0=6.0}
Map<BigDecimal, BigDecimal> : {6=6, 0=0, 5=5, 9=9, 7=7, 8=8, 3=3, 4=4, 2=2, 1=1}
Map<String, String>         : {3=3, 2=2, 1=1, 0=0, 7=7, 6=6, 5=5, 4=4, 9=9, 8=8}

最佳答案

因为对于 LongIntegerByteShort 覆盖了 int hashCode() 方法很简单,返回数值本身。由于哈希码用于索引元素放置在 HashMap 中的特定桶,因此较低的哈希值会导致较低的索引。请注意,保留的顺序是显而易见的:通过向 HashMap 中添加其他元素,可能会将更多元素放置在同一个桶中,因此不能保证您看到的内容,这只是可能出现的副作用。 HashMap 不是排序集合,您不应该这样使用它TreeMap 是您需要排序对的方式。

对于 DoubleFloatBigIntegerString,哈希码函数不太重要,因此顺序不是保留。

关于java - 为什么要对哈希表进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19853148/

相关文章:

java - 为什么在Weka中使用libsvm时出现 "NoClassDefFoundError:libsvm/svm_print_interface"错误?

java - Log4j smtp appender 只为所有错误发送一封电子邮件

java - 如何在HashMap中创建和推送动态元素

java - 如何在不使用 Collections.sort() 的情况下按值对 LinkedHashMap 进行排序?

java - 具有快速查找功能的排序集(与 HashSet 一样快?)

java - HashMap 方法不理解它们是如何调用的

java - Vector 上的增强 for 循环中的空指针异常

java - Spring websocket 30分钟后自动关闭(超时)

java - 在 ExpectedExceptionMessageRegExp TestNG 中包含模式匹配

groovy - 拆分 map 的最常规方法是什么?