如何标记数组中的重复项?

发布于 2024-10-24 11:15:56 字数 1591 浏览 1 评论 0原文

我想知道一种在数组中标记值、删除重复项以及在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

私藏温柔 2024-10-31 11:15:56

您可以这样做:

//assuming that the array you are filtering is called 'myArray'
List<Object> seenObjects = new ArrayList<Object>();
for (Object obj : myArray) {
    if (! seenObjects.contains(obj)) {
        seenObjects.add(obj);
    }
    else {
        //merge the 'desc' field in 'obj' with the 'desc' field in
        //'seenObjects.get(seenObjects.indexOf(obj))'
    }
}

请注意,只有当您要比较的对象具有 equals()hashCode() 的实现来执行您想要的操作(在您的在这种情况下,他们应该只考虑“纬度”和“经度”字段)。

更新:

这是一些完整的示例代码:

import java.util.ArrayList;
import java.util.List;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

public class JsonMergeTest {
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static void main(String[] args) {
        List<Object> myArray = new ArrayList<Object>();
        myArray.add(MyJsonObject.parse("{\"lon\": 0.001, \"lat\": 0.001, \"desc\": \"test\"}"));
        myArray.add(MyJsonObject.parse("{\"lon\": 0.001, \"lat\": 0.001, \"desc\": \"test2\"}"));

        List seenObjects = new ArrayList<Object>();
        for (Object obj : myArray) {
            if (! seenObjects.contains(obj)) {
                seenObjects.add(obj);
            }
            else {
                //merge the 'desc' field in 'obj' with the 'desc' field in the list
                MyJsonObject original = (MyJsonObject)seenObjects.get(seenObjects.indexOf(obj));
                MyJsonObject update = (MyJsonObject)obj;
                original.put("desc", original.get("desc") + ", " + update.get("desc"));
            }
        }

        for (MyJsonObject obj : (List<MyJsonObject>)seenObjects) {
            System.out.println(obj.toJSONString());
        }
    }

    private static class MyJsonObject extends JSONObject  {
        @Override
        public boolean equals(Object obj) {
            if (obj == null || ! (obj instanceof MyJsonObject) || ! this.containsKey("lat") || ! this.containsKey("lon")) {
                return super.equals(obj);
            }
            MyJsonObject jsonObj = (MyJsonObject)obj;
            return this.get("lat").equals(jsonObj.get("lat")) && this.get("lon").equals(jsonObj.get("lon"));
        }

        @Override
        public int hashCode() {
            if (! this.containsKey("lat") || ! this.containsKey("lon")) {
                return super.hashCode();
            }
            return this.get("lat").hashCode() ^ this.get("lon").hashCode();
        }

        @SuppressWarnings("unchecked")
        public static Object parse(String json) {
            Object parsedJson = JSONValue.parse(json);
            if (! (parsedJson instanceof JSONObject)) {
                return parsedJson;
            }

            MyJsonObject result = new MyJsonObject();
            result.putAll((JSONObject)parsedJson);
            return result;
        }
    }
}

You could do something like:

//assuming that the array you are filtering is called 'myArray'
List<Object> seenObjects = new ArrayList<Object>();
for (Object obj : myArray) {
    if (! seenObjects.contains(obj)) {
        seenObjects.add(obj);
    }
    else {
        //merge the 'desc' field in 'obj' with the 'desc' field in
        //'seenObjects.get(seenObjects.indexOf(obj))'
    }
}

Note that this will only work if the objects you are comparing have implementations of equals() and hashCode() 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:

import java.util.ArrayList;
import java.util.List;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

public class JsonMergeTest {
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static void main(String[] args) {
        List<Object> myArray = new ArrayList<Object>();
        myArray.add(MyJsonObject.parse("{\"lon\": 0.001, \"lat\": 0.001, \"desc\": \"test\"}"));
        myArray.add(MyJsonObject.parse("{\"lon\": 0.001, \"lat\": 0.001, \"desc\": \"test2\"}"));

        List seenObjects = new ArrayList<Object>();
        for (Object obj : myArray) {
            if (! seenObjects.contains(obj)) {
                seenObjects.add(obj);
            }
            else {
                //merge the 'desc' field in 'obj' with the 'desc' field in the list
                MyJsonObject original = (MyJsonObject)seenObjects.get(seenObjects.indexOf(obj));
                MyJsonObject update = (MyJsonObject)obj;
                original.put("desc", original.get("desc") + ", " + update.get("desc"));
            }
        }

        for (MyJsonObject obj : (List<MyJsonObject>)seenObjects) {
            System.out.println(obj.toJSONString());
        }
    }

    private static class MyJsonObject extends JSONObject  {
        @Override
        public boolean equals(Object obj) {
            if (obj == null || ! (obj instanceof MyJsonObject) || ! this.containsKey("lat") || ! this.containsKey("lon")) {
                return super.equals(obj);
            }
            MyJsonObject jsonObj = (MyJsonObject)obj;
            return this.get("lat").equals(jsonObj.get("lat")) && this.get("lon").equals(jsonObj.get("lon"));
        }

