最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java根據(jù)表達(dá)式獲取對(duì)象中的值及設(shè)置值的例子

 更新時(shí)間:2025年03月31日 10:49:21   作者:zru_9602  
這篇文章主要介紹了Java根據(jù)表達(dá)式獲取對(duì)象中的值及設(shè)置值的例子,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧

表達(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)文章

最新評(píng)論

正镶白旗| 商都县| 南康市| 金沙县| 榕江县| 溆浦县| 安多县| 永新县| 和硕县| 开阳县| 抚顺县| 柳州市| 庆元县| 台前县| 冀州市| 五峰| 湖口县| 新兴县| 曲水县| 邳州市| 永福县| 交口县| 敦化市| 醴陵市| 岑巩县| 黑河市| 呼和浩特市| 荣昌县| 海阳市| 吴堡县| 文登市| 托克托县| 老河口市| 巴楚县| 资讯 | 永春县| 大港区| 洞口县| 高阳县| 安阳市| 祁阳县|