博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Understand Hash Table.
阅读量:6697 次
发布时间:2019-06-25

本文共 2710 字,大约阅读时间需要 9 分钟。

Creating a Hash Table
A hash table, or
map, holds key/value pairs.
// Create a hash table    Map map = new HashMap();    // hash table    map = new TreeMap();        // sorted map        // Add key/value pairs to the map    map.put(, );    map.put(, );    map.put(, );        // Get number of entries in map    int size = map.size();        // 2        // Adding an entry whose key exists in the map causes    // the new value to replace the old value    Object oldValue = map.put(, );  // 1        // Remove an entry from the map and return the value of the removed entry    oldValue = map.remove();  // 3        // Iterate over the keys in the map    Iterator it = map.keySet().iterator();    while (it.hasNext()) {        // Get key        Object key = it.next();    }        // Iterate over the values in the map    it = map.values().iterator();    while (it.hasNext()) {        // Get value        Object value = it.next();    } Creating a Map That Retains Order-of-Insertion
Map map = new LinkedHashMap();        // Add some elements    map.put(, );    map.put(, );    map.put(, );    map.put(, );        // List the entries    for (Iterator it=map.keySet().iterator(); it.hasNext(); ) {        Object key = it.next();        Object value = map.get(key);    }    // [1=value1, 2=value4, 3=value3] Automatically Removing an Unreferenced Element from a Hash TableWhen a key is added to a map, the map will prevent the key from being garbage-collected. However, a  will automatically remove a key if the key is not being referenced by any other object. An example where this type of map might be useful is a registry where a registrant is automatically removed after it is garbage-collected.
// Create the weak map    Map weakMap = new WeakHashMap();        // Add a key to the weak map    weakMap.put(, );        // Get all keys that are still being referenced    Iterator it = weakMap.keySet().iterator();    while (it.hasNext()) {        // Get key        Object key = it.next();    }
The weak map does not automatically release the value if it is no longer used. To enable automatically release of the value, the value must be wrapped in a
WeakReference object:
WeakReference weakValue = new WeakReference();    weakMap.put(, weakValue);        // Get all keys that are still being referenced and check whether    // or not the value has been garbage-collected    it = weakMap.keySet().iterator();    while (it.hasNext()) {        // Get key        Object key = it.next();            weakValue = (WeakReference)weakMap.get(key);        if (weakValue == null) {            // Value has been garbage-collected        } else {            // Get value            valueObject = weakValue.get();        }    }

转载地址:http://fcvoo.baihongyu.com/

你可能感兴趣的文章
网站平台架构演变史(三) - 数据库表的查询优化
查看>>
fastdfs 图片服务器 使用java端作为客户端上传图片
查看>>
步步为营 .NET 代码重构学习笔记 三、内联方法(Inline Method)
查看>>
前端地址大全
查看>>
DAY19-Django之model进阶
查看>>
从0移植uboot(六) _实现网络功能
查看>>
Linux命令——du
查看>>
Cube Stacking
查看>>
WPF中DataGrid的ComboBox的简单绑定方式(绝对简单)
查看>>
UIViewContentMode各类型效果
查看>>
转:开启nginx的gzip压缩的相关参数设置
查看>>
转:网站架构-从无到有
查看>>
MUI的一些笔记
查看>>
Jenkins可持续集成Python自动化脚本
查看>>
Linux系统起源及主流发行版
查看>>
跨域问题、跨域cookie问题
查看>>
smarty获取php中的变量
查看>>
GIL 锁
查看>>
平衡二叉树
查看>>
linux中wget 、apt-get、yum rpm区别
查看>>