        @Override
        public int hashCode() {
            if (! this.containsKey("lat") || ! this.containsKey("lon")) {
                return super.hashCode();
            }
            return this.get("lat").hashCode() ^ this.get("lon").hashCode();
        }

        @SuppressWarnings("unchecked")
        public static Object parse(String json) {
            Object parsedJson = JSONValue.parse(json);
            if (! (parsedJson instanceof JSONObject)) {
                return parsedJson;
            }

            MyJsonObject result = new MyJsonObject();
            result.putAll((JSONObject)parsedJson);
            return result;
        }
    }
}
(り薆情海 2024-10-31 11:15:56

您可以使用 GSon。并按照以下步骤操作:

1. 在 Java 中定义一个等效的 POJO,以映射 JSON 字符串

public class Location implements Comparable<Location> {
    public String lon;
    public String lat;
    public String desc;

    @Override
    public String toString() {
        return "<lon: " + lon +", lat: "+ lat +", desc: " + desc +">";
    }

    @Override
    public boolean equals(Object obj) {
        return ((Location)obj).lon.equals(lon) && ((Location)obj).lat.equals(lat); 
    }

    public int compareTo(Location obj) {
        return ((Location)obj).lon.compareTo(lon) + ((Location)obj).lat.compareTo(lat);
    }


}

2. 编写合并相似位置的代码。好的,今天是周日,让我们开始吧:)

public static void main(String[] args){
      //Some test data
    String s = "[" +
            " {\"lon\": 0.001, \"lat\": 0.001, \"desc\": \"test\"}," +
            " {\"lon\": 0.002, \"lat\": 0.001, \"desc\": \"test3\"}," +
            " {\"lon\": 0.002, \"lat\": 0.005, \"desc\": \"test4\"}," +
            " {\"lon\": 0.002, \"lat\": 0.001, \"desc\": \"test5\"}," +
            " {\"lon\": 0.001, \"lat\": 0.001, \"desc\": \"test2\"}]";
    Gson gson = new Gson();
    Location[] al = gson.fromJson(s, Location[].class);
    List<Location> tl = Arrays.asList(al);

     //lets sort so that similar locations are grouped
    Collections.sort(tl);
    List<Location> fl = new ArrayList<Location>();
    Location current = null;

     //merge!
    for(Iterator<Location> it = tl.iterator(); it.hasNext();){
        current = current==null?it.next():current;
        Location ltmp = null;
        while(it.hasNext() && (ltmp = it.next()).equals(current))
            current.desc = current.desc + "," + ltmp.desc;
        fl.add(current);
        current = ltmp;
    }

       //convert back to JSON?
    System.out.println(gson.toJson(fl));

}

3. 输出

[{"lon":"0.002","lat":"0.005","desc":"test4"},
{"lon":"0.002","lat":"0.001","desc":"test3,test5"},
{"lon":"0.001","lat":"0.001","desc":"test,test2"}]

You can use GSon. And follow the steps:

1. Define an equivalent POJO in Java, to map the JSON String

public class Location implements Comparable<Location> {
    public String lon;
    public String lat;
    public String desc;

    @Override
    public String toString() {
        return "<lon: " + lon +", lat: "+ lat +", desc: " + desc +">";
    }

    @Override
    public boolean equals(Object obj) {
        return ((Location)obj).lon.equals(lon) && ((Location)obj).lat.equals(lat); 
    }

    public int compareTo(Location obj) {
        return ((Location)obj).lon.compareTo(lon) + ((Location)obj).lat.compareTo(lat);
    }


}

2. Write the code that merges similar location. OK, it's Sunday, lets do it :)

public static void main(String[] args){
      //Some test data
    String s = "[" +
            " {\"lon\": 0.001, \"lat\": 0.001, \"desc\": \"test\"}," +
            " {\"lon\": 0.002, \"lat\": 0.001, \"desc\": \"test3\"}," +
            " {\"lon\": 0.002, \"lat\": 0.005, \"desc\": \"test4\"}," +
            " {\"lon\": 0.002, \"lat\": 0.001, \"desc\": \"test5\"}," +
            " {\"lon\": 0.001, \"lat\": 0.001, \"desc\": \"test2\"}]";
    Gson gson = new Gson();
    Location[] al = gson.fromJson(s, Location[].class);
    List<Location> tl = Arrays.asList(al);

     //lets sort so that similar locations are grouped
    Collections.sort(tl);
    List<Location> fl = new ArrayList<Location>();
    Location current = null;

     //merge!
    for(Iterator<Location> it = tl.iterator(); it.hasNext();){
        current = current==null?it.next():current;
        Location ltmp = null;
        while(it.hasNext() && (ltmp = it.next()).equals(current))
            current.desc = current.desc + "," + ltmp.desc;
        fl.add(current);
        current = ltmp;
    }

       //convert back to JSON?
    System.out.println(gson.toJson(fl));

}

3. output

[{"lon":"0.002","lat":"0.005","desc":"test4"},
{"lon":"0.002","lat":"0.001","desc":"test3,test5"},
{"lon":"0.001","lat":"0.001","desc":"test,test2"}]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文