Created
June 29, 2022 11:04
-
-
Save maykon-oliveira/ef90e4604d55e3ea0ad3f6ea86c1ab25 to your computer and use it in GitHub Desktop.
Meu set de classes útil para Java
This file contains hidden or 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 br.com.logann.sgbe.util; | |
import java.util.Collection; | |
import java.util.HashSet; | |
import java.util.Set; | |
import java.util.function.Function; | |
/** | |
* Example: | |
* | |
* Set<Long> ids = CollectionsUtil.extractFieldSet(ItemDto::getId, listOfItems); | |
*/ | |
public class CollectionsUtil { | |
private CollectionsUtil() { | |
} | |
/** | |
* @param mapper A class field method reference. | |
* @param collection Collection to extract field. | |
* @param <T> Set's type. | |
* @param <R> Collection's type. | |
* @return A Set of a field of collection. | |
*/ | |
public static <T, R> Set<T> extractFieldSet(Function<R, T> mapper, Collection<R> collection) { | |
final Set<T> set = new HashSet<>(); | |
for (R item : collection) { | |
set.add(mapper.apply(item)); | |
} | |
return set; | |
} | |
} |
This file contains hidden or 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 br.com.logann.sgbe.util; | |
import java.util.*; | |
import java.util.function.BiConsumer; | |
import java.util.function.BinaryOperator; | |
import java.util.function.Function; | |
import java.util.function.Supplier; | |
import java.util.stream.Collector; | |
public class MapCollector<A, B> implements Collector<A, Collection<B>, Collection<B>> { | |
private final Function<A, B> mapper; | |
private final Supplier<Collection<B>> collectionFactory; | |
private MapCollector(Function<A, B> mapper, Supplier<Collection<B>> collectionFactory) { | |
this.mapper = mapper; | |
this.collectionFactory = collectionFactory; | |
} | |
@Override | |
public Supplier<Collection<B>> supplier() { | |
return collectionFactory; | |
} | |
@Override | |
public BiConsumer<Collection<B>, A> accumulator() { | |
return (bs, a) -> bs.add(mapper.apply(a)); | |
} | |
@Override | |
public BinaryOperator<Collection<B>> combiner() { | |
return (bs, bs2) -> { | |
bs.addAll(bs2); | |
return bs; | |
}; | |
} | |
@Override | |
public Function<Collection<B>, Collection<B>> finisher() { | |
return bs -> bs; | |
} | |
@Override | |
public Set<Characteristics> characteristics() { | |
return Collections.singleton(Characteristics.UNORDERED); | |
} | |
public static <A, B> Collector<A, List<B>, List<B>> mapper(Function<A, B> mapper) { | |
return new MapCollector(mapper, ArrayList::new); | |
} | |
public static <A, B> Collector<A, List<B>, List<B>> mapper(Function<A, B> mapper, Supplier<Collection<B>> collectionFactory) { | |
return new MapCollector(mapper, collectionFactory); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment