-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"));
                        }
                    }
modabeckham
  • 227
  • 5
  • 19

5 Answers5

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"));
Jens
  • 67,715
  • 15
  • 98
  • 113
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);
Prashant Bhoir
  • 900
  • 6
  • 8
3

You can use Set, for Example HashSet.

Seraphis
  • 1,026
  • 2
  • 14
  • 30
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();
            }
        }
niks
  • 204
  • 1
  • 4
0

check if pickupOutlet contains the dulicate value as

if(!pickupOutlet.contains(object.getString("OutletName"))){
pickupOutlet.add(object.getString("OutletName"));
}
Jens
  • 67,715
  • 15
  • 98
  • 113
Santhosh
  • 1,867
  • 2
  • 16
  • 23