Skip to content

Commit

Permalink
添加 toMap 重载, 重复 value 合并
Browse files Browse the repository at this point in the history
  • Loading branch information
timandy committed Nov 28, 2023
1 parent f83b7fa commit 81ceb58
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/main/java/com/bestvike/linq/IEnumerable.java
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,10 @@ default <TKey, TElement> Map<TKey, TElement> toLinkedMap(Func1<? super TSource,
return ToCollection.toLinkedMap(this, (Func1<TSource, TKey>) keySelector, (Func1<TSource, TElement>) elementSelector);
}

default <TKey, TElement> Map<TKey, TElement> toLinkedMap(Func1<? super TSource, ? extends TKey> keySelector, Func1<? super TSource, ? extends TElement> elementSelector, Func2<? super TElement,? super TElement,? extends TElement> resultElementSelector) {
return ToCollection.toLinkedMap(this, (Func1<TSource, TKey>) keySelector, (Func1<TSource, TElement>) elementSelector, (Func2< TElement, TElement, TElement>) resultElementSelector);
}

default Set<TSource> toLinkedSet() {
return ToCollection.toLinkedSet(this);
}
Expand Down Expand Up @@ -1149,6 +1153,10 @@ default <TKey, TElement> Map<TKey, TElement> toMap(Func1<? super TSource, ? exte
return ToCollection.toMap(this, (Func1<TSource, TKey>) keySelector, (Func1<TSource, TElement>) elementSelector);
}

default <TKey, TElement> Map<TKey, TElement> toMap(Func1<? super TSource, ? extends TKey> keySelector, Func1<? super TSource, ? extends TElement> elementSelector, Func2<? super TElement,? super TElement,? extends TElement> resultElementSelector) {
return ToCollection.toMap(this, (Func1<TSource, TKey>) keySelector, (Func1<TSource, TElement>) elementSelector, (Func2< TElement, TElement, TElement>) resultElementSelector);
}

default Set<TSource> toSet() {
return ToCollection.toSet(this);
}
Expand Down
91 changes: 91 additions & 0 deletions src/main/java/com/bestvike/linq/enumerable/ToCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.bestvike.collections.generic.ICollection;
import com.bestvike.function.Action2;
import com.bestvike.function.Func1;
import com.bestvike.function.Func2;
import com.bestvike.linq.IEnumerable;
import com.bestvike.linq.IEnumerator;
import com.bestvike.linq.exception.ExceptionArgument;
Expand Down Expand Up @@ -183,6 +184,52 @@ public static <TSource, TKey, TElement> Map<TKey, TElement> toMap(IEnumerable<TS
return map;
}

public static <TSource, TKey, TElement> Map<TKey, TElement> toMap(IEnumerable<TSource> source, Func1<TSource, TKey> keySelector, Func1<TSource, TElement> elementSelector, Func2<TElement, TElement, TElement> resultElementSelector) {
if (source == null)
ThrowHelper.throwArgumentNullException(ExceptionArgument.source);
if (keySelector == null)
ThrowHelper.throwArgumentNullException(ExceptionArgument.keySelector);
if (elementSelector == null)
ThrowHelper.throwArgumentNullException(ExceptionArgument.elementSelector);
if (resultElementSelector == null)
ThrowHelper.throwArgumentNullException(ExceptionArgument.resultElementSelector);

if (source instanceof ICollection) {
ICollection<TSource> collection = (ICollection<TSource>) source;
int capacity = collection._getCount();
if (capacity == 0)
return new HashMap<>();

if (collection instanceof IArrayList) {
IArrayList<TSource> list = (IArrayList<TSource>) collection;
Map<TKey, TElement> map = new HashMap<>(initialCapacity(capacity));
for (int i = 0; i < capacity; i++) {
TSource element = list.get(i);
map.merge(keySelector.apply(element), elementSelector.apply(element), resultElementSelector::apply);
}
return map;
}

Map<TKey, TElement> map = new HashMap<>(initialCapacity(capacity));
try (IEnumerator<TSource> e = source.enumerator()) {
while (e.moveNext()) {
TSource element = e.current();
map.merge(keySelector.apply(element), elementSelector.apply(element), resultElementSelector::apply);
}
}
return map;
}

Map<TKey, TElement> map = new HashMap<>();
try (IEnumerator<TSource> e = source.enumerator()) {
while (e.moveNext()) {
TSource element = e.current();
map.merge(keySelector.apply(element), elementSelector.apply(element), resultElementSelector::apply);
}
}
return map;
}

