inayzi :
I have a set of JSON array :
listSession: [h0y78u93, h0y78u93, h0y78u93, h0y78u93, h0y78u93, 9i88u93, 9i88u93, 9i88u93, 9i88u93, 9i88u93]
I've created the array using the below code:
ArrayList<String> listSession = new ArrayList<String>();
for(int u=1; u < k+1; u++) {
String str = Integer.toString(u);
JSONArray arrTime=(JSONArray)mergedJSON2.get(str);
JSONObject objSession;
StringsessionName;
for (Object ro : arrTime) {
objSession = (JSONObject) ro;
sessionName = String.valueOf(objSession.get("sessionID"));
listSession.add(sessionName);
}
}
May I get your advice or opinion on how am I going to compare the value from each of the attributes in the list. If it is the same, I should it as ONE. Meaning from the above sample, the count should be only TWO instead of TEN.
Thank You.
Jojo Narte :
You can utilize Arraylist.contains()
method like below:
ArrayList<String> listSession = new ArrayList<String>();
for(int u=1; u < k+1; u++) {
String str = Integer.toString(u);
JSONArray arrTime=(JSONArray)mergedJSON2.get(str);
JSONObject objSession;
StringsessionName;
for (Object ro : arrTime) {
objSession = (JSONObject) ro;
sessionName = String.valueOf(objSession.get("sessionID"));
if (!listSession.contains(sessionName)) {
listSession.add(sessionName);
}
}
}
OR
You can use a Set implementation which doesn't allow duplicate values instead of ArrayList
. There's no need to compare explicitly.
// initialize
Set sessionsSet = new HashSet();
//add like below
sessionsSet.add(sessionName);
sessionsSet.size() // getting the length which should be what you expect to be 2