Documentation update for LinkedHashMap

Added example code blocks for LinkedHashMap class and includes some minor textual updates to comments.

Closes https://github.com/dart-lang/sdk/pull/47706
https://github.com/dart-lang/sdk/pull/47706

GitOrigin-RevId: 86bded093f3304169aa97d7d6d0e64932ad184be
Change-Id: I8aa18dc04fb67498277c214916a3076428244a00
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/220301
Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
diff --git a/sdk/lib/collection/linked_hash_map.dart b/sdk/lib/collection/linked_hash_map.dart
index 646b581..be71118 100644
--- a/sdk/lib/collection/linked_hash_map.dart
+++ b/sdk/lib/collection/linked_hash_map.dart
@@ -4,7 +4,16 @@
 
 part of dart.collection;
 
-/// A hash-table based implementation of [Map].
+/// An insertion-ordered [Map] with expected constant-time lookup.
+///
+/// A non-constant map literal, like `{"a": 42, "b": 7}`, is a `LinkedHashMap`.
+///
+/// The [keys], [values] and [entries] are iterated in key insertion order.
+///
+/// The map uses a hash-table to look up entries, so keys must have
+/// suitable implementations of [Object.operator==] and [Object.hashCode].
+/// If the hash codes are not well-distributed, the performance of map
+/// operations may suffer.
 ///
 /// The insertion order of keys is remembered,
 /// and keys are iterated in the order they were inserted into the map.
@@ -14,11 +23,93 @@
 /// but removing the key and adding it again
 /// will make it be last in the iteration order.
 ///
+/// **Notice:**
+/// Do not modify a map (add or remove keys) while an operation
+/// is being performed on that map, for example in functions
+/// called during a [forEach] or [putIfAbsent] call,
+/// or while iterating the map ([keys], [values] or [entries]).
+///
 /// The keys of a `LinkedHashMap` must have consistent [Object.==]
 /// and [Object.hashCode] implementations. This means that the `==` operator
 /// must define a stable equivalence relation on the keys (reflexive,
 /// symmetric, transitive, and consistent over time), and that `hashCode`
 /// must be the same for objects that are considered equal by `==`.
