-2

I have a List and it has duplicate value, I want to leave out the duplicate values from that and filter only the single values as,

Current output is {a, a, a, b, b, c, c, d, d, d, d} and I want to change it and get the output as {a, b, c, d}.

This is the code I'm using right now to get the current output,

List<String> pickupOutlet = new ArrayList<String>();
        @Override
        public void onTaskCompleted(JSONArray responseJson) {
            // TODO Auto-generated method stub
            try {

                for (int i = 0; i < responseJson.length(); ++i) {
                    JSONObject object = responseJson.getJSONObject(i);

                    Log.i("OutletName", object.getString("OutletName"));
                    pickupOutlet.add(object.getString("OutletName"));

                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

Can anyone help me how to achieve it.

UPDATED

Current code

This worked for me

                    for (int i = 0; i < responseJson.length(); ++i) {
                        JSONObject object = responseJson.getJSONObject(i);

                        if (!pickupOutletDuplicates.contains(object.getString("OutletName"))){
                        Log.i("OutletName", object.getString("OutletName"));
                        pickupOutletDuplicates.add(object
                                .getString("OutletName"));
                        }
                    }
1
  • you can use google to search this question or try using HashSet Commented Apr 15, 2015 at 5:58

5 Answers 5

5

Check if your List already contains the value:

JSONObject object = responseJson.getJSONObject(i);

Log.i("OutletName", object.getString("OutletName"));
if (!pickupOutlet.contains(object.getString("OutletName"))
   pickupOutlet.add(object.getString("OutletName"));
Sign up to request clarification or add additional context in comments.

3 Comments

can u check the updated question
@modabeckham What means that it doesn`t work. Can oyu provide a runable excample so that i can try it by my self?
I was able to get the output
4
  List<String> duplicateList = (List<String>) Arrays.asList("a" ,"a" ,"a" , "b", "b", "c","c","d","d","d","d");

  duplicateList.size());
  System.out.println(duplicateList);

//Converting ArrayList to HashSet to remove duplicates

HashSet<String> listToSet = new HashSet<String>(duplicateList);

//Creating Arraylist without duplicate values

 List<String> listWithoutDuplicates = new ArrayList<String>(listToSet);

System.out.println("size of ArrayList without duplicates: " + listToSet.size()); 

System.out.println(listWithoutDuplicates);

Comments

3

You can use Set, for Example HashSet.

1 Comment

use LinkedHashSet to perserve order
2

Simply use Set instead of List. Because Set contains only unique values.and list contains duplicate values.

        Set<String> pickupOutlet = new HashSet<String>();

        @Override
        public void onTaskCompleted(JSONArray responseJson) {
            // TODO Auto-generated method stub
            try {

                for (int i = 0; i < responseJson.length(); ++i) {
                    JSONObject object = responseJson.getJSONObject(i);

                    Log.i("OutletName", object.getString("OutletName"));
                    pickupOutlet.add(object.getString("OutletName"));

                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

Comments

0

check if pickupOutlet contains the dulicate value as

if(!pickupOutlet.contains(object.getString("OutletName"))){
pickupOutlet.add(object.getString("OutletName"));
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.