SoFunction
Updated on 2025-04-14

Java obtains values ​​in objects and sets values ​​based on expressions

Expression parsing - BeanPath

origin

Many JavaBeans are nested with many layer objects, which are also mixed with Map, Collection and other objects. Therefore, obtaining nested objects that are too deep will make the code verbose. Therefore, we can consider using an expression to obtain objects of a specified depth, so BeanResolver came into being.

principle

By passing in an expression, obtain the field value specified in the bean according to the expression's rules.

There are two types of expressions:

  • .Expression, which can obtain the attribute (field) value in the Bean object or the value corresponding to the key in the Map
  • [] Expression, you can get the corresponding index value in the set and other objects

chestnut:

  • person Gets the value of the person field under the Bean object, or if the bean itself is a Person object, return it.
  • Get the value of the name field under the person field in the bean, or if the bean itself is a Person object, return the value of its name field.
  • persons[3] Get the value of the third element under the persons field (assuming that person is an array or Collection object)
  • [5].name Gets the name attribute of the 5th element object of the friends list (or array) under the person field.

use

Since the definition of nested beans is too complex, we omit it here. If you are interested, you can see here: (under src/test/java) defines the beans used for test cases.
First, we create this complex bean (in fact, this complex bean may be retrieved from the database or transferred from JSON)
The relationship of this complex bean is as follows:
Define a Map that contains user information (UserInfoDict) and a flag (flag), which includes some basic information and an exam information list (ExamInfoDict).

//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ExamInfoDict examInfoDict = new ExamInfoDict();
(1);
(0);
(1);
ExamInfoDict examInfoDict1 = new ExamInfoDict();
(2);
(0);
(0);
ExamInfoDict examInfoDict2 = new ExamInfoDict();
(3);
(1);
(0);
List<ExamInfoDict> examInfoDicts = new ArrayList<ExamInfoDict>();
(examInfoDict);
(examInfoDict1);
(examInfoDict2);
//-----------------------------------------------------------------------------------------------------------------------------UserInfoDict userInfoDict = new UserInfoDict();
(1);
("");
("Zhang San");
(examInfoDicts);
Map<String, Object> tempMap = new HashMap<String, Object>();
("userInfo", userInfoDict);
("flag", 1);
under,We useBeanPathGet thisMapThe first exam for this userID:
BeanPath resolver = new BeanPath("[0].id");
Object result = (tempMap);//ID is 1Just two sentences(Even one sentence)ComplicatedBeanObtaining objects at each level。

Note: In order to simplify the use of BeanPath, Hutool also adds a shortcut entry method to BeanUtil: The naming of this method is easier to understand (after all, BeanPath can not only parse beans, but also parse maps and collections).

example

The above article was directly intercepted from Hutu’s official website. Missing process for setting values/pages/BeanPath/#%E5%8E%9F%E7%90%86
Below are some usage examples I have tried for your reference

  		Map<String, Object> map = new HashMap<>();
        ("name", "Zhang San");
        ("age", 18);
        Map<String, Object> score = new HashMap<>();
        ("math", 100);
        ("english", 99);
        List<String> likeFood = new ArrayList<>();
        ("apple");
        ("banana");
        ("score", score);
        ("likeFood", likeFood);
        BeanPath matchScorePath = ("");
        BeanPath likeFoodPath = ("likeFood[1]");
        // Output result        ("matchScore"+(map));
        ("likeFood"+(map));
        // Set value        (map, 90);
        (map, "orange");
        (((Map<String,Object>)("score")).get("math"));
        (((List)("likeFood")).get(1));
        // Set a value that does not exist        BeanPath likeFoodPath1 = ("likeFood[2]");
        (map, "pear");
        (((List)("likeFood")).get(2));

This is the article about Java obtaining values ​​in objects based on expressions and setting values. For more related Java obtaining values ​​in objects, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!