栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java集合框架Part12

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java集合框架Part12

目录
  • Properties基本介绍
  • Properties用法
  • Properties源码剖析

Properties基本介绍

Properties可以用于从*.properties中加载数据到Properties类对象,进行数据的读取和修改。

工作中*.properties通常作为配置文件。

Properties用法
public class Properties_ {
    public static void main(String[] args) {
        Properties properties = new Properties();
        //1.put
        //不能存null key或者null value。 public class Properties extends Hashtable
        //properties.put(null, "null key");
        //properties.put("null value", null);//Exception in thread "main" java.lang.NullPointerException
        properties.put("1001", "jack");//Exception in thread "main" java.lang.NullPointerException
        properties.put("1001", "lucky");//相同的key,后一个value会替换前一个value。
        properties.put("1002", "cara");
        properties.put("1003", "ada");
        System.out.println("=====put=====");
        System.out.println(properties);

        //2.get
        System.out.println("=====get=====");
        System.out.println(properties.get("1001"));

        //3.getProperty(String key)
        System.out.println("=====getProperty(String key)=====");
        System.out.println(properties.getProperty("1002"));

        //4.getProperty(String key, String defaultValue)
        System.out.println("=====getProperty(String key, String defaultValue)=====");
        System.out.println(properties.getProperty("1005", "user not exists"));

        //3.remove
        properties.remove("1003");
        System.out.println("=====remove=====");
        System.out.println(properties);
    }
}
=====put=====
{1003=ada, 1002=cara, 1001=lucky}
=====get=====
lucky
=====getProperty(String key)=====
cara
=====getProperty(String key, String defaultValue)=====
user not exists
=====remove=====
{1002=cara, 1001=lucky}
Properties源码剖析
public Properties() {
    this(null);
}
public Properties(Properties defaults) {
    this.defaults = defaults;
}
public Hashtable() {
    this(11, 0.75f);
}
public synchronized V put(K key, V value) {
    // Make sure the value is not null
    if (value == null) {
        throw new NullPointerException();
    }

    // Makes sure the key is not already in the hashtable.
    Entry tab[] = table;
    int hash = key.hashCode();
    int index = (hash & 0x7FFFFFFF) % tab.length;
    @SuppressWarnings("unchecked")
    Entry entry = (Entry)tab[index];
    for(; entry != null ; entry = entry.next) {
        //如果要插入的的key的hash值和准备插入的table索引位置的对象的hash值一样,并且equals方法相等,那么就执行替换操作
        if ((entry.hash == hash) && entry.key.equals(key)) {
            V old = entry.value;
            entry.value = value;//替换
            return old;
        }
    }

    addEntry(hash, key, value, index);
    return null;
}
private void addEntry(int hash, K key, V value, int index) {
    modCount++;

    Entry tab[] = table;
    if (count >= threshold) {
        // Rehash the table if the threshold is exceeded
        //如果table中的数据大于等于临界值,就会扩容
        rehash();

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

    // Creates the new entry.
    @SuppressWarnings("unchecked")
    Entry e = (Entry) tab[index];
    tab[index] = new Entry<>(hash, key, value, e);
    count++;
}
protected void rehash() {
    int oldCapacity = table.length;
    Entry[] oldMap = table;

    // overflow-conscious code
    //2*oldCapacity + 1
    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;

    //遍历旧数据,将原来的值再放到新的map上
    for (int i = oldCapacity ; i-- > 0 ;) {
        for (Entry old = (Entry)oldMap[i] ; old != null ; ) {
            Entry e = old;
            old = old.next;

            int index = (e.hash & 0x7FFFFFFF) % newCapacity;
            e.next = (Entry)newMap[index];
            newMap[index] = e;
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/835912.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号