使用HashSet获取重复元素

时间:2015-06-05 11:32:33

标签: java android arraylist hashset

我在ListView中使用ArrayList获取重复的条目以填充和HashSet以删除重复的元素,但仍然得到重复的值,如下所示:

Delhi
Mumbai
Delhi
Mumbai

这是我用来将JSON数据解析为List并删除重复条目的代码

MainActivity.java:

public class MainActivity extends Activity {

    ArrayList<Destination> destinationArrayList;    
    MainAdapter adapter;
    Destination destination;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        destinationArrayList = new ArrayList<Destination>();

        new JSONAsyncTask().execute("my api link");

        Set<Destination> hashset = new HashSet<>(); 
        hashset.addAll(destinationArrayList); 
        destinationArrayList.clear(); 
        destinationArrayList.addAll(hashset);

        ListView listview = (ListView)findViewById(R.id.list);
        adapter = new MainAdapter(getApplicationContext(), R.layout.row, destinationArrayList);

        listview.setAdapter(adapter);

        listview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long id) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), destinationArrayList.get(position).getTitle().toString(), Toast.LENGTH_LONG).show();                
            }
        });

    }


    class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {

        ProgressDialog dialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog = new ProgressDialog(MainActivity.this);
            dialog.setMessage("Loading, please wait");
            dialog.show();
            dialog.setCancelable(false);
        }

        @Override
        protected Boolean doInBackground(String... urls) {
            try {

                //------------------>>
                HttpGet httppost = new HttpGet(urls[0]);
                HttpClient httpclient = new DefaultHttpClient();
                HttpResponse response = httpclient.execute(httppost);

                // StatusLine stat = response.getStatusLine();
                int status = response.getStatusLine().getStatusCode();

                if (status == 200) {
                    HttpEntity entity = response.getEntity();
                    String data = EntityUtils.toString(entity);


                    JSONObject jsono = new JSONObject(data);
                    JSONArray jarray = jsono.getJSONArray("data");

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

                        destination = new Destination();

                        destination.setCity(object.getString("city"));

                        destinationArrayList.add(destination);

                    }
                    return true;
                }

                //------------------>>

            } catch (ParseException e1) {
                e1.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return false;
        }

        protected void onPostExecute(Boolean result) {
            dialog.cancel();
            adapter.notifyDataSetChanged();
            if(result == false)
                Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();

        }
    }

}

当我使用它时,甚至not getting single record进入ListView:

destinationArrayList = new ArrayList<Destination>();

        new JSONAsyncTask().execute("my api url");

        System.out.println("size of Arraylist with duplicates: " + destinationArrayList.size());

        System.out.println(destinationArrayList);

        HashSet<Destination> listToSet = new HashSet<Destination>(destinationArrayList);

        ArrayList<Destination> listWithoutDuplicates = new ArrayList<Destination>(listToSet);
        System.out.println("size of ArrayList without duplicates: " + listToSet.size());

        System.out.println(listWithoutDuplicates);

Destination.java

public class Destination {

    private String city;
    ..............

    public Destination() {
        // TODO Auto-generated constructor stub
    }

    public Destination(String city) {
        super();
        this.city = city;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

}

4 个答案:

答案 0 :(得分:2)

您正在将Custom objects放入HashSet,因此不会消除重复项。您需要覆盖Destination对象的equalshashCode。然后它工作正常。

class Destination {

    private String city;

    public Destination() {
        // TODO Auto-generated constructor stub
    }

    public Destination(String city) {
        super();
        this.city = city;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Destination that = (Destination) o;

        if (city != null ? !city.equals(that.city) : that.city != null) return false;

        return true;
    }

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

将此代码放在AsynTask中的postExecute()方法中,然后显示列表中的所有元素。

 Set<Destination> hashset = new HashSet<>(); 
        hashset.addAll(destinationArrayList); 
        destinationArrayList.clear(); 
        destinationArrayList.addAll(hashset);

        ListView listview = (ListView)findViewById(R.id.list);
        adapter = new MainAdapter(getApplicationContext(), R.layout.row, destinationArrayList);

        listview.setAdapter(adapter);

答案 1 :(得分:0)

使用Set的重复数据删除无效,因为根据您当前的Destination类定义,不考虑具有相同Destination值的city个实例相等。

Destination课程中尝试overriding .equals ....

答案 2 :(得分:0)

我敢打赌,哈希集无法识别两对Destination对象,只要它们只有一个属性City

要比较两个对象,您需要覆盖超级方法equals,并且您的对象也被用于哈希表,您还需要覆盖hashCode

有一些经过验证的方法可以避免在hashCode中使用的哈希冲突。其中一个是Best implementation for hashCode method

答案 3 :(得分:0)

您的Destination类应该有equals()和hashCode()方法来消除对城市的重复记录。包含这些方法的以下代码可以解决您的问题:

public class Destination {

    private String city;
    ..............

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((city == null) ? 0 : city.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Destination other = (Destination) obj;
        if (city == null) {
            if (other.city != null)
                return false;
        } else if (!city.equals(other.city))
            return false;
        return true;
    }
}