0

I am working with ArrayList ,Here I want to remove objects with similar values from Arraylist. I tried many solutions posted over stackoverflow but something is wrong in my code due to which code is not working . I am getting list with dupliactes .

Here is my code :

public class StateCityModel {

    private String id ;
    private String code ;
    private String name ;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object obj) {

        if (!(obj instanceof StateCityModel))
            return false;

        return id.equals(((StateCityModel) obj).getId());

    }

    @Override
    public int hashCode() {
        return (id == null) ? 0 : id.hashCode();
    }
}

Code add values in the ArrayList

    businessTypeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView , View view , int position , long l) {
 businessTypeObj = clubsList.get(position);
                    selectedBusinessTypeList.add(businessTypeObj);
                }

            }
        });

Code to remove objects with similar values .

Set<StateCityModel> unique = new LinkedHashSet<StateCityModel>(selectedBusinessTypeList);
            selectedBusinessTypeList = new ArrayList<StateCityModel>(unique);

After doing above code I am getting objects with similar values in the selectedBusinessTypeList .

Help me please , I am not able to find what is wrong in the above code .

Shishupal Shakya
  • 1,632
  • 2
  • 18
  • 41

3 Answers3

2

You can also do in this way

Step 1: Only insert those data in arraylist which id's are not same so after that you don't need to remove duplicate elements from arraylist.

businessTypeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView , View view , int position , long l) {
     businessTypeObj = clubsList.get(position);
                        selectedBusinessTypeList.add(businessTypeObj);
                    }

                }
            });

Replace it by

businessTypeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView , View view , int position , long l) {
                }

    StateCityModel stateCityModel = new StateCityModel();
    stateCityModel = clubsList.get(position);;

    if (!selectedBusinessTypeList.contains(stateCityModel)){
         selectedBusinessTypeList.add(stateCityModel);
    }

}

});

Step2 : Remove this not needed

Set<StateCityModel> unique = new LinkedHashSet<StateCityModel>(selectedBusinessTypeList);
            selectedBusinessTypeList = new ArrayList<StateCityModel>(unique);
Vishal Patoliya ツ
  • 3,170
  • 4
  • 24
  • 45
  • 1
    Please explain why OP would want to use this approach instead of his/hers. – Andy Turner Jan 09 '17 at 10:14
  • @AndyTurner not getting you what you want to asking actually ? – Vishal Patoliya ツ Jan 09 '17 at 10:16
  • 2
    Well, you're saying "don't write that, write this", without actually saying *why* you shouldn't write that, but should write this instead. You're "giving a man a fish" here, not "teaching a man to fish". Explain the problem, explain why your solution fixes it. – Andy Turner Jan 09 '17 at 10:18
  • no, it's for everybody reading this page. *I* understand what you've done, but I'm encouraging you to improve your answer so that it is useful to other current and future readers. – Andy Turner Jan 09 '17 at 10:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/132687/discussion-between-vishal-patoliya--and-andy-turner). – Vishal Patoliya ツ Jan 09 '17 at 10:26
  • @VishalPatoliyaツ Great , Nice Idea . – Shishupal Shakya Jan 10 '17 at 09:40
0

I guess you are seeing duplicate values in the list right ? Since reference of selectedBusinessTypeList is changed. Change adapter also by creating new adapter by passing new selectedBusinessTypeList and set that adapter to listview again inside that onClick

Sangeet Suresh
  • 2,527
  • 1
  • 18
  • 19
0

You can replace the code to add values by below lines:

businessTypeListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
        businessTypeObj = clubsList.get(position);

        if (!selectedBusinessTypeList.contains(businessTypeObj)) {
            selectedBusinessTypeList.add(businessTypeObj);
        }
    }
});
cbr
  • 12,563
  • 3
  • 38
  • 63
Android Geek
  • 8,956
  • 2
  • 21
  • 35