I have a loop thats constantly setting new values to a few string and looking into it, i thought that i could improve it. At first it was easy (this is how it actually looks) but i am looking for a way to avoid doing that lot of "ToString" and then resetting that string to "".
private void obtenerProfCita(JSONArray catalogoDatos) {
String areaConsumer = "";
String resourceConsumer = "";
String activityConsumerid = "";
for(int i = 0; i < catalogoDatos.length(); i++) {
areaConsumer = catalogoDatos.getJSONObject(i).get("area_consumerid").toString();
resourceConsumer = catalogoDatos.getJSONObject(i).get("resource_consumerid").toString();
activityConsumerid = catalogoDatos.getJSONObject(i).get("activity_consumerid").toString();
if(StringUtils.isNotBlank(areaConsumer) && StringUtils.isNotBlank(resourceConsumer) && StringUtils.isNotBlank(activityConsumerid)) {
log.error("REFERENCIA TUOTEMPO:\nModulo: " + i + "\nCONSUL: " + areaConsumer + "\nIDPROF: " + resourceConsumer);
TuotempoDAO.guardarActivityId(catalogoDatos.getJSONObject(i));//Guardar en BBDD el Activity_ID
}
areaConsumer = "";
resourceConsumer = "";
}
}
As you can see, is a basic for loop but taking in account that the JSONArray is quite big, i was looking for a way to use StringBuilders or StringBuffers and avoid the "ToStrings" in the JSON array.
Actual code with stringBuilders:
private void obtenerProfCita(JSONArray catalogoDatos) {
StringBuilder areaConsumer = new StringBuilder();
StringBuilder resourceConsumer = new StringBuilder();
StringBuilder activityConsumerid = new StringBuilder();
for(int i = 0; i < catalogoDatos.length(); i++) {
areaConsumer.append(catalogoDatos.getJSONObject(i).get("area_consumerid").toString());
resourceConsumer.append(catalogoDatos.getJSONObject(i).get("resource_consumerid").toString());
activityConsumerid.append(catalogoDatos.getJSONObject(i).get("activity_consumerid").toString());
if(StringUtils.isNotBlank(areaConsumer) && StringUtils.isNotBlank(resourceConsumer) && StringUtils.isNotBlank(activityConsumerid)) {
log.error("REFERENCIA TUOTEMPO:\nModulo: " + i + "\nCONSUL: " + areaConsumer + "\nIDPROF: " + resourceConsumer);
TuotempoDAO.guardarActivityId(catalogoDatos.getJSONObject(i));//Guardar en BBDD el Activity_ID
}
areaConsumer.setLength(0);
resourceConsumer.setLength(0);
}
}
Also i have doubts about how efficient is to use StringBuilder instead of String, if someone could explain it to me it would be great.
EDIT. Thanks to OH GOD SPIDERS below, i have improved a little my code. here it´s how it looks right now:
private void obtenerProfCita(JSONArray catalogoDatos) {
StringBuilder areaConsumer = new StringBuilder();
StringBuilder resourceConsumer = new StringBuilder();
StringBuilder activityConsumerid = new StringBuilder();
for(int i = 0; i < catalogoDatos.length(); i++) {
areaConsumer.append(catalogoDatos.getJSONObject(i).get("area_consumerid"));
resourceConsumer.append(catalogoDatos.getJSONObject(i).get("resource_consumerid"));
activityConsumerid.append(catalogoDatos.getJSONObject(i).get("activity_consumerid"));
if (StringUtils.isNotBlank(areaConsumer)
&& StringUtils.isNotBlank(resourceConsumer)
&& StringUtils.isNotBlank(activityConsumerid)) {
log.error("REFERENCIA TUOTEMPO:\nModulo: " + i + "\nCONSUL: " + areaConsumer + "\nIDPROF: " + resourceConsumer);
TuotempoDAO.guardarActivityId(catalogoDatos.getJSONObject(i));//Guardar en BBDD el Activity_ID
}
areaConsumer.setLength(0);
resourceConsumer.setLength(0);
activityConsumerid.setLength(0);
}
}
Any help is appreciatted
EDIT2:
Thank you for all the help, i will modify my code taken in account the last comment and answers.
Strings foractivityConsumeridnew every loop iteration overriding the old values while in the example with aStringBuilderyou keep appending toactivityConsumerid. The result of those 2 code blocks could therefor be very different.toString()on the result of (for example)catalogoDatos.getJSONObject(i).get("area_consumerid"), it also needs to copy the contents of that String into theStringBuilderby callingareaConsumer.append(). More method calls and more copying will lead to worse performance.