How should we manage jdk8 stream for null values

Although the answers are 100% correct, a small suggestion to improve null case handling of the list itself with Optional: List<String> listOfStuffFiltered = Optional.ofNullable(listOfStuff) .orElseGet(Collections::emptyList) .stream() .filter(Objects::nonNull) .collect(Collectors.toList()); The part Optional.ofNullable(listOfStuff).orElseGet(Collections::emptyList) will allow you to handle nicely the case when listOfStuff is null and return an emptyList instead of failing with NullPointerException.

Java 8 Stream API to find Unique Object matching a property value

Instead of using a collector try using findFirst or findAny. Optional<Person> matchingObject = objects.stream(). filter(p -> p.email().equals(“testemail”)). findFirst(); This returns an Optional since the list might not contain that object. If you’re sure that the list always contains that person you can call: Person person = matchingObject.get(); Be careful though! get throws NoSuchElementException if no … Read more

How do I keep the iteration order of a List when using Collections.toMap() on a stream?

The 2-parameter version of Collectors.toMap() uses a HashMap: public static <T, K, U> Collector<T, ?, Map<K,U>> toMap( Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper) { return toMap(keyMapper, valueMapper, throwingMerger(), HashMap::new); } To use the 4-parameter version, you can replace: Collectors.toMap(Function.identity(), String::length) with: Collectors.toMap( Function.identity(), String::length, (u, v) -> … Read more

forEach vs forEachOrdered in Java 8 Stream

Stream.of(“AAA”,”BBB”,”CCC”).parallel().forEach(s->System.out.println(“Output:”+s)); Stream.of(“AAA”,”BBB”,”CCC”).parallel().forEachOrdered(s->System.out.println(“Output:”+s)); The second line will always output Output:AAA Output:BBB Output:CCC whereas the first one is not guaranted since the order is not kept. forEachOrdered will processes the elements of the stream in the order specified by its source, regardless of whether the stream is sequential or parallel. Quoting from forEach Javadoc: The behavior of … Read more

Check instanceof in stream

You can apply another filter in order to keep only the ScheduleIntervalContainer instances, and adding a map will save you the later casts : scheduleIntervalContainers.stream() .filter(sc -> sc instanceof ScheduleIntervalContainer) .map (sc -> (ScheduleIntervalContainer) sc) .filter(sic -> sic.getStartTime() != sic.getEndTime()) .collect(Collectors.toList()); Or, as Holger commented, you can replace the lambda expressions with method references if … Read more

Java 8: preferred way to count iterations of a lambda?

Let me reformat your example a bit for the sake of discussion: long runCount = 0L; myStream.stream() .filter(…) .forEach(item -> { foo(); bar(); runCount++; // doesn’t work }); System.out.println(“The lambda ran ” + runCount + ” times”); If you really need to increment a counter from within a lambda, the typical way to do so … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)