问题
I've made my own custom ArrayList like this:
public class Points {
String hoodName;
Double points;
Integer hoodId;
public Points(String hN, Double p, Integer hI){
hoodName = hN;
points =p;
hoodId = hI;
}
public Double getPoints() {
return points;
}
public Integer getHoodId() {
return hoodId;
}
public String getHoodName() {
return hoodName;
}
}
When I'm adding data from my JSON api it adds item multiple times. I've tried this code to add the items only once it:
if (!points.contains(jsonObject.getString("hood_name"))) {
points.add(new Points(jsonObject.getString("hood_name"), jsonObject.getDouble("points"), jsonObject.getInt("hood_id")));
}
If also tried this:
if (!points.contains(Points(jsonObject.getString("hood_name"), jsonObject.getDouble("points"), jsonObject.getInt("hood_id")))) {
points.add(new Points(jsonObject.getString("hood_name"), jsonObject.getDouble("points"), jsonObject.getInt("hood_id")));
}
This code is working when I use a ArrayList or ArrayList<Integer>
but not when I'm using ArrayList<Points>
Can anyone explain me how I can avoid duplication in my list?
回答1:
As you mentioned in the comments that Order doesn't matter , I would have an HashSet<String>
to store and check the hood_name
and If you want to get object
by entering hood_name
you can use HashMap<String,Point
instead which returns the object in O(1)
time.
So You need to create a HashSet<String>
which will keep track of hood_name
of all objects present in the ArrayList<Points>
.
HashSet<String> all_ids=new HashSet<String>();
if (!all_ids.contains(jsonObject.getString("hood_name")))
{
points.add(new Points(jsonObject.getString("hood_name"), jsonObject.getDouble("points"), jsonObject.getInt("hood_id")));
all_ids.add(jsonObject.getString("hood_name")); //You need to add it to set as Now it exists in the list.
}
Further more , If you want to only use ArrayList<Point>
to execute this task , You can override equals(Object E)
and hashCode()
methods in Point
class. For more information , refer this.
回答2:
If the sequence is not important, then you can use a HashMap
to uniquely identify them.
HashMap<String, Point> pointMap = new HashMap<>();
String hoodName = jsonObject.getString("hood_name");
Point point = new Point(jsonObject.getString("hood_name"),
jsonObject.getDouble("points"),
jsonObject.getInt("hood_id"))
if (!points.containsKey(hoodName)) pointMap.put(hoodName, point);
In fact, if you will always want to overwrite the old point with new point which has the same hoodName
, you do not need to check whether the Point
exists in your list. Calling pointMap.put(key, object)
will always replace the object with the same key.
回答3:
In this case the Points object use default equals()
method which is inconsistent. As you want to use contains
method, you should implement equals()
method like this way.
public class Points {
String hoodName;
Double points;
Integer hoodId;
public Points(String hN, Double p, Integer hI) {
hoodName = hN;
points = p;
hoodId = hI;
}
public Double getPoints() {
return points;
}
public Integer getHoodId() {
return hoodId;
}
public String getHoodName() {
return hoodName;
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Points)) {
return false;
}
Points points = (Points) obj;
return points.getHoodName().equals(getHoodName().trim()) &&
points.getHoodId() == getHoodId()
&& points.getPoints() == getPoints();
}
}
If you ovverride
equals()
method, you can easily use it in ArrayList<Points>
. Hope it will work in your case.
来源:https://stackoverflow.com/questions/43436292/add-item-only-once-in-custom-arraylist