Properties的遍历几种方式

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中,之后直接用增强foreach来操作,但是只是针对key,value都是String类型的情况下,这个是重点。

  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如果有一个不是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);
                // 这里就是关键了
                if (k instanceof String && v instanceof String) {
                    h.put((String) k, (String) v);
                }
            }
        }
    
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值