From eb97a2b6cc7d7c5402c04f863f8e482b21a6c1ae Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 3 Aug 2025 08:17:47 +0100 Subject: Add toMapPreferFirst() --- Mammoth/Couscous/mammoth/internal/util/Maps.cs | 61 +++++++++++++++++--------- 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/Mammoth/Couscous/mammoth/internal/util/Maps.cs b/Mammoth/Couscous/mammoth/internal/util/Maps.cs index 0902fd3..7848f13 100644 --- a/Mammoth/Couscous/mammoth/internal/util/Maps.cs +++ b/Mammoth/Couscous/mammoth/internal/util/Maps.cs @@ -3,6 +3,7 @@ using Mammoth.Couscous.java.util; using Mammoth.Couscous.java.lang; using Mammoth.Couscous.java.util.function; using Mammoth.Couscous; +using System.Collections.Generic; namespace Mammoth.Couscous.org.zwobble.mammoth.@internal.util { @@ -11,20 +12,20 @@ namespace Mammoth.Couscous.org.zwobble.mammoth.@internal.util internal static Map map() { return ToJava.DictionaryToMap(new System.Collections.Generic.Dictionary()); } - + internal static Map map(K key1, V value1) { return ToJava.DictionaryToMap(new System.Collections.Generic.Dictionary { {key1, value1} }); } - + internal static Map map(K key1, V value1, K key2, V value2) { return ToJava.DictionaryToMap(new System.Collections.Generic.Dictionary { {key1, value1}, {key2, value2} }); } - + internal static Map map(K key1, V value1, K key2, V value2, K key3, V value3) { return ToJava.DictionaryToMap(new System.Collections.Generic.Dictionary { {key1, value1}, @@ -32,15 +33,33 @@ namespace Mammoth.Couscous.org.zwobble.mammoth.@internal.util {key3, value3} }); } - + internal static Map toMap(Iterable iterable, Function> function) { var dictionary = FromJava.IterableToEnumerable(iterable) .Select(function.apply) .ToDictionary(entry => entry.getKey(), entry => entry.getValue()); return ToJava.DictionaryToMap(dictionary); - + + } + + /// + /// Convert an iterable to map using the given mapping function. If the + /// mapping function generates entries with the same key, only the first + /// entry will be used, and subsequent entries will be ignored. + /// + internal static Map toMapPreferFirst(Iterable iterable, Function> function) { + var dictionary = new Dictionary(); + + foreach (var element in FromJava.IterableToEnumerable(iterable)) { + var entry = function.apply(element); + if (!dictionary.ContainsKey(entry.getKey())) { + dictionary[entry.getKey()] = entry.getValue(); + } + } + + return ToJava.DictionaryToMap(dictionary); } - + internal static Map toMapWithKey(Iterable iterable, Function function) { var dictionary = FromJava.IterableToEnumerable(iterable) .ToDictionary(function.apply); @@ -55,7 +74,7 @@ namespace Mammoth.Couscous.org.zwobble.mammoth.@internal.util grouping => ToJava.ListToList(grouping.ToList())); return ToJava.DictionaryToMap(dictionary); } - + internal static Map> toMultiMap(Iterable iterable, Function> function) { var dictionary = FromJava.IterableToEnumerable(iterable) .Select(element => function.apply(element)) @@ -65,11 +84,11 @@ namespace Mammoth.Couscous.org.zwobble.mammoth.@internal.util grouping => ToJava.ListToList(grouping.Select(entry => entry.getValue()).ToList())); return ToJava.DictionaryToMap(dictionary); } - + internal static Optional lookup(Map map, K key) { return map._lookup(key); } - + internal static Map eagerMapKeys(Map map, Function function) { var dictionary = FromJava.MapToDictionary(map) .ToDictionary( @@ -77,7 +96,7 @@ namespace Mammoth.Couscous.org.zwobble.mammoth.@internal.util entry => entry.Value); return ToJava.DictionaryToMap(dictionary); } - + internal static Map eagerMapValues(Map map, Function function) { var dictionary = FromJava.MapToDictionary(map) .ToDictionary( @@ -85,28 +104,28 @@ namespace Mammoth.Couscous.org.zwobble.mammoth.@internal.util entry => function.apply(entry.Value)); return ToJava.DictionaryToMap(dictionary); } - + internal static Map__Entry entry(K key, V value) { return new Entry(key, value); } - + private class Entry : Map__Entry { private readonly K _key; private readonly V _value; - + internal Entry(K key, V value) { this._key = key; this._value = value; } - + public K getKey() { return _key; } - + public V getValue() { return _value; } - + public override bool Equals(object otherObj) { var other = otherObj as Entry; if (other == null) { @@ -122,24 +141,24 @@ namespace Mammoth.Couscous.org.zwobble.mammoth.@internal.util return _key.GetHashCode() * 17 + _value.GetHashCode(); } } - + internal static Builder builder() { return new Builder(); } - + internal class Builder { private readonly System.Collections.Generic.Dictionary _values = new System.Collections.Generic.Dictionary(); - + internal Builder put(K key, V value) { _values[key] = value; return this; } - + internal Map build() { return ToJava.DictionaryToMap(_values); } } - + internal static Map mutableMap(K key1, V value1) { var map = new HashMap(); map.put(key1, value1); -- cgit v1.2.3