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

本文共 2778 字,大约阅读时间需要 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();        }    }

转载于:https://www.cnblogs.com/licheng/archive/2008/08/08/1263651.html

你可能感兴趣的文章
1.3节 逻辑门与二进制数 part2
查看>>
不重装系统修复系统的一些实例
查看>>
异步GEI (2) 线程
查看>>
通过管理控制台和命令行两种方式新建邮箱数据库(exchange2010)
查看>>
网卡设置(设置IP地址、网关、DNS)
查看>>
linux之sed用法
查看>>
HBTC2012 参会感受
查看>>
如何愉快的使用MQ-详述各种功能场景
查看>>
SQL查询语句中的 limit 与 offset 的区别
查看>>
hadoop SequenceFile介绍 大数据 存储
查看>>
手动订制一个基于BusyBox的微型Linux系统
查看>>
TCP/IP协议和Socket编程
查看>>
lnmp(new)
查看>>
使用fastjson时出现$ref: "$.list[2]"的解决办法(重复引用)
查看>>
ZooKeeper观察节点
查看>>
关系图报错"dataIndex undefined"
查看>>
[python] 各种ERROR
查看>>
利用Maven搭建Spring开发环境
查看>>
Swift重写set和get以及willSet和didSet介绍
查看>>
oracle分区表的迁移
查看>>