Add item only once in custom ArrayList

后端 未结 3 560
春和景丽
春和景丽 2021-01-23 08:28

I\'ve made my own custom ArrayList like this:

public class Points {
    String hoodName;
    Double points;
    Integer hoodId;
    public Points(String hN, Doub         


        
相关标签:
3条回答
  • 2021-01-23 09:15

    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.

    0 讨论(0)
  • 2021-01-23 09:27

    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.

    0 讨论(0)
  • 2021-01-23 09:30

    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.

    0 讨论(0)
提交回复
热议问题