1

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?

Sander bakker
  • 518
  • 1
  • 6
  • 20
  • 2
    hoodName is just one property in your list, you can't check contain with one property of your object, create `Points` object, override equals method in `Points` then your code will be worked – Shayan Pourvatan Apr 16 '17 at 10:40
  • Also, looking at your code, seems like the sequence of Point is not important? In that case, using a Map or HashMap may be better. – tingyik90 Apr 16 '17 at 10:42
  • @tingyik90 No the sequence is not important – Sander bakker Apr 16 '17 at 10:43
  • Possible duplicate of [Finding out if a list of Objects contains something with a specified field value?](http://stackoverflow.com/questions/7481346/finding-out-if-a-list-of-objects-contains-something-with-a-specified-field-value) – Narendra Singh Apr 16 '17 at 11:11
  • Do something similar to [this](http://stackoverflow.com/a/7481383/1479511) – Narendra Singh Apr 16 '17 at 11:12

3 Answers3

3

As you mentioned in the comments that Order doesn't matter , I would have an HashSet<String> to store and check the hood_nameand 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.

Community
  • 1
  • 1
Sanket Makani
  • 2,491
  • 2
  • 15
  • 23
1

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.

tingyik90
  • 1,641
  • 14
  • 23
1

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.

androidcodehunter
  • 21,567
  • 19
  • 47
  • 70