Created
May 28, 2024 05:40
-
-
Save Hashibutogarasu/e45dcc3505a6842138b078960a5d92e0 to your computer and use it in GitHub Desktop.
キーで重複を削除するやつ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.git.hashibutogarasu; | |
import org.jetbrains.annotations.NotNull; | |
import java.util.List; | |
import java.util.Set; | |
import java.util.concurrent.ConcurrentHashMap; | |
import java.util.function.Function; | |
public class Utils { | |
public static <T> List<T> distinctByKey(@NotNull List<T> list, Function<? super T, ?> keyExtractor) { | |
Set<Object> seen = ConcurrentHashMap.newKeySet(); | |
seen.addAll(list); | |
return list.stream() | |
.filter(t -> seen.add(keyExtractor.apply(t))) | |
.toList(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment