網(wǎng)站首頁 編程語言 正文
文章目錄
- 1. 前言
- 2. 正片
- 1. keySet()方式
- 2. entrySet()方式
- 3. propertyNames()方式
- 4. stringPropertyNames()方式
1. 前言
- 無意有次需求是遍歷Properties對象,但突然就不知道怎么遍歷了,因此寫文章記錄一下。
2. 正片
- 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()方式
-
stringPropertyNames() 是把key全部獲取封裝到Set中,之后直接用增強(qiáng)foreach來操作,但是只是針對key,value都是String類型的情況下,這個(gè)是重點(diǎn)。
-
下面是正常使用情況:
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)); } } }
-
如果我把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
相關(guān)推薦
- 2022-08-21 Golang中slice刪除元素的性能對比_Golang
- 2023-05-29 linux?rename?批量修改文件名的操作方法_linux shell
- 2022-05-05 python?scipy.spatial.distance?距離計(jì)算函數(shù)??_python
- 2023-01-15 關(guān)于networkx返回圖的鄰接矩陣問題_python
- 2022-08-05 C語言示例講解if?else語句的用法_C 語言
- 2022-04-30 C語言實(shí)現(xiàn)考勤管理系統(tǒng)_C 語言
- 2022-12-11 centos離線安裝mongodb-database-tools方法詳解_MongoDB
- 2022-04-17 WPF框架Prism中ViewModelLocator用法介紹_基礎(chǔ)應(yīng)用
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支