Java根據(jù)表達(dá)式獲取對(duì)象中的值及設(shè)置值的例子
表達(dá)式解析-BeanPath
由來(lái)
很多JavaBean嵌套著很多層對(duì)象,這其中還夾雜著Map、Collection等對(duì)象,因此獲取太深的嵌套對(duì)象會(huì)讓代碼變得冗長(zhǎng)不堪。因此我們可以考慮使用一種表達(dá)式來(lái)獲取指定深度的對(duì)象,于是BeanResolver應(yīng)運(yùn)而生。
原理
通過(guò)傳入一個(gè)表達(dá)式,按照表達(dá)式的規(guī)則獲取bean中指定的字段值。
表達(dá)式分為兩種:
- .表達(dá)式,可以獲取Bean對(duì)象中的屬性(字段)值或者M(jìn)ap中key對(duì)應(yīng)的值
- []表達(dá)式,可以獲取集合等對(duì)象中對(duì)應(yīng)index的值
栗子:
- person 獲取Bean對(duì)象下person字段的值,或者Bean本身如果是Person對(duì)象,返回本身。
- person.name 獲取Bean中person字段下name字段的值,或者Bean本身如果是Person對(duì)象,返回其name字段的值。
- persons[3] 獲取persons字段下第三個(gè)元素的值(假設(shè)person是數(shù)組或Collection對(duì)象)
- person.friends[5].name 獲取person字段下friends列表(或數(shù)組)的第5個(gè)元素對(duì)象的name屬性
使用
由于嵌套Bean定義過(guò)于復(fù)雜,在此我們省略,有興趣的可以看下這里:cn.hutool.core.lang.test.bean(src/test/java下)下定義了測(cè)試用例用的bean。
首先我們創(chuàng)建這個(gè)復(fù)雜的Bean(實(shí)際當(dāng)中這個(gè)復(fù)雜的Bean可能是從數(shù)據(jù)庫(kù)中獲取,或者從JSON轉(zhuǎn)入)
這個(gè)復(fù)雜Bean的關(guān)系是這樣的:
定義一個(gè)Map包含用戶信息(UserInfoDict)和一個(gè)標(biāo)志位(flag),用戶信息包括一些基本信息和一個(gè)考試信息列表(ExamInfoDict)。
//------------------------------------------------- 考試信息列表
ExamInfoDict examInfoDict = new ExamInfoDict();
examInfoDict.setId(1);
examInfoDict.setExamType(0);
examInfoDict.setAnswerIs(1);
ExamInfoDict examInfoDict1 = new ExamInfoDict();
examInfoDict1.setId(2);
examInfoDict1.setExamType(0);
examInfoDict1.setAnswerIs(0);
ExamInfoDict examInfoDict2 = new ExamInfoDict();
examInfoDict2.setId(3);
examInfoDict2.setExamType(1);
examInfoDict2.setAnswerIs(0);
List<ExamInfoDict> examInfoDicts = new ArrayList<ExamInfoDict>();
examInfoDicts.add(examInfoDict);
examInfoDicts.add(examInfoDict1);
examInfoDicts.add(examInfoDict2);
//------------------------------------------------- 用戶信息
UserInfoDict userInfoDict = new UserInfoDict();
userInfoDict.setId(1);
userInfoDict.setPhotoPath("yx.mm.com");
userInfoDict.setRealName("張三");
userInfoDict.setExamInfoDict(examInfoDicts);
Map<String, Object> tempMap = new HashMap<String, Object>();
tempMap.put("userInfo", userInfoDict);
tempMap.put("flag", 1);
下面,我們使用BeanPath獲取這個(gè)Map下此用戶第一門(mén)考試的ID:
BeanPath resolver = new BeanPath("userInfo.examInfoDict[0].id");
Object result = resolver.get(tempMap);//ID為1
只需兩句(甚至一句)即可完成復(fù)雜Bean中各層次對(duì)象的獲取。說(shuō)明: 為了簡(jiǎn)化BeanPath的使用,Hutool在BeanUtil中也加入了快捷入口方法:BeanUtil.getProperty,這個(gè)方法的命名更容易理解(畢竟BeanPath不但可以解析Bean,而且可以解析Map和集合)。
例子
上面的文章是從糊涂官網(wǎng)上直接截取的。缺少了設(shè)置值的過(guò)程https://doc.hutool.cn/pages/BeanPath/#%E5%8E%9F%E7%90%86
下面我自己嘗試的一些使用示例,供大家參考
Map<String, Object> map = new HashMap<>();
map.put("name", "張三");
map.put("age", 18);
Map<String, Object> score = new HashMap<>();
score.put("math", 100);
score.put("english", 99);
List<String> likeFood = new ArrayList<>();
likeFood.add("apple");
likeFood.add("banana");
map.put("score", score);
map.put("likeFood", likeFood);
BeanPath matchScorePath = BeanPath.create("score.math");
BeanPath likeFoodPath = BeanPath.create("likeFood[1]");
// 輸出結(jié)果
System.out.println("matchScore"+matchScorePath.get(map));
System.out.println("likeFood"+likeFoodPath.get(map));
// 設(shè)置值
matchScorePath.set(map, 90);
likeFoodPath.set(map, "orange");
System.out.println(((Map<String,Object>)map.get("score")).get("math"));
System.out.println(((List)map.get("likeFood")).get(1));
// 設(shè)置不存在的值
BeanPath likeFoodPath1 = BeanPath.create("likeFood[2]");
likeFoodPath1.set(map, "pear");
System.out.println(((List)map.get("likeFood")).get(2));到此這篇關(guān)于Java根據(jù)表達(dá)式獲取對(duì)象中的值,設(shè)置值的文章就介紹到這了,更多相關(guān)java獲取對(duì)象中的值內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java工程中使用Mybatis (工程結(jié)合Mybatis,數(shù)據(jù)結(jié)合Swing使用))
這篇文章主要介紹了Java工程中使用Mybatis (工程結(jié)合Mybatis,數(shù)據(jù)可以結(jié)合Swing使用),需要的朋友可以參考下2017-04-04
java中的Timer和Timertask的關(guān)系解讀
本文詳細(xì)介紹了Java中的Timer和TimerTask類(lèi),包括它們之間的關(guān)系、API的使用方法、注意事項(xiàng)以及操作案例,Timer是一個(gè)調(diào)度器,而TimerTask是具體的任務(wù)類(lèi),Timer僅對(duì)應(yīng)一個(gè)線程,不保證任務(wù)執(zhí)行的精確性,但線程安全,一個(gè)Timer可以調(diào)度多個(gè)TimerTask2024-12-12
Mybatis自動(dòng)創(chuàng)建表和更新表結(jié)構(gòu)
這篇文章主要介紹了Mybatis自動(dòng)創(chuàng)建表和更新表結(jié)構(gòu)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
詳解PipedInputStream和PipedOutputStream_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了管道PipedInputStream和PipedOutputStream,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
用Java實(shí)現(xiàn)簡(jiǎn)單ATM機(jī)功能
這篇文章主要為大家詳細(xì)介紹了用Java實(shí)現(xiàn)簡(jiǎn)單ATM機(jī)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
通過(guò)volatile驗(yàn)證線程之間的可見(jiàn)性
這篇文章主要介紹了通過(guò)volatile驗(yàn)證線程之間的可見(jiàn)性,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10

