It only takes me 24 seconds to implement batch assignment of Map values!

The function of the function is to batch assign a set of key-value pairs to the keys in the Map. In Java, Map's put method is usually used to assign key-value pairs to the Map one by one. However, in some scenarios, it may be necessary to assign multiple key-value pairs to the Map at once.

Function: Map batch assignment

Parameter 1: Parameter name: target; Parameter type: Map; Parameter description: Map object

Parameter 2: Parameter name: keyAndValue; Parameter type: Object; Parameter description: key value and value value, corresponding one after another (key=2n, value=2n+1)

 

Suppose you have an ordering system that needs to create orders based on form data submitted by users. In this case, you can use the above Java function to implement the function of batch assignment of form data to the order object.

 

Specifically, when the order system receives the form data submitted by the user, it can call the above Java function and pass the order object as the target Map object to the function. At the same time, pass the key-value pairs in the form data as parameters to the function. The function will traverse the array of key-value pairs and assign each key-value pair to the target Map object, which is the order object.

 

By using the above Java functions, the order system can easily assign values ​​to the form data submitted by users in batches without having to manually set the properties of the order object one by one. This can improve development efficiency and code maintainability, and ensure the consistency between the order object and the form data.

 

At this time, how will you implement this function? Written by hand? Or with the help of AI tools?

 

Next, let’s take a look at how these two methods implement this function, and what are their advantages and disadvantages? Perhaps comparative analysis can provide you with a key to delivering high-quality code efficiently.

 

  • manual code

 

@MethodDesc(desc = "Map batch assignment function", params = {

        @ParamDesc(name = "target", desc = "target Map object"),

        @ParamDesc(name = "keyAndValue", desc = "key and Value parameters")})

public static Map batchSetValue(Map target, Object... keyAndValue) {

    try {

        Object key = "";

        int j = 2;

        if (keyAndValue.length % j != 0) {

            throw new FlowTaskException("The number of keys and values ​​passed in are inconsistent, please check yourself");

        }

        for (int i = 0; i < keyAndValue.length; i++) {

            if (i % j == 0) {

                key = keyAndValue[i];

                if (Objects.isNull(key) || !(key instanceof String)) {

                    throw new FlowTaskException("Map batch assignment exception, the incoming key is not a string type or an empty object, please check key=" + key);

                }

            } else {

                if (Objects.nonNull(keyAndValue[i]) && "null".equals(keyAndValue[i])) {

                    target.put(key, null);

                } else {

                    target.put(key, keyAndValue[i]);

                }

            }

        }

        return target;

    } catch (Exception e) {

        throw e;

    }

}

 

  • With AI tools

Try it by using FuncGPT (Hui Function) launched by Feisuan SoFlu software robot, which focuses on AI-generated Java functions. In just 24 seconds, FuncGPT automatically generates the following code:

// Class name: MapUtil

// Function name: bulkAssignValues

// Function: Assign key-value pairs to Map objects in batches

// POM dependency package:

// <dependency>

//     <groupId>org.apache.commons</groupId>

//     <artifactId>commons-lang3</artifactId>

//     <version>3.12.0</version>

// </dependency>

 

import org.apache.commons.collections4.MapUtils;

 

import java.util.Map;

 

public class MapUtil {

    /**

     * Assign key-value pairs to Map objects in batches

     * @param target target Map object

     * @param keyAndValue key-value pairs, corresponding one after another (key=2n, value=2n+1)

     */

    public static void bulkAssignValues(Map<Object, Object> target, Object... keyAndValue) {

        if (target == null) { // Check whether the target Map is empty

            throw new IllegalArgumentException("Target Map cannot be null"); // Throws an exception, the target Map cannot be null

        }

 

        if (keyAndValue == null || keyAndValue.length % 2 != 0) { // Check whether the key-value pair parameter is empty or whether the number of key-value pairs is an even number

            throw new IllegalArgumentException("Invalid key-value pairs"); // Throws an exception, the key-value pair parameter is illegal

        }

 

        for (int i = 0; i < keyAndValue.length; i += 2) {

            Object key = keyAndValue[i];

            Object value = keyAndValue[i + 1];

            target.put(key, value); // Assign the key-value pair to the target Map object

        }

    }

}

 

// Function example

// Example of assigning key-value pairs to Map objects in batches

//Input parameters: target, target Map object

//Input parameters: keyAndValue, key-value pairs, corresponding one after another (key=2n, value=2n+1)

// Call example:

// Map<String, Integer> targetMap = new HashMap<>();

// MapUtil.bulkAssignValues(targetMap, "key1", 1, "key2", 2, "key3", 3);

// System.out.println(targetMap);

//Output result: For example, assign the key-value pair to the Map object, the result is: {key1=1, key2=2, key3=3}

//The output result is: {key1=1, key2=2, key3=3}

 

It can be seen from the above comparison:

One final word: AI technology is sweeping across thousands of industries, and AI programming tools are emerging one after another. Developers need to choose appropriate tools based on their actual personal needs to help them improve work efficiency and break the boundaries of capabilities.

This tool is free to use link: https://suo.im/d5lOT

The web version of Windows 12 deepin-IDE compiled by junior high school students was officially unveiled. It is known as "truly independently developed" QQ has achieved "three-terminal simultaneous updates", and the underlying NT architecture is based on Electron QQ for Linux officially released 3.2.0 "Father of Hongmeng" Wang Chenglu : Hongmeng PC version system will be launched next year to challenge ChatGPT, these 8 domestic AI large model products GitUI v0.24.0 are released, the default wallpaper of Ubuntu 23.10, a Git terminal written in Rust, is revealed, the "Tauren" in the maze JetBrains announces the WebStorm 2023.3 roadmap China Human Java Ecosystem, Solon v2.5.3 released
{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/4868096/blog/10108756