public static <TSource, TKey> Map<TKey, TSource> toLinkedMap(IEnumerable<TSource> source, Func1<TSource, TKey> keySelector) {
if (source == null)
ThrowHelper.throwArgumentNullException(ExceptionArgument.source);
Expand Down Expand Up @@ -269,6 +316,50 @@ public static <TSource, TKey, TElement> Map<TKey, TElement> toLinkedMap(IEnumera
return map;
}

public static <TSource, TKey, TElement> Map<TKey, TElement> toLinkedMap(IEnumerable<TSource> source, Func1<TSource, TKey> keySelector, Func1<TSource, TElement> elementSelector, Func2<TElement, TElement, TElement> resultElementSelector) {
if (source == null)
ThrowHelper.throwArgumentNullException(ExceptionArgument.source);
if (keySelector == null)
ThrowHelper.throwArgumentNullException(ExceptionArgument.keySelector);
if (elementSelector == null)
ThrowHelper.throwArgumentNullException(ExceptionArgument.elementSelector);

if (source instanceof ICollection) {
ICollection<TSource> collection = (ICollection<TSource>) source;
int capacity = collection._getCount();
if (capacity == 0)
return new LinkedHashMap<>();

if (collection instanceof IArrayList) {
IArrayList<TSource> list = (IArrayList<TSource>) collection;
Map<TKey, TElement> map = new LinkedHashMap<>(initialCapacity(capacity));
for (int i = 0; i < capacity; i++) {
TSource element = list.get(i);
map.merge(keySelector.apply(element), elementSelector.apply(element), resultElementSelector::apply);
}
return map;
}

Map<TKey, TElement> map = new LinkedHashMap<>(initialCapacity(capacity));
try (IEnumerator<TSource> e = source.enumerator()) {
while (e.moveNext()) {
TSource element = e.current();
map.merge(keySelector.apply(element), elementSelector.apply(element), resultElementSelector::apply);
}
}
return map;
}

Map<TKey, TElement> map = new LinkedHashMap<>();
try (IEnumerator<TSource> e = source.enumerator()) {
while (e.moveNext()) {
TSource element = e.current();
map.merge(keySelector.apply(element), elementSelector.apply(element), resultElementSelector::apply);
}
}
return map;
}

public static <TSource> Set<TSource> toSet(IEnumerable<TSource> source) {
if (source == null)
ThrowHelper.throwArgumentNullException(ExceptionArgument.source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public enum ExceptionArgument {
options,
other,
range,
resultElementSelector,
start,
startIndex,
value,
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/com/bestvike/linq/enumerable/ToLinkedMapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,19 @@ void testToMapWithSelector() {
assertEquals(2, map2.size());
}

@Test
void testToMapWithResultElementSelector() {
Map<Integer, String> map = Linq.of(emps).toLinkedMap(emp -> emp.empno, emp -> emp.name,(x,y)->x);
assertTrue(map.get(110).equals("Bill"));
assertEquals(4, map.size());

//key 重复,保留第一个
Map<Integer, String> map2 = Linq.of(emps).toLinkedMap(emp -> emp.deptno, emp -> emp.name,(x,y)->y);
assertEquals(emps[3].name, map2.get(10));
assertEquals(emps[1].name, map2.get(30));
assertEquals(2, map2.size());
}


private static class NameScore extends ValueType {
private final String Name;
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/com/bestvike/linq/enumerable/ToMapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,19 @@ void testToMapWithSelector() {
assertEquals(2, map2.size());
}

@Test
void testToMapWithResultElementSelector() {
Map<Integer, String> map = Linq.of(emps).toMap(emp -> emp.empno, emp -> emp.name, (x, y) -> x);
assertTrue(map.get(110).equals("Bill"));
assertEquals(4, map.size());

//key 重复,保留第一个
Map<Integer, String> map2 = Linq.of(emps).toMap(emp -> emp.deptno, emp -> emp.name, (x, y) -> y);
assertEquals(emps[3].name, map2.get(10));
assertEquals(emps[1].name, map2.get(30));
assertEquals(2, map2.size());
}


private static class NameScore extends ValueType {
private final String Name;
Expand Down

0 comments on commit 81ceb58

Please sign in to comment.