import java.util.*; import java.util.function.*; import java.util.stream.*; public class SideEffects { int y; //y is not local so effectively final does not apply Function<Integer,Integer> G =(n) ->{return n+y;}; Function<Integer,Integer> F= (n) ->{y++;return n*n;}; class justanInt { public int anint; public justanInt(int t){ anint=t; } } public static void main(String[] args){ new SideEffects(); } public SideEffects(){ y=5; System.out.println("\nThe Usual Example:"); System.out.println(g(2)+f(1)); y=5; System.out.println(f(1)+g(2)); System.out.println("\nAs Lambdas:"); y=5; System.out.println(G.apply(2)+F.apply(1)); y=5; System.out.println(F.apply(1)+G.apply(2)); System.out.println("\nThe Stream Output:"); List<justanInt> lst = new ArrayList<>(); List<justanInt> lst2 = new ArrayList<>(); justanInt j1=new justanInt(2); justanInt j2=new justanInt(5); lst.add(j1);lst.add(j2); lst2=lst; lst.stream() //Think of as stream of pointers! .map((s)->{ s.anint=s.anint*s.anint; j2.anint++; return s; }) .forEach((s)->System.out.println(s.anint)); System.out.println("\nThe Present value of j2.anint is: "+j2.anint); System.out.println("\nnow lst2"); lst2.stream() .forEach((s)->System.out.println(s.anint)); } public int g(int x){ return x+y; } public int f(int x){ y++; return x*x; } }
: