gpt4 book ai didi

java - 使用 HashSet 获取重复元素

转载 作者:行者123 更新时间:2023-12-02 04:36:39 25 4
gpt4 key购买 nike

我使用 ArrayList 填充并使用 HashSet 删除重复元素,将重复条目添加到 ListView 中,但仍然获取如下重复值:

Delhi
Mumbai
Delhi
Mumbai

这是我的代码,我用它来将 JSON 数据解析到列表中并删除重复的条目

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();

}
}

}

当我使用它时,甚至没有将单个记录放入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;
}

}

最佳答案

您将自定义对象放入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);

关于java - 使用 HashSet 获取重复元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30665464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com