Close Java 8 Stream

It is generally not necessary to close streams at all. You only need to close streams that use IO resources. From the Stream documentation: Streams have a BaseStream.close() method and implement AutoCloseable, but nearly all stream instances do not actually need to be closed after use. Generally, only streams whose source is an IO channel … Read more

Does Java 8 lack a Stream.concat working on a varags of streams?

Just flatMap it: public static void main(final String[] args) throws Exception { final Stream<String> stream1 = /*some stream*/ final Stream<String> stream2 = /*some stream*/ final Stream<String> stream3 = /*some stream*/ final Stream<String> stream4 = /*some stream*/ final Stream<String> stream5 = /*some stream*/ final Stream<String> stream = Stream.of(stream1, stream2, stream3, stream4, stream5).flatMap(Function.identity()); } In your example: … Read more

Transform and filter a Java Map with streams

Yes, you can map each entry to another temporary entry that will hold the key and the parsed integer value. Then you can filter each entry based on their value. Map<String, Integer> output = input.entrySet() .stream() .map(e -> new AbstractMap.SimpleEntry<>(e.getKey(), Integer.valueOf(e.getValue()))) .filter(e -> e.getValue() % 2 == 0) .collect(Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue )); Note that I … Read more

Grouping by and map value

You need to chain a Collectors.mapping() collector to the Collectors.groupingBy() collector: Map<String, List<Fizz>> collect = docs.stream() .collect(Collectors.groupingBy(DistrictDocument::getCity, Collectors.mapping(d->createFizz(d),Collectors.toList()))); If createFizz(d) would return a List<Fizz, you can flatten it using Java 9’s Collectors.flatMapping: Map<String, List<Fizz>> collect = docs.stream() .collect(Collectors.groupingBy(DistrictDocument::getCity, Collectors.flatMapping(d->createFizz(d).stream(),Collectors.toList()))); If you can’t use Java 9, perhaps using Collectors.toMap will help: Map<String, List<Fizz>> collect = docs.stream() … Read more

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