您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

在自定义ArrayList中仅添加一次项目

在自定义ArrayList中仅添加一次项目

正如您在评论中提到的,Order无关紧要,我将有一个HashSet<String>to存储并检查hood_name和。如果要object通过输入来获取,则hood_name可以使用HashMap<String,Point,它可以O(1)及时返回对象。

因此,您需要创建一个HashSet<String>,以跟踪中存在hood_name的所有对象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.                     
}

此外,如果您只想用来ArrayList<Point>执行此任务,则可以在类中重写equals(ObjectE)hashCode()方法Point

其他 2022/1/1 18:31:35 有401人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