Consumer with more than one argument in Java 8?

For 3 and more arguments you could use curried(http://en.wikipedia.org/wiki/Currying) functions with last consumer: Function<Double, Function<Integer, Consumer<String>>> f = d -> i -> s -> { System.out.println(“” + d+ “;” + i+ “;” + s); }; f.apply(1.0).apply(2).accept(“s”); Output is: 1.0;2;s It’s enough to have a function of one argument to express function of any number of … Read more

How do I get an IntStream from a List?

I guess (or at least it is an alternative) this way is more performant: public static IntStream baz(List<Integer> list) { return list.stream().mapToInt(Integer::intValue); } since the function Integer::intValue is fully compatible with ToIntFunction since it takes an Integer and it returns an int. No autoboxing is performed. I was also looking for an equivalent of Function::identity, … Read more

Java 8 toMap IllegalStateException Duplicate Key

The pramodh’s answer is good if you want to map your value to 1. But in case you don’t want to always map to a constant, the use of the “merge-function” might help: Map<Integer, Integer> map1 = Files.lines(Paths.get(inputFile)) .map(line::trim()) .map(Integer::valueOf) .collect(Collectors.toMap(x -> x, x -> 1, (x1, x2) -> x1)); The above code is almost … Read more

Stream and lazy evaluation

It means that the filter is only applied during the terminal operation. Think of something like this: public Stream filter(Predicate p) { this.filter = p; // just store it, don’t apply it yet return this; // in reality: return a new stream } public List collect() { for (Object o : stream) { if (filter.test(o)) … Read more

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