+///
+/// Example:
+///
+/// ```dart
+/// final planetsByDiameter = {0.949: 'Venus'}; // A new LinkedHashMap
+/// ```
+/// To add data to a map, use [operator[]=], [addAll] or [addEntries].
+/// ```
+/// planetsByDiameter[1] = 'Earth';
+/// planetsByDiameter.addAll({0.532: 'Mars', 11.209: 'Jupiter'});
+/// ```
+/// To check if the map is empty, use [isEmpty] or [isNotEmpty].
+/// To find the number of map entries, use [length].
+/// ```
+/// print(planetsByDiameter.isEmpty); // false
+/// print(planetsByDiameter.length); // 4
+/// print(planetsByDiameter);
+/// // {0.949: Venus, 1.0: Earth, 0.532: Mars, 11.209: Jupiter}
+/// ```
+/// The [forEach] method calls a function for each key/value entry of the map.
+/// ```
+/// planetsByDiameter.forEach((key, value) {
+///   print('$key \t $value');
+///   // 0.949    Venus
+///   // 1.0      Earth
+///   // 0.532    Mars
+///   // 11.209   Jupiter
+/// });
+/// ```
+/// To check whether the map has an entry with a specific key, use [containsKey].
+/// ```
+/// final keyOneExists = planetsByDiameter.containsKey(1); // true
+/// final keyFiveExists = planetsByDiameter.containsKey(5); // false
+/// ```
+/// To check whether the map has an entry with a specific value,
+/// use [containsValue].
+/// ```
+/// final earthExists = planetsByDiameter.containsValue('Earth'); // true
+/// final saturnExists =  planetsByDiameter.containsValue('Saturn'); // false
+/// ```
+/// To remove an entry with a specific key, use [remove].
+/// ```
+/// final removedValue = planetsByDiameter.remove(1);
+/// print(removedValue); // Earth
+/// print(planetsByDiameter); // {0.949: Venus, 0.532: Mars, 11.209: Jupiter}
+/// ```
+/// To remove multiple entries at the same time, based on their keys and values,
+/// use [removeWhere].
+/// ```
+/// planetsByDiameter.removeWhere((key, value) => key == 0.949);
+/// print(planetsByDiameter); // {0.532: Mars, 11.209: Jupiter}
+/// ```
+/// To conditionally add or modify a value for a specific key, depending on
+/// whether there already is an entry with that key,
+/// use [putIfAbsent] or [update].
+/// ```
+/// planetsByDiameter.update(0.949, (v) => 'Venus', ifAbsent: () => 'Venus');
+/// planetsByDiameter.putIfAbsent(0.532, () => "Another Mars if needed");
+/// print(planetsByDiameter); // {0.532: Mars, 11.209: Jupiter, 0.949: Venus}
+/// ```
+/// To update the values of all keys, based on the existing key and value,
+/// use [updateAll].
+/// ```
+/// planetsByDiameter.updateAll((key, value) => 'X');
+/// print(planetsByDiameter); // {0.532: X, 11.209: X, 0.949: X}
+/// ```
+/// To remove all entries and empty the map, use [clear].
+/// ```
+/// planetsByDiameter.clear();
+/// print(planetsByDiameter); // {}
+/// print(planetsByDiameter.isEmpty); // true
+/// ```
+/// **See also:**
+/// * [Map], the general interface of key/value pair collections.
+/// * [HashMap] is unordered (the order of iteration is not guaranteed).
+/// * [SplayTreeMap] iterates the keys in sorted order.
 abstract class LinkedHashMap<K, V> implements Map<K, V> {
   /// Creates an insertion-ordered hash-table based [Map].
   ///
@@ -26,7 +117,7 @@
   /// new keys. If [equals] is omitted, the key's own [Object.==] is used
   /// instead.
   ///
-  /// Similar, if [hashCode] is provided, it is used to produce a hash value
+  /// Similarly, if [hashCode] is provided, it is used to produce a hash value
   /// for keys in order to place them in the hash table. If it is omitted, the
   /// key's own [Object.hashCode] is used.
   ///
@@ -35,7 +126,7 @@
   /// of an object, or what it compares equal to, should not change while the
   /// object is in the table. If it does change, the result is unpredictable.
   ///
-  /// If you supply one of [equals] and [hashCode],
+  /// If you supply one of [equals] or [hashCode],
   /// you should generally also supply the other.
   ///
   /// Some [equals] or [hashCode] functions might not work for all objects.
@@ -73,7 +164,7 @@
 
   /// Creates an insertion-ordered identity-based map.
   ///
-  /// Effectively a shorthand for:
+  /// Effectively shorthand for:
   /// ```dart template:expression
   /// LinkedHashMap<K, V>(equals: identical,
   ///                     hashCode: identityHashCode)
@@ -84,6 +175,12 @@
   ///
   /// The keys must all be instances of [K] and the values to [V].
   /// The [other] map itself can have any type.
+  /// Example:
+  /// ```dart
+  /// final baseMap = <num, Object>{1: 'A', 2: 'B', 3: 'C'};
+  /// final fromBaseMap = LinkedHashMap<int, String>.from(baseMap);
+  /// print(fromBaseMap); // {1: A, 2: B, 3: C}
+  /// ```
   factory LinkedHashMap.from(Map<dynamic, dynamic> other) {
     LinkedHashMap<K, V> result = LinkedHashMap<K, V>();
     other.forEach((dynamic k, dynamic v) {
@@ -93,20 +190,33 @@
   }
 
   /// Creates a [LinkedHashMap] that contains all key value pairs of [other].
+  /// Example:
+  /// ```dart
+  /// final baseMap = <int, String> {3: 'A', 2: 'B', 1: 'C', 4: 'D'};
+  /// final mapOf = LinkedHashMap<num, Object>.of(baseMap);
+  /// print(mapOf); // {3: A, 2: B, 1: C, 4: D}
+  /// ```
   factory LinkedHashMap.of(Map<K, V> other) =>
       LinkedHashMap<K, V>()..addAll(other);
 
   /// Creates a [LinkedHashMap] where the keys and values are computed from the
   /// [iterable].
   ///
-  /// For each element of the [iterable] this constructor computes a key/value
-  /// pair, by applying [key] and [value] respectively.
+  /// For each element of the [iterable], this constructor computes a key/value
+  /// pair by applying [key] and [value] respectively.
   ///
   /// The keys of the key/value pairs do not need to be unique. The last
   /// occurrence of a key will simply overwrite any previous value.
   ///
-  /// If no values are specified for [key] and [value] the default is the
-  /// identity function.
+  /// If no values are specified for [key] and [value], the default is the
+  /// both default to the identity function.
+  /// Example:
+  /// ```dart
+  /// final numbers = [11, 12, 13, 14];
+  /// final mapFromIterable =
+  ///     LinkedHashMap.fromIterable(numbers, key: (i) => i, value: (i) => i * i);
+  /// print(mapFromIterable); // {11: 121, 12: 144, 13: 169, 14: 196}
+  /// ```
   factory LinkedHashMap.fromIterable(Iterable iterable,
       {K Function(dynamic element)? key, V Function(dynamic element)? value}) {
     LinkedHashMap<K, V> map = LinkedHashMap<K, V>();
@@ -123,6 +233,14 @@
   /// overwrites the previous value.
   ///
   /// It is an error if the two [Iterable]s don't have the same length.
+  /// Example:
+  /// ```dart
+  /// final values = [0.06, 0.81, 1, 0.11];
+  /// final keys = ['Mercury', 'Venus', 'Earth', 'Mars'];
+  /// final mapFromIterables = LinkedHashMap.fromIterables(keys, values);
+  /// print(mapFromIterables);
+  /// // {Mercury: 0.06, Venus: 0.81, Earth: 1, Mars: 0.11}
+  /// ```
   factory LinkedHashMap.fromIterables(Iterable<K> keys, Iterable<V> values) {
     LinkedHashMap<K, V> map = LinkedHashMap<K, V>();
     MapBase._fillMapWithIterables(map, keys, values);
@@ -136,6 +254,12 @@
   ///
   /// If multiple [entries] have the same key,
   /// later occurrences overwrite the earlier ones.
+  /// Example:
+  /// ```dart
+  /// final numbers = [11, 12, 13, 14];
+  /// final map = LinkedHashMap.fromEntries(numbers.map((i) => MapEntry(i, i * i)));
+  /// print(map); // {11: 121, 12: 144, 13: 169, 14: 196}
+  /// ```
   @Since("2.1")
   factory LinkedHashMap.fromEntries(Iterable<MapEntry<K, V>> entries) =>
       LinkedHashMap<K, V>()..addEntries(entries);