[英]Complexity of using pagination/sorting through DB or using stream Java API?
[英]sorting a json array using java 8 stream api
我一直在尝试使用Java 8的Stream api对JSONArray进行排序。 但是我想我不能正确完成类型转换。
我可以得到的是以下代码。,
String objects = "[\n" +
" {\n" +
" \"lat\": \"-16.408545\",\n" +
" \"lon\": \"-71.539105\",\n" +
" \"type\": \"0\",\n" +
" \"distance\": \"0.54\"\n" +
" },\n" +
" {\n" +
" \"lat\": \"-16.4244317845\",\n" +
" \"lon\": \"-71.52562186\",\n" +
" \"type\": \"1\",\n" +
" \"distance\": \"1.87\"\n" +
" },\n" +
" {\n" +
" \"lat\": \"-16.4244317845\",\n" +
" \"lon\": \"-71.52562186\",\n" +
" \"type\": \"1\",\n" +
" \"distance\": \"0.22\"\n" +
" }\n" +
" {\n" +
" \"lat\": \"-16.4244317845\",\n" +
" \"lon\": \"-71.52562186\",\n" +
" \"type\": \"1\",\n" +
" \"distance\": \"2.69\"\n" +
" }\n" +
"]";
JSONArray objectsArray = (JSONArray) new JSONParser().parse(objects);
objectsArray.stream().sorted(Comparator.comparing(a -> ((JSONObject) a).get("distance")));
我收到以下错误。
Error:(42, 62) java: incompatible types: inferred type does not conform to upper bound(s)
inferred: java.lang.Object
upper bound(s): java.lang.Comparable<? super java.lang.Object>
我也提到了这个问题。 但是我觉得可以使用流以更简单的方式完成此操作。 有任何想法吗? 我将以下依赖项用于JSONObject
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
可能您正在寻找这样的代码:
objectsArray.stream().sorted(
Comparator.comparing(a -> Double.valueOf((String)((JSONObject) a).get("distance")))
).forEach(o -> System.out.println(o));
我们使用的演员表如下:
((JSONObject) a).get("distance")) //to get the distance as an Object
接着
((String) ((JSONObject) a).get("distance"))) // this Object is actually a String, so cast to it
接着
Double.valueOf((String)((JSONObject) a).get("distance")) //this String, however, is a double, and we want to compare the double size, not alphabetically
因为您需要根据距离的双精度值进行比较。
但是,还有很多事情需要改进。 当我们在这里使用OOP时,至少将“ String”流转换为某些有意义的对象可能是有意义的。
编辑:
您可能应该这样做:
//define a class to conform with OOP stuff
public static class Coordinate {
private final Double lat;
private final Double lon;
private final Integer type;
private final Double distance;
public Coordinate(Double lat, Double lon, Integer type, Double distance){
this.lat = lat;
this.lon = lon;
this.type = type;
this.distance = distance;
}
public Double getDistance() {
return distance;
}
}
// define a conversion logic
private static Coordinate convert(JSONObject raw){
Double distance = Double.valueOf((String) raw.get("distance"));
Double lon = Double.valueOf((String) raw.get("lon"));
Double lat = Double.valueOf((String) raw.get("lat"));
Integer type = Integer.valueOf((String) raw.get("type"));
return new Coordinate(lat, lon, type, distance);
}
那么您的方法就像编写代码一样简单:
List<JSONObject> coordinates = (List<JSONObject>)new JSONParser().parse(objects);
List<Coordinate> sortedCoordinates = coordinates
.stream()
.map(raw -> convert(raw))
.sorted(Comparator.comparing(Coordinate::getDistance))
.collect(Collectors.toList());
我为您提供的另一个技巧是使用更好的库(例如https://github.com/FasterXML/jackson)进行对象映射。
尝试这个。
JSONArray objectsArray = (JSONArray) new JSONParser().parse(OBJECTS);
List<JSONObject> jo = (List<JSONObject>) objectsArray.parallelStream()
.sorted(Comparator.comparing((t) -> Double.parseDouble(((JSONObject) t).get("distance").toString())))
.collect(Collectors.toList());
System.out.println(JSONArray.toJSONString(jo));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.