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 .

3
  • "with similar values" How similar? Please provide examples of values you expect to be deduplicated, but aren't. Commented Jan 9, 2017 at 10:07
  • @AndyTurner "with similar values" - > A list containing two objects whose fields has same values . Example : There are two objects obj1 and obj2 with fileds (id , code) and the value of id and code in both objects is same . Commented Jan 9, 2017 at 10:13
  • @AndroidDev check below answer!! Commented Jan 9, 2017 at 10:19

3 Answers 3

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

6 Comments

Please explain why OP would want to use this approach instead of his/hers.
@AndyTurner not getting you what you want to asking actually ?
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.
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.
|
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

1 Comment

He is already using a HashSet variant with the purpose of removing duplicates.
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);
        }
    }
});

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.