This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.stream.*; | |
| class Main { | |
| public static void main(String[] args) | |
| { | |
| // Get the stream | |
| Stream<String> stream | |
| = Stream.of("Geeks", "For", | |
| "Geeks", "A", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var wrapper = new Object(){ int listincrement = 0; }; | |
| list.forEach(s -> { | |
| s.setOrdinal(wrapper.ordinal++); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| AtomicInteger listincrement = new AtomicInteger(0); | |
| list.forEach(s -> { | |
| s.setOrdinal(listincrement.getAndIncrement()); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| List<String> list = Arrays.asList("one", "two", "three"); | |
| final int listincrement = 1; | |
| list.forEach(s -> { | |
| listincrement++;//error here | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| List<String> list = Arrays.asList("one", "two", "three"); | |
| int listincrement = 1; | |
| list.forEach(s -> { | |
| listincrement++; | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.function.Consumer; | |
| public class LambdaScopeTest { | |
| public int x = 0; | |
| class FirstLevel { | |
| public int x = 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.behindjava.java8; | |
| import java.util.ArrayList; | |
| import java.util.Arrays; | |
| import java.util.List; | |
| import java.util.function.Function; | |
| public class FunctionTRExample{ | |
| public static void main(String args[]){ | |
| Function<Employee, String> funcEmpToString= (Employee e)-> {return e.getName();}; | |
| List<Employee> employeeList= | |
| Arrays.asList(new Employee("Tom Jones", 45), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @FunctionalInterface | |
| public interface Function<T, R> { | |
| R apply(T t); | |
| default <V> Function<V, R> compose(Function<? super V, ? extends T> before) { | |
| Objects.requireNonNull(before); | |
| return (V v) -> apply(before.apply(v)); | |
| } | |
| default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) { | |
| Objects.requireNonNull(after); | |
| return (T t) -> after.apply(apply(t)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.behindjava.tutorial; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import java.util.stream.Stream; | |
| /** | |
| * @author Anish Antony | |
| */ | |
| public class StreamLimitSkipExample { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| String[] names = {"Sam", "Pamela", "Dave", "Pascal", "Erik"}; | |
| List<String> nameList = | |
| IntStream.range(0, names.length) | |
| .filter(i -> names[i].length() <= i) | |
| .mapToObj(i -> names[i]) | |
| .collect(toList()); |