iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >详解Java集合类之HashTable,Properties篇
  • 198
分享到

详解Java集合类之HashTable,Properties篇

2024-04-02 19:04:59 198人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

目录1.基本介绍2.HashTable底层3.HashTable扩容机制4.HashMap和HashTable的对比5.Properties6.集合选型规则1.基本介绍 HashTa

1.基本介绍

HashTable的键和值都不能为空,否则会抛出一个异常

使用方法基本与HashMap一致

HashTable是线程安全的,HashMap是线程不安全的

2.HashTable底层

先上代码:

Hashtable hashtable = new Hashtable();
hashtable.put("john",100);
hashtable.put("tom",250);
hashtable.put("tom",1314);
System.out.println(hashtable);

输出:

{tom=1314, john=100}

先进入put方法,可以看到在put方法最前面先判断了value是否为空,如果为空直接抛出一个异常

if (value == null) {
    throw new NullPointerException();
}

同样的,HashTable也存在替换机制和扩容机制!

3.HashTable扩容机制

HashTable拥有自己的扩容机制,这不同于HashSet和HashMap

首先,我们要明白,在HashTable添加键值对时,真正起到添加作用的是如下方法:

addEntry(hash, key, value, index);

我们来看一下他的真面目:

private void addEntry(int hash, K key, V value, int index) {
    Entry<?,?> tab[] = table;
    if (count >= threshold) {
        // Rehash the table if the threshold is exceeded
        rehash();

        tab = table;
        hash = key.hashCode();
        index = (hash & 0x7FFFFFFF) % tab.length;
    }

    // Creates the new entry.
    @SuppressWarnings("unchecked")
    Entry<K,V> e = (Entry<K,V>) tab[index];
    tab[index] = new Entry<>(hash, key, value, e);
    count++;
    modCount++;
}

当添加的元素数量大于临界值时,执行rehash方法(这个方法就是真正的扩容方法)

if (count >= threshold) {
    // Rehash the table if the threshold is exceeded
    rehash();
    tab = table;
    hash = key.hashCode();
    index = (hash & 0x7FFFFFFF) % tab.length;
}

继续追进去到rehash方法:

protected void rehash() {
    int oldCapacity = table.length;
    Entry<?,?>[] oldMap = table;

    // overflow-conscious code
    int newCapacity = (oldCapacity << 1) + 1;
    if (newCapacity - MAX_ARRAY_SIZE > 0) {
        if (oldCapacity == MAX_ARRAY_SIZE)
            // Keep running with MAX_ARRAY_SIZE buckets
            return;
        newCapacity = MAX_ARRAY_SIZE;
    }
    Entry<?,?>[] newMap = new Entry<?,?>[newCapacity];

    modCount++;
    threshold = (int)Math.min(newCapacity * loadFactor, MAX_ARRAY_SIZE + 1);
    table = newMap;

    for (int i = oldCapacity ; i-- > 0 ;) {
        for (Entry<K,V> old = (Entry<K,V>)oldMap[i] ; old != null ; ) {
            Entry<K,V> e = old;
            old = old.next;

            int index = (e.hash & 0x7FFFFFFF) % newCapacity;
            e.next = (Entry<K,V>)newMap[index];
            newMap[index] = e;
        }
    }
}

不要慌,我们来分析一下这个扩容方法

首先,拿到老的容量:

int oldCapacity = table.length;

新的容量为老的容量 * 2 + 1:

int newCapacity = (oldCapacity << 1) + 1;

继续往下,到达真正扩容的代码:

Entry<?,?>[] newMap = new Entry<?,?>[newCapacity];

4.HashMap和HashTable的对比

5.Properties

Properties继承了HashTable

一般用于可操作的配置文件编写

使用实例:

import java.util.Properties;


public class PropertiesText {
    @SuppressWarnings({"all"})
    public static void main(String[] args) {
        Properties properties = new Properties();
        // 增加
        properties.put("john",521);
        properties.put("tom",1314);
        properties.put("tom",100);
        System.out.println(properties);
        // 通过key获取值
        System.out.println(properties.get("tom"));
        // 删除
        properties.remove("tom");
        System.out.println(properties);
    }
}

输出:

{tom=100, john=521}
100
{john=521}

6.集合选型规则

存储一组对象:Collection

允许重复,增删多选LinkedList,改查多选ArrayList

不允许重复,无序选HashSet,排序选TreeSet,插入和取出顺序一致选择LinkedHashSet

存储键值对:Map

键无序:HashMap

键排序:TreeMap

键插入和取出顺序一致:LinkedHashMap

读取文件:Properties

到此这篇关于详解Java集合类之HashTable,Properties篇的文章就介绍到这了,更多相关Java集合类HashTable Properties内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: 详解Java集合类之HashTable,Properties篇

本文链接: https://www.lsjlt.com/news/165336.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作