Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Find key in map of List (Set) by one of List (Set) values

In Map<String, Set<Type>> I want to find key which contains Set which contains given Type (Value of Set).

Have:

final map = <String, Set<Type>>{
  'key1': <Type>{
    Type1,
    Type2,
  },
  'key2': <Type>{
    Type3,
    Type4,
  },
}

Want:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

find(map, Type3); // 'key2'

>Solution :

You can use Map#entries with firstWhere (or firstWhereOrNull from package:collection)

import 'package:collection/collection.dart';

typedef Type1 = int;
typedef Type2 = double;
typedef Type3 = String;
typedef Type4 = Object;

void main() {
  final map = <String, Set<Type>>{
    'key1': <Type>{
      Type1,
      Type2,
    },
    'key2': <Type>{
      Type3,
      Type4,
    },
  };

  final search = Type3;
  print(
    map.entries.firstWhereOrNull(
      (entry) => entry.value.contains(search),
    )?.key,
  );
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading