java集合类史上最细讲解 - HashTable,Properties篇

简介: 1.基本介绍HashTable的键和值都不能为空,否则会抛出一个异常使用方法基本与HashMap一致HashTable是线程安全的,HashMap是线程不安全的


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}


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


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


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


3.HashTable扩容机制


HashMapHashSet拥有自己的扩容机制,这不同于HashTable添加键值对时,真正起到添加作用的是如下方法: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方法:


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;
}


首先,拿到老的容量:


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;
/**
 * 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);  // {tom=100, john=521}
        // 通过key获取值
        System.out.println(properties.get("tom"));  // 100
        // 删除
        properties.remove("tom");
        System.out.println(properties);  // {john=521}
    }
}


6.集合选型规则🙌


存储一组对象:Collection


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

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

存储键值对:Map


键无序:HashMap

键排序:TreeMap

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

读取文件:Properties


目录
相关文章
|
11天前
|
Java Linux
java基础(3)安装好JDK后使用javac.exe编译java文件、java.exe运行编译好的类
本文介绍了如何在安装JDK后使用`javac.exe`编译Java文件,以及使用`java.exe`运行编译好的类文件。涵盖了JDK的安装、环境变量配置、编写Java程序、使用命令行编译和运行程序的步骤,并提供了解决中文乱码的方法。
27 1
|
10天前
|
Java 索引
java基础(13)String类
本文介绍了Java中String类的多种操作方法,包括字符串拼接、获取长度、去除空格、替换、截取、分割、比较和查找字符等。
24 0
java基础(13)String类
|
4天前
|
Java API
Java的日期类都是怎么用的
【10月更文挑战第1天】本文介绍了 Java 中处理日期和时间的三个主要类:`java.util.Date`、`java.util.Calendar` 和 `java.time` 包下的新 API。`Date` 类用于表示精确到毫秒的瞬间,可通过时间戳创建或获取当前日期;`Calendar` 抽象类提供丰富的日期操作方法,如获取年月日及时区转换;`java.time` 包中的 `LocalDate`、`LocalTime`、`LocalDateTime` 和 `ZonedDateTime` 等类则提供了更为现代和灵活的日期时间处理方式,支持时区和复杂的时间计算。
26 14
|
8天前
|
安全 Java 编译器
java访问类字段
java访问类字段
|
10天前
|
Java
java的class类
java的class类
18 5
|
10天前
|
存储 安全 Java
Java 常用集合分类
Java 常用集合分类
13 2
|
11天前
|
Java 数据处理
Java Scanner 类详解
`Scanner` 类是 Java 中 `java.util` 包提供的强大工具,用于从多种输入源(如键盘、文件、字符串)读取数据。本文详细介绍如何创建 `Scanner` 对象并使用其常用方法(如 `next()`, `nextInt()`, `nextLine()` 等)。通过示例代码展示如何从标准输入、字符串及文件中读取数据,并进行输入验证。使用时需注意关闭 `Scanner` 以释放资源,并确保输入类型匹配,避免异常。掌握 `Scanner` 可显著提升程序的数据处理能力。
下一篇
无影云桌面