有没有办法从 dart 中的 Map 获取默认值(如 Java):
static Map DEFAULT_MAPPING = Map.unmodifiable({
"k1": "value"
});
DEFAULT_MAPPING['k1'] //get 'value'
DEFAULT_MAPPING.getOrElse('non-present-key', 'default-value') //something like Java has
Run Code Online (Sandbox Code Playgroud) 我有一个 GeoJSON 简单数据,需要使用 L.CRS.Simple crs 在传单地图上显示,因为它是反子午线数据,有时,坐标可以是 [450,389] (超过 180)
这是非常简单的 GeoJSON:
{
"type": "FeatureCollection",
"name": "entities",
"features": [
{
"type": "Feature",
"properties": {
"Layer": "0",
"SubClasses": "AcDbEntity:AcDbPolyline",
"EntityHandle": "1F9",
"style": "PEN(c:#FF0000)"
},
"geometry": {
"type": "LineString",
"coordinates": [
[
0,
0
],
[
0,
150
],
[
150,
150
],
[
150,
0
],
[
0,
0
]
]
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
使用geojson-vt,(演示页面)我得到这个矩形:

我对 geojson-vt lib 做了一些修改:
投影功能:
function projectX(x, simple, projectionFactor) {
return x / 256 …Run Code Online (Sandbox Code Playgroud) 有没有更好的方法尝试将int转换为可以是或不是整数的字符串?Integer.parseInt(String value)适用于"25"或"019"但不适用于"hello"或"8A".在Java 8中,我们有可选的值,例如:
public static void main(String[] args) {
Optional<Integer> optionalResult = functionThatReturnsOptionalInteger();
Integer finalValue = optionalResult.orElse(0);
System.out.println(finalValue);
}
public static Optional<Integer> functionThatReturnsOptionalInteger() {
Integer[] ints = new Integer[0];
return Stream.of(ints).findAny();
}
Run Code Online (Sandbox Code Playgroud)
您不需要检查空值,因为Optional包装器公开了处理这种情况的有用方法.
但是,如果您想要parseInt一个字符串,可以为null,或者不包含有效整数,则解决方案与以下内容完全相同:
public static Integer parseIntOrDefault(String toParse, int defaultValue) {
try {
return Integer.parseInt(toParse);
} catch (NumberFormatException e) {
return defaultValue;
}
}
Run Code Online (Sandbox Code Playgroud)
如何利用Java 8功能改进这一点,为什么Integer.parseInt()没有重载以在出现错误参数时返回Optional?(或者只是将一个新方法Integer.parseIntOptional()添加到Integer包装器中)