相关疑难解决方法(0)

对象的地址在其生命周期中是否已修复?

对象的地址在其生命周期中是不变的还是可以改变?我只是觉得一个对象的地址永远不会改变.它是JVM依赖的吗?我没有找到任何明确的规格.

java memory jvm object memory-address

9
推荐指数
1
解决办法
202
查看次数

HashMap应该是未分类的,但仍然按键排序

根据这些:

  1. http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html
  2. HashMap,LinkedHashMap和TreeMap之间的区别
  3. java beginner:密钥如何在哈希映射中排序?

HashMapJava应该是未排序的,但它被相对于分类Key.

我认为这是一个问题,因为我需要插入订单数据.所以,我用了LinkedHashMap.但我仍然困惑为什么HashMap排序它.

有人能解释一下吗?

我做了一个简单的例子来查看排序.

public static void main(String[] args) {

        HashMap<Integer, String> newHashMap = new HashMap<Integer, String>();
        newHashMap.put(2, "First");
        newHashMap.put(0, "Second");
        newHashMap.put(3, "Third");
        newHashMap.put(1, "Fourth");

        Iterator<Entry<Integer, String>> iterator = newHashMap.entrySet()
                .iterator();
        while (iterator.hasNext()) {

            Map.Entry<Integer, String> entry = iterator.next();
            System.out.println("Key: " + entry.getKey());
            System.out.println("Value: " + entry.getValue());
            iterator.remove();
        }

    }
Run Code Online (Sandbox Code Playgroud)

结果:

Key: 0
Value: Second
Key: 1
Value: Fourth
Key: 2
Value: First
Key: 3 …
Run Code Online (Sandbox Code Playgroud)

java sorting hashmap data-structures

4
推荐指数
1
解决办法
3168
查看次数

标签 统计

java ×2

data-structures ×1

hashmap ×1

jvm ×1

memory ×1

memory-address ×1

object ×1

sorting ×1