com.nls.util.Ids Maven / Gradle / Ivy
package com.nls.util;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
public final class Ids {
private Ids() {
}
public static Optional get(Collection identifiables, Integer id) {
return identifiables.stream().filter(o -> o.getId().equals(id)).findAny();
}
public static T find(Collection identifiables, int id) {
return identifiables.stream().filter(o -> o.getId() != null && o.getId() == id).findFirst().orElse(null);
}
public static Set collect(Collection identifiables) {
return identifiables.stream().map(HasId::getId).collect(Collectors.toSet());
}
public static Map index(Collection identifiables) {
return identifiables.stream().collect(Collectors.toMap(HasId::getId, Function.identity()));
}
public static List select(Collection identifiables, Set ids) {
return identifiables.stream().filter(o -> ids.contains(o.getId())).collect(Collectors.toList());
}
}