Dart如何通过按键过滤地图 [英] Dart How to filter a map by keys

查看:34
本文介绍了Dart如何通过按键过滤地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何根据按键处理地图?

How to handle a map based on the keys?

如果在我的地图中键等于我的变量,那么我想创建一个包含我的地图的列表

If in my map the key is equal to my variable then I want to create a list which contains my map

如果在我的地图中键不等于变量,那么我想创建一个包含2个地图对象的列表,其中键值最近的下限值最近,键值最近的上限值

If in my map the key is not equal to my variablecthen I want to create a list which contains 2 map objects with the key which has the nearest lower value and the key which has the nearest higher value

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的示例,我想要这个:

For the exemple with myVar = 100, I want to have this:

int myVar = 100;
 
  Map values = {
      "100"   : -22,
    };

如果myVar = 112,例如,我需要具有最接近的键值.我的结果必须是:

And if myVar = 112 for exemple I need to have the closest keys values. My result must be :

Map values = {
      "100"   : -22,
      "200"   : -43,
    };

我不知道该怎么做.我可能有将地图转换成地图列表以使用列表功能的想法.

I don't know how to do that.I had perhaps the idea to transform the map into a map list to be able to use the list functions.

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();

这是正确的方法吗?如果可以,如何转换我的基本地图?

is this the right method? How to transform my basic map if yes ?

编辑:在@jamesdlin的帮助下,我尝试了此操作,但出现错误.

EDIT : With the help of @jamesdlin I tried this but I have an error.

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));
}

我在dartpad上有这个东西:

I have this on 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>'

推荐答案

默认情况下, Map LinkedHashMap ,其中迭代顺序是键插入顺序.供您使用,您可能想使用 SplayTreeMap ,其中查找是O(log n)而不是(理想情况下)O(1)(WRT是元素数),但是迭代顺序是键的升序.这将允许您使用 lastKeyBefore(key) firstKeyAfter(key) 方法查找上一个和下一个元素.

By default, Map is a LinkedHashMap, where iteration order is key insertion order. For your use, you instead probably will want to use a SplayTreeMap where lookups are O(log n) instead of (ideally) O(1) (WRT the number of elements), but iteration order is in ascending order of keys. This would allow you to use the lastKeyBefore(key) and firstKeyAfter(key) methods to find the previous and next elements if the key isn't directly contained.

我还建议您使用 int 作为键,而不要使用 String .如果您使用 String ,则默认顺序为词典编目顺序("1" < "10" < "100" < "2" ).您可以提供自己的对 SplayTreeMap 的比较回调,以在比较时将 String s解析为 int s,但这样做仍需要解析相同的 String 多次.直接将 int s作为键存储会更加简单和有效.

I also would recommend that you use int as the key instead of a String. If you use a String, the default ordering will be a lexicographic order ("1" < "10" < "100" < "2"). You could supply you own comparison callback to SplayTreeMap to parse Strings into ints when comparing, but doing so would still require parsing the same Strings multiple times. It'd be much simpler and more efficient to store ints as the keys directly.

int 键与 SplayTreeMap 一起使用,看起来像这样:

Using int keys with a SplayTreeMap, it'd look something like:

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],
  };
}

这篇关于Dart如何通过按键过滤地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