Skip to content

Instantly share code, notes, and snippets.

@Hashibutogarasu
Created May 28, 2024 05:40
Show Gist options
  • Save Hashibutogarasu/e45dcc3505a6842138b078960a5d92e0 to your computer and use it in GitHub Desktop.
Save Hashibutogarasu/e45dcc3505a6842138b078960a5d92e0 to your computer and use it in GitHub Desktop.
キーで重複を削除するやつ
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