Collectors.toSet() and HashSet
If you want a guaranteed HashSet, use Collectors.toCollection(HashSet::new).
If you want a guaranteed HashSet, use Collectors.toCollection(HashSet::new).
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.
You can do int sum = lst.stream().filter(o -> o.getField() > 10).mapToInt(o -> o.getField()).sum(); or (using Method reference) int sum = lst.stream().filter(o -> o.getField() > 10).mapToInt(Obj::getField).sum();
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
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
You may do it like so: String s = Stream.of(str1, str2, str3) .filter(Objects::nonNull) .findFirst() .orElse(str4);
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
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
This is known as vacuous truth. All members of an empty collection satisfy your condition; after all, can you point to one that doesn’t? Similarly, anyMatch returns false, because you can’t find an element of your collection that does match the condition. This is confusing to a lot of people, but it turns out to … Read more
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