user3646858
user3646858

Reputation: 147

How to avoid unnecessary object creation in Java in a for loop

I am creating a following json file using java

JSONObject json = new JSONObject();
JSONArray vertex = new JSONArray();
for (int i = 0; i < num; i++) {
    JSONObject usr1 = new JSONObject();
    JSONObject usr2 = new JSONObject();

    // String na="name"+i
    usr1.put("type", "string");
    usr1.put("value", "name" + i);
    usr2.put("name", usr1);

    usr2.put("_id", Integer.toString(i));
    usr2.put("_type", "vertex");
    // v2.put(usr2);
    vertex.put(usr2);

    // email
    JSONObject usr3 = new JSONObject();
    JSONObject usr4 = new JSONObject();
    usr3.put("type", "string");
    usr3.put("value", "email" + i + "@gmail.com");
    usr4.put("email", usr3);

    usr4.put("_id", Integer.toString(i + num));
    usr4.put("_type", "vertex");
    vertex.put(usr4);
}
json.put("vertex", vertex);

The num can be approx 60,000-200,000.

But over here i am creating java objects again and again, which is quite expensive. I want the object creation to be minimized as well as don't want to change the code structure too much. How can i achieve this without changing the code structure too much?

I'm using JSON* classes from org.codehaus.jettison.json package.And i have to write it to a file the json object

Upvotes: 2

Views: 737

Answers (2)

Angad Tiwari
Angad Tiwari

Reputation: 1768

your json seems to be like

{
"vertex": [
    {
        "name": {
            "type": "string",
            "value": "name0"
        },
        "_id": 0,
        "_type": "vertex"
    },
    {
        "email": {
            "type": "string",
            "value": "[email protected]"
        },
        "_id": 0,
        "_type": "vertex"
    },
    .....
]
}

at each successive loop... 2 jsonobject is been set on vertext json array... you can reduce the size by half...by creating 1 jsonobject at each successive loop

something like this

{
"vertex": [
    {
        "_id": 0,
        "_type": "vertex",
        "name": "name0",
        "email": "[email protected]"
    },
    ....
]
}

you wouldn't do too much on json creation ...

although i also wouldn't find the sense to include "_id" and "_type" key ... as "_id" contains indexing from 0 to num and u can get that index using vertex array...

hence you can use the structure of json as

{
"vertex": [
  {
    "name": "name0",
    "email": "[email protected]"
  },
  ....
]
}

and your java code may reduce to this...

JSONObject json = new JSONObject();
JSONArray vertex = new JSONArray();
for (int i = 0; i < num; i++) {
   vertex.put(new JSONObject().put("name", "name" + i).put("email", "email" + i + "@gmail.com"));
}
json.put("vertex", vertex);

Upvotes: 1

Tagir Valeev
Tagir Valeev

Reputation: 100279

For fast JSON generation without creating the intermediate objects a JSONWriter could be used:

StringWriter sw = new StringWriter();
JSONWriter writer = new JSONWriter(sw);
writer.object().key("vertex").array();
for (int i = 0; i < num; i++) {
    writer
        .object() // usr2
            .key("name")
                .object() // usr1
                    .key("type").value("string")
                    .key("value").value("name"+i)
                .endObject()
            .key("_id").value(Integer.toString(i))
            .key("_type").value("vertex")
        .endObject()
        .object() // usr4
            .key("email")
                .object() // usr3
                    .key("type").value("string")
                    .key("value").value("email"+i+"@gmail.com")
                .endObject()
            .key("_id").value(Integer.toString(i+num))
            .key("_type").value("vertex")
        .endObject();
}
writer.endArray().endObject();
String json = sw.toString();

Here the StringWriter is used to store the intermediate JSON text. Alternatively you may write directly to file or network socket without keeping it in the memory at all.

Upvotes: 2

Related Questions