日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

Properties的遍歷幾種方式

作者:搏·夢 更新時(shí)間: 2022-05-25 編程語言

文章目錄

  • 1. 前言
  • 2. 正片
    • 1. keySet()方式
    • 2. entrySet()方式
    • 3. propertyNames()方式
    • 4. stringPropertyNames()方式

1. 前言

  1. 無意有次需求是遍歷Properties對象,但突然就不知道怎么遍歷了,因此寫文章記錄一下。

2. 正片

  1. Properties 是鍵值對 存儲的,key—value,因此遍歷方式跟Map基本一致。

1. keySet()方式

public class demo {
    public static void main(String[] args) {
        Properties properties = new Properties();
        properties.put("1", "11");
        properties.put("2", "22");
        properties.put("3", "31");
        properties.put("4", "41");

        Set<Object> keySet = properties.keySet();
        for (Object key : keySet) {
            System.out.println(key + ": " + properties.get(key));
        }
    }
}

在這里插入圖片描述

2. entrySet()方式

public class demo {
    public static void main(String[] args) {
        Properties properties = new Properties();
        properties.put("1", "11");
        properties.put("2", "22");
        properties.put("3", "31");
        properties.put("4", "41");

        Set<Map.Entry<Object, Object>> entries = properties.entrySet();
        for (Map.Entry<Object, Object> map : entries) {
            System.out.println(map.getKey() + ":" + map.getValue());
        }
    }
}

在這里插入圖片描述

3. propertyNames()方式

public class demo {
    public static void main(String[] args) {
        Properties properties = new Properties();
        properties.put("1", "11");
        properties.put("2", "22");
        properties.put("3", "31");
        properties.put("4", "41");

		// 就是迭代器
        Enumeration<?> enumeration = properties.propertyNames();
        while (enumeration.hasMoreElements()) {
            Object key = enumeration.nextElement();
            System.out.println(key + ":" + properties.get(key));
        }
    }
}

在這里插入圖片描述

4. stringPropertyNames()方式

  1. stringPropertyNames() 是把key全部獲取封裝到Set中,之后直接用增強(qiáng)foreach來操作,但是只是針對key,value都是String類型的情況下,這個(gè)是重點(diǎn)。

  2. 下面是正常使用情況:

    public class demo {
        public static void main(String[] args) {
            Properties properties = new Properties();
            properties.put("1", "11");
            properties.put("2", "22");
            properties.put("3", "31");
            properties.put("4", "41");
    
            Set<String> keys = properties.stringPropertyNames();
            for (String key : keys) {
                System.out.println(key + ":" + properties.get(key));
            }
        }
    }
    

    在這里插入圖片描述

  3. 如果我把key,value 改成不是String 類型的,如下:

    public class demo {
        public static void main(String[] args) {
            Properties properties = new Properties();
            properties.put(1, "11");
            properties.put("2", 22);
            properties.put("3", 31);
            properties.put(4, "41");
    
            Set<String> keys = properties.stringPropertyNames();
            System.out.println("keys的size:" + keys.size());
            for (String key : keys) {
                System.out.println(key + ":" + properties.get(key));
            }
        }
    }
    

    在這里插入圖片描述
    由上可以看出,key,value如果有一個(gè)不是String類型,則不會接受。我們從源碼角度來看:

    public Set<String> stringPropertyNames() {
            Hashtable<String, String> h = new Hashtable<>();
            enumerateStringProperties(h);
            return h.keySet();
    }
    
    
     private synchronized void enumerateStringProperties(Hashtable<String, String> h) {
            if (defaults != null) {
                defaults.enumerateStringProperties(h);
            }
            for (Enumeration<?> e = keys() ; e.hasMoreElements() ;) {
                Object k = e.nextElement();
                Object v = get(k);
                // 這里就是關(guān)鍵了
                if (k instanceof String && v instanceof String) {
                    h.put((String) k, (String) v);
                }
            }
        }
    

原文鏈接:https://blog.csdn.net/xueyijin/article/details/123968385

欄目分類
最近更新