List of usage examples for java.util.stream Stream filter
Stream<T> filter(Predicate<? super T> predicate);
From source file:Main.java
public static void main(String[] args) { List<String> names = Arrays.asList("Chris", "HTML", "XML", "CSS", ""); Stream<String> s = names.stream().filter(name -> name.startsWith("C")); List<String> collect = s.filter(Objects::nonNull).collect(Collectors.toList()); System.out.println(collect);/*from w w w . jav a 2s .c o m*/ }
From source file:Main.java
public static void main(String[] args) { List<String> names = Arrays.asList("Chris", "HTML", "XML", "CSS", ""); Stream<String> s = names.stream(); Predicate<String> isEmpty = String::isEmpty; Predicate<String> notEmpty = isEmpty.negate(); System.out.println(s.filter(notEmpty).collect(Collectors.toList())); }
From source file:com.liferay.ide.core.util.FileListing.java
public static List<IPath> getFileListing(File dir, String fileType) { Collection<File> files = FileUtils.listFiles(dir, new String[] { fileType }, true); Stream<File> stream = files.stream(); return stream.filter(file -> file.exists()).map(file -> new Path(file.getPath())) .collect(Collectors.toList()); }
From source file:Main.java
private static <T> List<T> where(Stream<T> stream, Predicate<T> filter) { return stream.filter(filter).collect(Collectors.toList()); }
From source file:Main.java
/** * This is a shortcut for {@code (Stream<O>) s.filter(o -> clazz.isInstance(o))}. * * @param s stream that's elements are filtered by type {@code clazz}. Must not be {@code null}. * @param clazz Type the elements of the input stream should be of to be part of the returned result stream. * Must not be {@code null}. * @param <I> Type of elements in input stream * @param <O> Type of element remaining in the output stream * @return input stream filtered by type {@code O} * @throws NullPointerException when {@code clazz} or {@code s} is {@code null}. *//*from ww w. j a v a2s . co m*/ @SuppressWarnings("unchecked") // we know the cast is safe, since all elements are of type O public static <I, O> Stream<O> filter(Stream<I> s, Class<O> clazz) throws NullPointerException { Objects.requireNonNull(clazz); Objects.requireNonNull(s); return (Stream<O>) s.filter(clazz::isInstance); }
From source file:com.ikanow.aleph2.core.shared.utils.TimeSliceDirUtils.java
/** Filters out non matching directories * @param in/*from w w w.ja va 2 s .c o m*/ * @param filter * @return */ public static Stream<String> filterTimedDirectories(final Stream<Tuple3<String, Date, Date>> in, final Tuple2<Optional<Date>, Optional<Date>> filter) { return in.filter(t3 -> filter._1().map(tmin -> { //lower bound return tmin.getTime() < t3._3().getTime(); // just has to be smaller than the largest time in the group }).orElse(true)).filter(t3 -> filter._2().map(tmax -> { //lower bound return tmax.getTime() >= t3._2().getTime(); // just has to be larger than the smallest time in the group }).orElse(true)).map(t3 -> t3._1()); }
From source file:edumsg.core.PostgresConnection.java
public static void readConfFile() throws Exception { String file = System.getProperty("user.dir") + "/Postgres.conf"; java.util.List<String> lines = new ArrayList<String>(); //extract string between square brackets and compile regex for faster performance Pattern pattern = Pattern.compile("\\[(.+)\\]"); Matcher matcher;/* ww w. jav a2s.c o m*/ Exception e; Stream<String> stream = Files.lines(Paths.get(file)); lines = stream.filter(line -> !line.startsWith("#")).collect(Collectors.toList()); //set variables based on matches for (int i = 0; i < lines.size(); i++) { if (lines.get(i).contains("user")) { matcher = pattern.matcher(lines.get(i)); if (matcher.find()) setDBUser(matcher.group(1)); else throw e = new Exception("empty user in Postgres.conf"); } if (lines.get(i).contains("database")) { matcher = pattern.matcher(lines.get(i)); if (matcher.find()) setDBName(matcher.group(1)); else throw e = new Exception("empty database name in Postgres.conf"); } if (lines.get(i).contains("pass")) { matcher = pattern.matcher(lines.get(i)); matcher.find(); setDBPassword(matcher.group(1)); } if (lines.get(i).contains("host")) { matcher = pattern.matcher(lines.get(i)); if (matcher.find()) setDBHost(matcher.group(1)); else setDBHost("localhost"); } if (lines.get(i).contains("port")) { matcher = pattern.matcher(lines.get(i)); if (matcher.find()) setDBPort(matcher.group(1)); else setDBPort("5432"); } } if (!formatURL()) { e = new Exception("Wrong Format in Postgres.conf"); throw e; } }
From source file:io.promagent.internal.HookMetadataParser.java
/** * Convert class file paths to class names and add them to result. */// w w w .j a va2s.c o m private static void addClassNames(Stream<String> paths, Collection<String> result, Predicate<String> classNameFilter) { paths.filter(name -> name.endsWith(".class")) .map(name -> name.substring(0, name.length() - ".class".length())) .map(name -> name.startsWith("/") ? name.substring(1) : name).map(name -> name.replace("/", ".")) .filter(classNameFilter).collect(Collectors.toCollection(() -> result)); }
From source file:com.github.robozonky.app.events.SessionEvents.java
@SuppressWarnings("unchecked") static <T extends Event> Class<T> getImplementingEvent(final Class<T> original) { final Stream<Class<?>> provided = ClassUtils.getAllInterfaces(original).stream(); final Stream<Class<?>> interfaces = original.isInterface() ? // interface could be extending it directly Stream.concat(Stream.of(original), provided) : provided; final String apiPackage = "com.github.robozonky.api.notifications"; return (Class<T>) interfaces.filter(i -> Objects.equals(i.getPackage().getName(), apiPackage)) .filter(i -> i.getSimpleName().endsWith("Event")) .filter(i -> !Objects.equals(i.getSimpleName(), "Event")).findFirst() .orElseThrow(() -> new IllegalStateException("Not an event:" + original)); }
From source file:de.fosd.jdime.Main.java
/** * Returns a <code>File</code> (possibly <code>f</code>) that does not exist in the parent directory of * <code>f</code>. If <code>f</code> exists an increasing number is appended to the name of <code>f</code> until * a <code>File</code> is found that does not exist. * * @param f//from w w w . j ava 2 s.c om * the <code>File</code> to find a non existent version of * @return a <code>File</code> in the parent directory of <code>f</code> that does not exist */ private static File findNonExistent(File f) { if (!f.exists()) { return f; } String fullName = f.getName(); String name; String extension; int pos = fullName.lastIndexOf('.'); if (pos != -1) { name = fullName.substring(0, pos); extension = fullName.substring(pos, fullName.length()); } else { name = fullName; extension = ""; } File parent = f.getParentFile(); Stream<File> files = IntStream.range(0, Integer.MAX_VALUE).mapToObj(v -> { String fileName = String.format("%s_%d%s", name, v, extension); return new File(parent, fileName); }); File nextFree = files.filter(file -> !file.exists()).findFirst() .orElseThrow(() -> new RuntimeException("Can not find a file that does not exist.")); return nextFree; }