3

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.

2
  • please add a tag for the language you are using Commented Feb 19, 2019 at 2:17
  • You only want to store unique values? Commented Feb 19, 2019 at 2:40

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

I would recommend to use a Set over ArrayList here. You can use ArrayList and check the list whether it contains the element and add it. ArrayList.contains() takes O(n) time because it maintains a dynamic array inside. Where as a HashSet or TreeSet can do that check in O(1) and you also don't have to do that compare yourself.

Set<String> setSession = new HashSet<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"));
                setSession.add(sessionName);
            }
        }

Comments

1

If you're okay using Java 8, then you can use shorthand implementation like this:

Example:

ArrayList<String> data = new ArrayList<String>(Arrays.asList("A", "A", "A", "B", "B", "B"));

// This will be required if your target SDK < Android N
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    List<String> uniqueData = data.stream().distinct().collect(Collectors.toList()); // Results ["A", "B"]
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.