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) { MapwordLengthMap = 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); } }
No comments:
Post a Comment