如何标记数组中的重复项?
我想知道一种在数组中标记值、删除重复项以及在 Java 中组合一些数据的方法。
我使用纬度、经度和描述来记录地理位置,这在 JSON 数组中编码如下:
[{"lon": 0.001, "lat": 0.001, "desc": test}, {"lon": 0.001, "lat": 0.001, "desc": test2}]
我希望能够删除重复的地理位置,同时保留数组的“desc”部分,例如
[{"lon": 0.001, "lat": 0.001, "desc": test, test2}]
编辑: 这就是我目前正在做的事情:
//Store locPoints from server in JSONArray
JSONArray jPointsArray = new JSONArray(serverData);
List<JSONObject> jObjects = new ArrayList<JSONObject>();
List<JSONObject> seenObjects = new ArrayList<JSONObject>();
for(int i = 0; i < jPointsArray.length(); ++i)
{
jObjects.add(jPointsArray.getJSONObject(i));
}
for (JSONObject obj : jObjects)
{
//This always returns true
if (!seenObjects.contains(obj))// && !seenObjects.contains(obj.get("lon")))
{
Log.i("Sucess", "Huzzah!");
seenObjects.add(obj);
}
else
{
//merge the 'desc' field in 'obj' with the 'desc' field in
JSONObject original = (JSONObject)seenObjects.get(seenObjects.indexOf(obj));
JSONObject update = obj;
original.put("desc", original.get("desc") + ", " + update.get("desc"));
seenObjects.get(seenObjects.indexOf(obj)).get("desc"));
}
}
I would like to know a method for flagging values in an array, removing the duplicates and combining some of the data in Java.
I am keeping a record of geo locations using lat, long and description this is encoded in a JSON array as follows:
[{"lon": 0.001, "lat": 0.001, "desc": test}, {"lon": 0.001, "lat": 0.001, "desc": test2}]
I would like to be able to remove the duplicate geo locations while keeping the "desc" part of the array, e.g.
[{"lon": 0.001, "lat": 0.001, "desc": test, test2}]
Edit:
This is what I am currently doing:
//Store locPoints from server in JSONArray
JSONArray jPointsArray = new JSONArray(serverData);
List<JSONObject> jObjects = new ArrayList<JSONObject>();
List<JSONObject> seenObjects = new ArrayList<JSONObject>();
for(int i = 0; i < jPointsArray.length(); ++i)
{
jObjects.add(jPointsArray.getJSONObject(i));
}
for (JSONObject obj : jObjects)
{
//This always returns true
if (!seenObjects.contains(obj))// && !seenObjects.contains(obj.get("lon")))
{
Log.i("Sucess", "Huzzah!");
seenObjects.add(obj);
}
else
{
//merge the 'desc' field in 'obj' with the 'desc' field in
JSONObject original = (JSONObject)seenObjects.get(seenObjects.indexOf(obj));
JSONObject update = obj;
original.put("desc", original.get("desc") + ", " + update.get("desc"));
seenObjects.get(seenObjects.indexOf(obj)).get("desc"));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以这样做:
请注意,只有当您要比较的对象具有
equals()
和hashCode()
的实现来执行您想要的操作(在您的在这种情况下,他们应该只考虑“纬度”和“经度”字段)。更新:
这是一些完整的示例代码:
You could do something like:
Note that this will only work if the objects you are comparing have implementations of
equals()
andhashCode()
that do what you want (in your case, they should only take into consideration the 'lat' and 'lon' fields).Update:
Here is some complete example code:
您可以使用 GSon。并按照以下步骤操作:
1. 在 Java 中定义一个等效的 POJO,以映射 JSON 字符串
2. 编写合并相似位置的代码。好的,今天是周日,让我们开始吧:)
3. 输出
You can use GSon. And follow the steps:
1. Define an equivalent POJO in Java, to map the JSON String
2. Write the code that merges similar location. OK, it's Sunday, lets do it :)
3. output