0

我们正在做一个项目,我们需要做的一件事是显示一个 mac 地址出现了多少个不同的位置。我们使用 RedisGraph 来查询信息,我使用 hashmap 来获取数据. 我的问题是我将在 json 响应中多次列出一个 mac 地址,其中 3 次具有相同的位置,而 3 次具有不同的位置。我想得到一个 JSON 响应,显示位置计数为 2 的 mac 地址,因此它只计算不同的位置。非常感谢任何帮助,以下是我的代码。

        ResultSet resultSet = api.query(GET_COUNT_OF_LOCATIONS_FOR_DEVICE);
        JSONArray finalResult = new JSONArray();

        JSONArray jsonArray = new JSONArray();

        try {
            while (resultSet.hasNext()) {

                JSONObject joined = new JSONObject();
                Map<String, String> mac = new HashMap<String, String>();
                Map<String, String> location = new HashMap<String, String>();
                Map<String, String> location_count = new HashMap<String, String>();

                Record record = resultSet.next();
                mac.put("mac_address", record.getString("m.address"));
                location.put("location", record.getString("l.lo"));
                location_count.put("location_count", record.getString("l.lo"));

                joined.put("mac_address", mac.values().stream().map(i -> i).collect(Collectors.toSet()));
                joined.put("location", location.values().stream().map(i -> i).collect(Collectors.toSet()));
                joined.put("location_count", location.values().stream().map(i -> i).collect(Collectors.toSet()).size());


                jsonArray.put(joined);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        finalResult.put(jsonArray);
        return finalResult;

    }
4

0 回答 0