e337. associating a value with an object.txt

来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 15 行

TXT
15
字号
This example demonstrates how to associate a value with an arbitrary object. The technique involves saving the object and the associated value as a key/value pair in an IdentityHashMap. A HashMap cannot be used for this purpose since if two objects happen to equal via the Object.equals() method, one of the objects will not be stored. 
    // Create the map
    Map objMap = new IdentityHashMap();
    
    // Add the object and value pair to the map
    Object o1 = new Integer(123);
    Object o2 = new Integer(123);
    objMap.put(o1, "first");
    objMap.put(o2, "second");
    
    // Retrieve the value associated with the objects
    Object v1 = objMap.get(o1);     // first
    Object v2 = objMap.get(o2);     // second

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?