Thursday, 12 April 2018

How to reduce a stream in to a specific map in Java 8

Using Collectors.toMap to collect stream elements into a map. You have to fill up four arguments.

the first argument: key mapper;
the second argument: value mapper;
the 3rd argument: binary operator -- merge function.
the 4th argument: supplier -- a specific map construction here



package StreamCollectingResult;

import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 *
 * @author YNZ
 */
public class CollectingToSpecificMap {

    public static void main(String[] args) {

        Map wordLengthMap = Pattern.compile("[\\s]")
                .splitAsStream("then you need to provide 4th argument")
                .collect(Collectors.toMap(w -> w, String::length, (o, n) -> {
                    throw new IllegalStateException();
                }, TreeMap::new));

        System.out.println("" + wordLengthMap);

    }

}


Can Jackson Deserialize Java Time ZonedDateTime

Yes, but must include JSR310. Thus ZonedDateTime can be deserialized directly from JSON response to POJO field. <dependency> <g...