11

Does the Map class in Dart have a way to ignore case if the key is a string?

Eg.

var map = new Map<String, int>(/*MyComparerThatIgnoresCase*/);
map["MyKey"] = 42;
var shouldBe42 = map["mykey"];

In C# the Dictionary constructor takes a comparer like the comment above. What is the canonical way to do this in Dart?

1

4 Answers 4

11

Maps in Dart have an internal method that compares keys for equality. So far as I know, you can't change this for the default Map class. However, you can use the very similar core LinkedHashMap class, which not only allows, but requires that you specify a key equality method. You can check out more about LinkedHashMaps at https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart:collection.LinkedHashMap

LinkedHashMap<String, String> map = new LinkedHashMap(
    (a, b) => a.toLowerCase() == b.toLowerCase(),
    (key) => key.toLowerCase().hashCode
);

map['Foo'] = 'bar';
print(map['foo']);   //bar
3
  • If you do new Map<X,Y>() you actually get a LinkedHashMap. This can be seen in the factory contructor for Map (factory Map() = LinkedHashMap<K, V>;) Jan 4, 2015 at 10:16
  • There is a function in quiver that does equals while ignoring case. The current implementation just makes them into lowercase but it might be better to use that than providing your own. The code then becomes: import 'package:quiver/strings.dart'; new LinkedHashMap<String, List<String>>(equals: equalsIgnoreCase, hashCode: (String k) => k.toLowerCase().hashCode); Mar 23, 2015 at 10:24
  • stackoverflow.com/a/27786241/116891, below, is the more efficient answer (according to the docstring of CanonicalizedMap itself).
    – Pat
    Jan 17, 2019 at 23:10
8

The way to create a HashMap with a custom equals function (and corresponding custom hashCode function) is to use the optional parameters on the HashMap constructor:

new HashMap<String,Whatever>(equals: (a, b) => a.toUpperCase() == b.toUpperCase(),
                             hashCode: (a) => a.toUpperCase().hashCode);

I really, really recommend finding a way to not do the toUpperCase on every operation!

1
4

You can also do this using package:collection's CanonicalizedMap class. This class is explicitly designed to support maps with "canonical" versions of keys, and is slightly more efficient than passing a custom equality and hash code method to a normal Map.

1
  • This is the preferred method according to its docstring: /// A map whose keys are converted to canonical values of type `C`. /// /// This is useful for using case-insensitive String keys, for example. It's /// more efficient than a [LinkedHashMap] with a custom equality operator /// because it only canonicalizes each key once, rather than doing so for each /// comparison. /// /// By default, `null` is allowed as a key. It can be forbidden via the /// `isValidKey` parameter.
    – Pat
    Jan 17, 2019 at 23:09
0

Dart has a nifty CaseInsensitiveEquality().equals(String a, String b) in their import 'package:collection/collection.dart'; It returns a bool and worked great for me when I was translating strings back to an enum. You do have to run dart pub add collection at the command line to install the package.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.