英文:
How to reduce the list of doubles to string?
问题
我对Java 8和Kotlin中的reduce和fold等函数式方法还不熟悉。我想要将以下内容进行reduce操作 -
List<List<List<Double>>> boundingPolygon = [[[-125.48845080566404,47.94508483691371],[-124.96110705566404,42.309040799653665],[-117.13884143066404,45.04173793121063],[-118.36931018066404,48.93624688577435],[-125.48845080566404,47.94508483691371]]];
将其简化为一个表示多个坐标串联的字符串 -
"-118.359053 33.931562,-118.372443 33.946939,-118.369053 33.951562,-118.337612 33.944342,-118.342012 33.944042,-118.359053 33.931562"
尝试了以下方法 -
val polygonCoordinates = boundingPolygon.first().reduce { acc, element ->
acc + "${element[0]} ${element[1]}"
}
但是这不起作用。
英文:
I'm new to functional methods like reduce and fold in java 8 and Kotlin. I want to reduce -
List<List<List<Double>>> boundingPolygon = [[[-125.48845080566404,47.94508483691371],[-124.96110705566404,42.309040799653665],[-117.13884143066404,45.04173793121063],[-118.36931018066404,48.93624688577435],[-125.48845080566404,47.94508483691371]]];
to single string that represents one String concatenated by coordinates -
"-118.359053 33.931562,-118.372443 33.946939,-118.369053 33.951562,-118.337612 33.944342,-118.342012 33.944042,-118.359053 33.931562"
Trying to do -
val polygonCoordinates = boundingPolygon.first().reduce { acc, element ->
acc + "${element[0]} ${element[1]}"
}
This is not working.
答案1
得分: 3
在您的reduce操作中,acc
的类型为List<Double>
,而不是String
。查看reduce函数的签名,您就应该明白原因。这是我提出的实现您想要的方式:
coords.first().joinToString(", ") { (x, y) -> "$x $y" }
我使用了解构来从坐标列表中提取第一个和第二个值。因此,这仅适用于二维坐标。
英文:
The acc
in your reduce operation is of type List<Double>
, not String
. Look at the reduce function signature and you should understand why. Here's what I propose to do what you want:
coords.first().joinToString(", ") { (x, y) -> "$x $y" }
I used destructuring to extract the first and second values from the coordinate lists. So this will only work with 2D coordinates.
答案2
得分: 1
代替将其缩小,你应该将它们添加到 StringBuilder
中,当您进行多个操作(例如连接大量字符串)时,它会更有效率:
val boundingPolygon = listOf(
listOf(
listOf(-125.48845080566404, 47.94508483691371),
listOf(-124.96110705566404, 42.309040799653665),
listOf(-117.13884143066404, 45.04173793121063),
listOf(-118.36931018066404, 48.93624688577435),
listOf(-125.48845080566404, 47.94508483691371)
)
)
val sb = StringBuilder()
for (nestedList in boundingPolygon) {
for (innerNestedList in nestedList) {
sb.append(innerNestedList.joinToString(" "))
sb.append(',')
}
}
if (sb.isNotEmpty()) sb.deleteCharAt(sb.lastIndex)
println(sb)
// 输出:-125.48845080566404 47.94508483691371,-124.96110705566404 42.309040799653665,-117.13884143066404 45.04173793121063,-118.36931018066404 48.93624688577435,-125.48845080566404 47.94508483691371
// val stringRepresentation = sb.toString() // 用作字符串数据类型的进一步用途
英文:
Instead of reducing it, you should just add those into a StringBuilder
, which is efficient when you are doing multiple operations (like concatenating a ton of Strings):
val boundingPolygon = listOf(
listOf(
listOf(-125.48845080566404, 47.94508483691371),
listOf(-124.96110705566404, 42.309040799653665),
listOf(-117.13884143066404, 45.04173793121063),
listOf(-118.36931018066404, 48.93624688577435),
listOf(-125.48845080566404, 47.94508483691371)
)
)
val sb = StringBuilder()
for (nestedList in boundingPolygon) {
for (innerNestedList in nestedList) {
sb.append(innerNestedList.joinToString(" "))
sb.append(',')
}
}
if (sb.isNotEmpty()) sb.deleteCharAt(sb.lastIndex)
println(sb)
// Output: -125.48845080566404 47.94508483691371,-124.96110705566404 42.309040799653665,-117.13884143066404 45.04173793121063,-118.36931018066404 48.93624688577435,-125.48845080566404 47.94508483691371
// val stringRepresentation = sb.toString() // for further use as String data-type
答案3
得分: 1
不要使用Reduce,可以使用flatMap。它会对你有帮助。
List<List<List
, List.of(-117.13884143066404, 45.04173793121063)
, List.of(118.36931018066404, 48.93624688577435)
));
var l = boundingPolygon.stream().flatMap(lists -> lists.stream().flatMap(doubles -> doubles.stream())).collect(Collectors.toList());
System.out.println(l);
它会输出如下结果。
[-124.96110705566404, 42.309040799653665, -117.13884143066404, 45.04173793121063, 118.36931018066404, 48.93624688577435]
尝试使用上述代码,这会对你有帮助。
英文:
Instead of Reduce, you can use flatMap. It will help you.
List<List<List<Double>>> boundingPolygon = List.of(List.of(List.of(-124.96110705566404, 42.309040799653665)
, List.of(-117.13884143066404, 45.04173793121063)
, List.of(118.36931018066404, 48.93624688577435)
));
var l = boundingPolygon.stream().flatMap(lists -> lists.stream().flatMap(doubles -> doubles.stream())).collect(Collectors.toList());
System.out.println(l);
It will print an output as below.
[-124.96110705566404, 42.309040799653665, -117.13884143066404, 45.04173793121063, 118.36931018066404, 48.93624688577435]
Try above code, this will help you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论