
solid.collectors.ToSolidMap Maven / Gradle / Ivy
package solid.collectors;
import java.util.Map;
import solid.collections.Pair;
import solid.collections.SolidMap;
import solid.functions.Func1;
import static solid.stream.Stream.stream;
public class ToSolidMap {
/**
* Returns a function that converts a stream into {@link SolidMap} using given key and value extractor methods.
*/
public static Func1, SolidMap> toSolidMap(final Func1 keyExtractor, final Func1 valueExtractor) {
return new Func1, SolidMap>() {
@Override
public SolidMap call(final Iterable iterable) {
return new SolidMap<>(
stream(iterable).map(new Func1>() {
@Override
public Pair call(T value) {
return new Pair<>(keyExtractor.call(value), valueExtractor.call(value));
}
}));
}
};
}
/**
* Returns a function that converts a stream into {@link SolidMap} using a given key extractor method.
*/
public static Func1, SolidMap> toSolidMap(final Func1 keyExtractor) {
return toSolidMap(keyExtractor, new Func1() {
@Override
public T call(T value) {
return value;
}
});
}
/**
* Returns a function that converts a stream of {@link java.util.Map.Entry} into {@link SolidMap}.
*/
public static Func1>, SolidMap> toSolidMap() {
return new Func1>, SolidMap>() {
@Override
public SolidMap call(Iterable> iterable) {
return new SolidMap<>(stream(iterable).map(new Func1, Pair>() {
@Override
public Pair call(Map.Entry value) {
return new Pair<>(value.getKey(), value.getValue());
}
}));
}
};
}
/**
* Returns a function that converts a stream of {@link Pair} into {@link SolidMap}.
*/
public static Func1>, SolidMap> pairsToSolidMap() {
return new Func1>, SolidMap>() {
@Override
public SolidMap call(Iterable> iterable) {
return new SolidMap<>(iterable);
}
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy