Flutter Dart如何按关键点过滤地图
如何处理基于键的地图 如果在我的映射中键等于我的变量,那么我想创建一个包含我的映射的列表 如果在我的地图中,键不等于我的变量,那么我要创建一个列表,其中包含两个地图对象,其中键具有最接近的较低值,键具有最接近的较高值Flutter Dart如何按关键点过滤地图,flutter,dart,Flutter,Dart,如何处理基于键的地图 如果在我的映射中键等于我的变量,那么我想创建一个包含我的映射的列表 如果在我的地图中,键不等于我的变量,那么我要创建一个列表,其中包含两个地图对象,其中键具有最接近的较低值,键具有最接近的较高值 int myVar = 100; Map values = { "-900" : 183, "-800" : 164, "-700" : 144, "
int myVar = 100;
Map values = {
"-900" : 183,
"-800" : 164,
"-700" : 144,
"-600" : 124,
"-500" : 104,
"-400" : 84,
"-300" : 63,
"-200" : 42,
"-100" : 21,
"0" : 0,
"100" : -22,
"200" : -43,
"300" : -64
};
对于myVar=100的示例,我希望有以下内容:
int myVar = 100;
Map values = {
"100" : -22,
};
例如,如果myVar=112,我需要最接近的键值。我的结果必须是:
Map values = {
"100" : -22,
"200" : -43,
};
我不知道该怎么做。我可能有一个想法,将地图转换成地图列表,以便能够使用列表函数
List<Map> values = [
{
"arg1" :-900,
"arg2": 183
},
{
"arg1" :-800,
"arg2": 164
},
{
"arg1" :-700,
"arg2": 144
},
// Some other values ...
];
List newValues = values.where((c) => c['arg1'] == 100).toList();
列表值=[
{
“arg1”:-900,
“arg2”:183
},
{
“arg1”:-800,
“arg2”:164
},
{
“arg1”:-700,
“arg2”:144
},
//其他一些价值观。。。
];
列出newValues=values.where((c)=>c['arg1']==100.toList();
这是正确的方法吗?如果是,如何转换我的基本地图
编辑:在@jamesdlin的帮助下,我尝试了这个方法,但出现了一个错误
import 'dart:collection';
void main() {
int myVar = 100;
Map<int, int> values = {
-900 : 183,
-800 : 164,
-700 : 144,
-600 : 124,
-500 : 104,
-400 : 84,
-300 : 63,
-200 : 42,
-100 : 21,
0 : 0,
100 : -22,
200 : -43,
300 : -64
};
print(values);
Map<int, int> filter(int myVar, SplayTreeMap<int, int> values) {
if (values.containsKey(myVar)) {
return {myVar: values[myVar]};
}
int lowerKey = values.lastKeyBefore(myVar);
int upperKey = values.firstKeyAfter(myVar);
return {
if (lowerKey != null) lowerKey: values[lowerKey],
if (upperKey != null) upperKey: values[upperKey],
};
}
print(filter(myVar, values));
}
导入“dart:collection”;
void main(){
int-myVar=100;
映射值={
-900 : 183,
-800 : 164,
-700 : 144,
-600 : 124,
-500 : 104,
-400 : 84,
-300 : 63,
-200 : 42,
-100 : 21,
0 : 0,
100 : -22,
200 : -43,
300 : -64
};
打印(值);
映射过滤器(int myVar、SplayTreeMap值){
if(值.containsKey(myVar)){
返回{myVar:values[myVar]};
}
int lowerKey=values.lastKeyBefore(myVar);
int upperKey=values.firstKeyAfter(myVar);
返回{
如果(lowerKey!=null)lowerKey:values[lowerKey],
如果(upperKey!=null)upperKey:values[upperKey],
};
}
打印(过滤器(myVar,值));
}
我在dartpad上有这个:
: TypeError: Instance of 'JsLinkedHashMap<int, int>': type 'JsLinkedHashMap<int, int>' is not a subtype of type 'SplayTreeMap<int, int>'Error: TypeError: Instance of 'JsLinkedHashMap<int, int>': type 'JsLinkedHashMap<int, int>' is not a subtype of type 'SplayTreeMap<int, int>'
:TypeError:JsLinkedHashMap的实例:“JsLinkedHashMap”类型不是“SplayTreeMap”类型的子类型错误:TypeError:JsLinkedHashMap的实例:“JsLinkedHashMap”类型不是“SplayTreeMap”类型的子类型
默认情况下,Map
是一个LinkedHashMap
,其中迭代顺序是键插入顺序。对于您的使用,您可能会希望使用查找为O(logn)而不是(理想情况下)O(1)(WRT元素数),但迭代顺序是键的升序。如果没有直接包含键,这将允许您使用和方法查找上一个和下一个元素
我还建议您使用int
作为键,而不是字符串
。如果使用字符串
,默认顺序将是字典顺序(“1”
“10”
“100”
“2”
)。您可以向SplayTreeMap
提供自己的比较回调,以便在比较时将String
s解析为int
s,但这样做仍需要多次解析相同的String
s。直接将int
s存储为键会更简单、更高效
使用int
键和SplayTreeMap
,它看起来像:
Map过滤器(int myVar,SplayTreeMap值){
if(值.containsKey(myVar)){
返回{myVar:values[myVar]};
}
int lowerKey=values.lastKeyBefore(myVar);
int upperKey=values.firstKeyAfter(myVar);
返回{
如果(lowerKey!=null)lowerKey:values[lowerKey],
如果(upperKey!=null)upperKey:values[upperKey],
};
}
谢谢您抽出时间。我理解您的解释,但我有一个错误。我编辑我的代码。@user2895656您的值
变量被创建为映射
,正如我所解释的,它是一个LinkedHashMap
。您需要使用从现有的映射创建SplayTreeMap
。例如:var values=SplayTreeMap.of({-900:183,…})代码>。