Home  >  Article  >  Java  >  What is the importance of accumulate() method in Java?

What is the importance of accumulate() method in Java?

王林
王林forward
2023-08-30 22:05:061192browse

What is the importance of accumulate() method in Java?

JSONObject is an unordered collection of name pairs and value pairs. Some important methods of JSONArray are accumulate(), put(), opt(), append(), write(), etc. accumulate() The method accumulates the values ​​under the key. This method is similar to the put() method, except that there are existing objects stored under the key. Then the JSONArray can be stored under the key to save all Cumulative value. If an existing JSONArray exists, new values ​​can be added.

Syntax

public JSONObject accumulate(java.lang.String key, java.lang.Object value) throws JSONException

Example

import org.json.*;
public class JSONAccumulateMethodTest {
   public static void main(String[] args) throws JSONException {
      JSONObject jsonObj = new JSONObject();
      jsonObj.accumulate("Technology", "Java");
      jsonObj.accumulate("Technology", "Python");
      jsonObj.accumulate("Technology", "Spark");
      jsonObj.accumulate("Technology", "Selenium");
      jsonObj.accumulate("Technology", ".Net");
      System.out.println(jsonObj.toString(3));
   }
}

Output

{"Technology": [
   "Java",
   "Python",
   "Spark",
   "Selenium",
   ".Net"
]}

The above is the detailed content of What is the importance of accumulate() method in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete