We're working on a project and one of the things we need done is to show how many different locations a mac address has shown up in. We're using RedisGraph to query the information and I've used a hashmap to get the data. My problem is that I'll have a mac address listed in the json response multiple times, 3 of the times with the same location and one of the times with a different location. I want to get a JSON response that shows that mac address with a location count of 2, so it only counts distinct locations. Any help is much appreciated and below is my code.
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;
}