diff --git a/CHANGELOG.md b/CHANGELOG.md index da17f5af851ac..b310f7119bafd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -150,6 +150,8 @@ a InstanceTypeError if the instance isn't of the class expected ### Core library changes +* Add `HashMap.fromEntries` and `LinkedHashmap.fromEntries` constructors. + ### Dart VM ### Tool Changes diff --git a/sdk/lib/collection/hash_map.dart b/sdk/lib/collection/hash_map.dart index 84655dc549967..dcdb4ee53a0b5 100644 --- a/sdk/lib/collection/hash_map.dart +++ b/sdk/lib/collection/hash_map.dart @@ -153,4 +153,16 @@ abstract class HashMap implements Map { MapBase._fillMapWithIterables(map, keys, values); return map; } + + /** + * Creates a [HashMap] containing the entries of [entries]. + * + * Returns a new `HashMap` where all entries of [entries] + * have been added in iteration order. + * + * If multiple [entries] have the same key, + * later occurrences overwrite the earlier ones. + */ + factory HashMap.fromEntries(Iterable> entries) => + HashMap()..addEntries(entries); } diff --git a/sdk/lib/collection/linked_hash_map.dart b/sdk/lib/collection/linked_hash_map.dart index b4c2bf4124529..770c6e16a4d04 100644 --- a/sdk/lib/collection/linked_hash_map.dart +++ b/sdk/lib/collection/linked_hash_map.dart @@ -141,4 +141,16 @@ abstract class LinkedHashMap implements Map { MapBase._fillMapWithIterables(map, keys, values); return map; } + + /** + * Creates a [LinkedHashMap] containing the entries of [entries]. + * + * Returns a new `LinkedHashMap` where all entries of [entries] + * have been added in iteration order. + * + * If multiple [entries] have the same key, + * later occurrences overwrite the earlier ones. + */ + factory LinkedHashMap.fromEntries(Iterable> entries) => + {}..addEntries(entries); } diff --git a/sdk/lib/core/map.dart b/sdk/lib/core/map.dart index 4e8a8780df829..42b606a096510 100644 --- a/sdk/lib/core/map.dart +++ b/sdk/lib/core/map.dart @@ -175,8 +175,11 @@ abstract class Map { /** * Creates a new map and adds all entries. * - * Creates a new map like `new Map()` and then adds the key - * and value of eacy entry in [entries] in iteration order. + * Returns a new `Map` where all entries of [entries] + * have been added in iteration order. + * + * If multiple [entries] have the same key, + * later occurrences overwrite the earlier ones. */ factory Map.fromEntries(Iterable> entries) => {}..addEntries(entries); diff --git a/tests/corelib_2/map_from_entries_test.dart b/tests/corelib_2/map_from_entries_test.dart new file mode 100644 index 0000000000000..ef114a7336351 --- /dev/null +++ b/tests/corelib_2/map_from_entries_test.dart @@ -0,0 +1,47 @@ +// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'dart:collection'; + +import 'package:expect/expect.dart'; + +main() { + runTests((entries) => Map.fromEntries(entries)); + runTests((entries) => HashMap.fromEntries(entries)); + runTests((entries) => LinkedHashMap.fromEntries(entries)); +} + +void runTests(Map Function(Iterable>) ctor) { + fromEntriesTest(ctor); + emptyIterableTest(ctor); + equalElementsTest(ctor); +} + +void fromEntriesTest(Map Function(Iterable>) ctor) { + var map = ctor([MapEntry(1, "one"), MapEntry(2, "two")]); + Expect.equals(2, map.length); + Expect.equals(2, map.keys.length); + Expect.equals(2, map.values.length); + Expect.equals("one", map[1]); + Expect.equals("two", map[2]); +} + +void emptyIterableTest( + Map Function(Iterable>) ctor) { + var map = ctor([]); + Expect.equals(0, map.length); + Expect.equals(0, map.keys.length); + Expect.equals(0, map.values.length); +} + +void equalElementsTest( + Map Function(Iterable>) ctor) { + var map = + ctor([MapEntry(1, "one"), MapEntry(2, "two"), MapEntry(2, "other")]); + Expect.equals(2, map.length); + Expect.equals(2, map.keys.length); + Expect.equals(2, map.values.length); + Expect.equals("one", map[1]); + Expect.equals("other", map[2]); +}