Java 8 forEach print with Index

A simple Java 8 tip to print the Array or List with index in the front. 1. Array with Index Generate the index with IntStream.range. JavaListWithIndex.java package com.mkyong.java8; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; public class JavaArrayWithIndex { public static void main(String[] args) { String[] names = {"Java", "Node", "JavaScript", "Rust", "Go"}; List<String> collect = …

Read more

Java 8 BiConsumer Examples

In Java 8, BiConsumer is a functional interface; it takes two arguments and returns nothing. @FunctionalInterface public interface BiConsumer<T, U> { void accept(T t, U u); } Further Reading – Java 8 Consumer Examples 1. BiConsumer JavaBiConsumer1.java package com.mkyong.java8; import java.util.function.Consumer; public class JavaBiConsumer1 { public static void main(String[] args) { BiConsumer<Integer, Integer> addTwo = …

Read more

Java 8 Consumer Examples

In Java 8, Consumer is a functional interface; it takes an argument and returns nothing. @FunctionalInterface public interface Consumer<T> { void accept(T t); } 1. Consumer Java8Consumer1.java package com.mkyong.java8; import java.util.function.Consumer; public class Java8Consumer1 { public static void main(String[] args) { Consumer<String> print = x -> System.out.println(x); print.accept("java"); // java } } Output java 2. …

Read more