diff --git a/java-8/src/com/learnJavaMyVersion/constructorreference/ConstructorReference.java b/java-8/src/com/learnJavaMyVersion/constructorreference/ConstructorReference.java new file mode 100644 index 0000000..12aef31 --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/constructorreference/ConstructorReference.java @@ -0,0 +1,15 @@ +package com.learnJavaMyVersion.constructorreference; + +import com.learnJava.data.Student; + +import java.util.function.Function; +import java.util.function.Supplier; + +public class ConstructorReference { + static Supplier studentSupplier = Student::new; + static Function studentFunction = Student::new; + public static void main(String[] args) { + System.out.println(studentSupplier.get()); + System.out.println(studentFunction.apply("ABC")); + } +} diff --git a/java-8/src/com/learnJavaMyVersion/functionalInterfaces/BiConsumerExample.java b/java-8/src/com/learnJavaMyVersion/functionalInterfaces/BiConsumerExample.java new file mode 100644 index 0000000..cce6104 --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/functionalInterfaces/BiConsumerExample.java @@ -0,0 +1,34 @@ +package com.learnJavaMyVersion.functionalInterfaces; + +import com.learnJava.data.Student; +import com.learnJava.data.StudentDataBase; + +import java.util.List; +import java.util.function.BiConsumer; + +public class BiConsumerExample { + + public static void nameAndActivities(){ + System.out.println("****** Inside nameAndActivities() method **********"); + BiConsumer> biConsumer = (name,activities) -> System.out.println(name +" : "+ activities); + List studentList = StudentDataBase.getAllStudents(); + studentList.forEach((student) -> biConsumer.accept(student.getName(),student.getActivities())); + } + + public static void main(String[] args) { + BiConsumer biConsumer = (a,b) ->{ + System.out.println("a: "+ a +", b: "+ b); + }; + BiConsumer multiplication = (a,b) ->{ + System.out.println("Multiplication: "+ a*b); + }; + BiConsumer division = (a,b) ->{ + System.out.println("division: "+ a/b); + }; + + biConsumer.accept("Java7","Java8"); + multiplication.andThen(division).accept(10,5); + + nameAndActivities(); + } +} diff --git a/java-8/src/com/learnJavaMyVersion/functionalInterfaces/BiFunctionExample.java b/java-8/src/com/learnJavaMyVersion/functionalInterfaces/BiFunctionExample.java new file mode 100644 index 0000000..77ca45e --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/functionalInterfaces/BiFunctionExample.java @@ -0,0 +1,27 @@ +package com.learnJavaMyVersion.functionalInterfaces; + +import com.learnJava.data.Student; +import com.learnJava.data.StudentDataBase; +import com.oracle.tools.packager.mac.MacAppBundler; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.BiFunction; +import java.util.function.Predicate; + +public class BiFunctionExample { + + static BiFunction, Predicate, Map> biFunction =((studentList, studentPredicate) -> { + Map studentGpaMap = new HashMap<>(); + studentList.forEach(student -> { + if (studentPredicate.test(student)){ + studentGpaMap.put(student.getName(),student.getGpa()); + } + }); + return studentGpaMap; + }); + public static void main(String[] args) { + System.out.println(biFunction.apply(StudentDataBase.getAllStudents(),PredicateStudentExample.predicate)); + } +} diff --git a/java-8/src/com/learnJavaMyVersion/functionalInterfaces/BinaryOperatorExample.java b/java-8/src/com/learnJavaMyVersion/functionalInterfaces/BinaryOperatorExample.java new file mode 100644 index 0000000..51e4a55 --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/functionalInterfaces/BinaryOperatorExample.java @@ -0,0 +1,19 @@ +package com.learnJavaMyVersion.functionalInterfaces; + +import java.util.Comparator; +import java.util.function.BinaryOperator; + +public class BinaryOperatorExample { + + static Comparator comparator = (a,b) -> a.compareTo(b); + public static void main(String[] args) { + BinaryOperator binaryOperator = (a, b) -> a*b; + + System.out.println(binaryOperator.apply(3,4)); + + BinaryOperator maxBy = BinaryOperator.maxBy(comparator); + System.out.println("Result of Max by is : " + maxBy.apply(3,4)); + BinaryOperator minBy = BinaryOperator.minBy(comparator); + System.out.println("Result of Min by is : " + minBy.apply(3,4)); + } +} diff --git a/java-8/src/com/learnJavaMyVersion/functionalInterfaces/ConsumerExample.java b/java-8/src/com/learnJavaMyVersion/functionalInterfaces/ConsumerExample.java new file mode 100644 index 0000000..556d106 --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/functionalInterfaces/ConsumerExample.java @@ -0,0 +1,42 @@ +package com.learnJavaMyVersion.functionalInterfaces; + +import com.learnJava.data.Student; +import com.learnJava.data.StudentDataBase; + +import java.util.List; +import java.util.function.Consumer; + +public class ConsumerExample { + static Consumer c1 = (s )-> System.out.println(s.toUpperCase()); + static Consumer c2 = (student) -> System.out.println(student); + static Consumer c3 = (student) -> System.out.println(student.getName()); + static Consumer c4 = (student) -> System.out.println(student.getActivities()); + public static void printName(){ + System.out.println("********Inside printName() method *****************"); + List studentList = StudentDataBase.getAllStudents(); + studentList.forEach(c2); + } + public static void printNameAndActivities(){ + System.out.println("********Inside printNameAndActivities() method *****************"); + List studentList = StudentDataBase.getAllStudents(); + studentList.forEach(c3.andThen(c4)); + } + + public static void printNameAndActivitiesWtihCondition(){ + System.out.println("********Inside printNameAndActivitiesWtihCondition() method *****************"); + List studentList = StudentDataBase.getAllStudents(); + studentList.forEach(((student) ->{ + if(student.getGradeLevel() >= 3 && student.getGpa() >= 3.9) + c3.andThen(c4).accept(student); + } )); + } + public static void main(String[] args) { + + + + c1.accept("Tiedros"); + printName(); + printNameAndActivities(); + printNameAndActivitiesWtihCondition(); + } +} diff --git a/java-8/src/com/learnJavaMyVersion/functionalInterfaces/FunctionExample.java b/java-8/src/com/learnJavaMyVersion/functionalInterfaces/FunctionExample.java new file mode 100644 index 0000000..01924fe --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/functionalInterfaces/FunctionExample.java @@ -0,0 +1,14 @@ +package com.learnJavaMyVersion.functionalInterfaces; + +import java.util.function.Function; + +public class FunctionExample { + + static Function function = (name)-> name.toUpperCase(); + static Function addSomething = (name)-> name.toUpperCase().concat("default"); + public static void main(String[] args) { + System.out.println("The Result is : "+ function.apply("java8")); + System.out.println("The Result addThen is : "+ function.andThen(addSomething).apply("java8")); + System.out.println("The Result addThen is : "+ function.compose(addSomething).apply("java8")); + } +} diff --git a/java-8/src/com/learnJavaMyVersion/functionalInterfaces/FunctionStudentExample.java b/java-8/src/com/learnJavaMyVersion/functionalInterfaces/FunctionStudentExample.java new file mode 100644 index 0000000..056dfe3 --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/functionalInterfaces/FunctionStudentExample.java @@ -0,0 +1,27 @@ +package com.learnJavaMyVersion.functionalInterfaces; + +import com.learnJava.data.Student; +import com.learnJava.data.StudentDataBase; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Function; + +public class FunctionStudentExample { + + static Function, Map> studentFunction = (students -> { + Map studentGrademap = new HashMap<>(); + students.forEach((student -> { + if(PredicateStudentExample.predicate.test(student)){ + studentGrademap.put(student.getName(), student.getGpa()); + } + })); + return studentGrademap; + } ); + + public static void main(String[] args) { + + System.out.println(studentFunction.apply(StudentDataBase.getAllStudents())); + } +} diff --git a/java-8/src/com/learnJavaMyVersion/functionalInterfaces/PredicateAndConsumerExample.java b/java-8/src/com/learnJavaMyVersion/functionalInterfaces/PredicateAndConsumerExample.java new file mode 100644 index 0000000..ef25d2a --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/functionalInterfaces/PredicateAndConsumerExample.java @@ -0,0 +1,33 @@ +package com.learnJavaMyVersion.functionalInterfaces; + +import com.learnJava.data.Student; +import com.learnJava.data.StudentDataBase; + +import java.util.List; +import java.util.function.BiConsumer; +import java.util.function.BiPredicate; +import java.util.function.Consumer; +import java.util.function.Predicate; + +public class PredicateAndConsumerExample { + + Predicate predicate1 = (student) -> student.getGradeLevel() >= 3; + Predicate getPredicate2 = (student) -> student.getGpa() >= 3.9; + BiPredicate biPredicate = (gradeLevel,gpa)-> gradeLevel >= 3 && gpa >= 3.9; + BiConsumer> studentBiConsumer = (name,activities) -> System.out.println(name +" : "+ activities); + Consumer studentConsumer = (student -> { + // if(predicate1.and(getPredicate2).test(student)){ //using Predicate chaining + if(biPredicate.test(student.getGradeLevel(),student.getGpa())){ // using biPredicate + studentBiConsumer.accept(student.getName(),student.getActivities()); + } + }); + + public void printNameAndActivities(List studentList){ + System.out.println("****************InSide printNameAndActivities() method******************"); + studentList.forEach(studentConsumer); + } + public static void main(String[] args) { + List studentList = StudentDataBase.getAllStudents(); + new PredicateAndConsumerExample().printNameAndActivities(studentList); + } +} diff --git a/java-8/src/com/learnJavaMyVersion/functionalInterfaces/PredicateExample.java b/java-8/src/com/learnJavaMyVersion/functionalInterfaces/PredicateExample.java new file mode 100644 index 0000000..3102c0e --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/functionalInterfaces/PredicateExample.java @@ -0,0 +1,41 @@ +package com.learnJavaMyVersion.functionalInterfaces; + +import java.util.function.Predicate; + +public class PredicateExample { + static Predicate predicate = (a)-> { return a%2==0;}; + static Predicate predicate2 = (a)-> a%2==0; + static Predicate predicate3 = (a)-> a%5==0; + + public static void predicateAnd(){ + System.out.println("********Inside predicateAnd() method ***********"); + System.out.println(predicate2.and(predicate3).test(10)); + System.out.println(predicate2.and(predicate3).test(8)); + System.out.println(predicate2.and(predicate3).test(9)); + } + + public static void predicateOr(){ + System.out.println("********Inside predicateOr() method ***********"); + System.out.println(predicate2.or(predicate3).test(10)); + System.out.println(predicate2.or(predicate3).test(8)); + System.out.println(predicate2.or(predicate3).test(9)); + } + + public static void predicateNegate(){ + System.out.println("********Inside predicateNegate() method ***********"); + System.out.println(predicate2.or(predicate3).negate().test(10)); + System.out.println(predicate2.or(predicate3).negate().test(8)); + System.out.println(predicate2.or(predicate3).negate().test(9)); + } + public static void main(String[] args) { + + + + System.out.println("Is 4 even number? : "+ predicate.test(4)); + System.out.println("Is 4 even number? : "+ predicate2.test(4)); + + predicateAnd(); + predicateOr(); + predicateNegate(); + } +} diff --git a/java-8/src/com/learnJavaMyVersion/functionalInterfaces/PredicateStudentExample.java b/java-8/src/com/learnJavaMyVersion/functionalInterfaces/PredicateStudentExample.java new file mode 100644 index 0000000..074d202 --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/functionalInterfaces/PredicateStudentExample.java @@ -0,0 +1,45 @@ +package com.learnJavaMyVersion.functionalInterfaces; + +import com.learnJava.data.Student; +import com.learnJava.data.StudentDataBase; + +import java.util.List; +import java.util.function.Predicate; + +public class PredicateStudentExample { + static Predicate predicate = (student) -> student.getGradeLevel() >=3; + static Predicate predicate2 = (student) -> student.getGpa() >=3.9; + public static void filterStudentByGradeLevel(){ + System.out.println("***********Inside filterStudentByGradeLevel() method*****************"); + List studentList = StudentDataBase.getAllStudents(); + studentList.forEach((student) ->{ + if(predicate.test(student)){ + System.out.println(student); + } + }); + } + public static void filterStudentByGpa(){ + System.out.println("***********Inside filterStudentByGpa() method*****************"); + List studentList = StudentDataBase.getAllStudents(); + studentList.forEach((student) ->{ + if(predicate2.test(student)){ + System.out.println(student); + } + }); + } + + public static void filterStudents(){ + System.out.println("***********Inside filterStudents() method*****************"); + List studentList = StudentDataBase.getAllStudents(); + studentList.forEach((student) ->{ + if(predicate.and(predicate2).test(student)){ + System.out.println(student); + } + }); + } + public static void main(String[] args) { + filterStudentByGradeLevel(); + filterStudentByGpa(); + filterStudents(); + } +} diff --git a/java-8/src/com/learnJavaMyVersion/functionalInterfaces/SupplierExample.java b/java-8/src/com/learnJavaMyVersion/functionalInterfaces/SupplierExample.java new file mode 100644 index 0000000..55e45eb --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/functionalInterfaces/SupplierExample.java @@ -0,0 +1,21 @@ +package com.learnJavaMyVersion.functionalInterfaces; + +import com.learnJava.data.Student; +import com.learnJava.data.StudentDataBase; + +import java.util.Arrays; +import java.util.List; +import java.util.function.Supplier; + +public class SupplierExample { + public static void main(String[] args) { + Supplier studentSupplier = () -> { + return new Student("Adam",2,3.6,"male", Arrays.asList("swimming","basketball","vollyeball")); + }; + + Supplier> listSupplier = () -> StudentDataBase.getAllStudents(); + + System.out.println("Student is : "+studentSupplier.get()); + System.out.println("Students are : "+ listSupplier.get()); + } +} diff --git a/java-8/src/com/learnJavaMyVersion/functionalInterfaces/UnaryOperatorExample.java b/java-8/src/com/learnJavaMyVersion/functionalInterfaces/UnaryOperatorExample.java new file mode 100644 index 0000000..4750941 --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/functionalInterfaces/UnaryOperatorExample.java @@ -0,0 +1,12 @@ +package com.learnJavaMyVersion.functionalInterfaces; + +import java.util.function.UnaryOperator; + +public class UnaryOperatorExample { + + static UnaryOperator unaryOperator = (s) -> s.concat("Default"); + public static void main(String[] args) { + System.out.println(unaryOperator.apply("Java8 ")); + + } +} diff --git a/java-8/src/com/learnJavaMyVersion/imperativeVsDeclarativePrograming/ImperativeVsDeclarativeExample1.java b/java-8/src/com/learnJavaMyVersion/imperativeVsDeclarativePrograming/ImperativeVsDeclarativeExample1.java new file mode 100644 index 0000000..f65277c --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/imperativeVsDeclarativePrograming/ImperativeVsDeclarativeExample1.java @@ -0,0 +1,28 @@ +package com.learnJavaMyVersion.imperativeVsDeclarativePrograming; + +import java.util.stream.IntStream; + +public class ImperativeVsDeclarativeExample1 { + + public static void main(String[] args) { + + /* + * Imperative - How style programming + */ + + int sum =0; + for(int i=0;i<=100;i++){ + sum+=i; + } + System.out.println("Sum using imperative Approach is : "+ sum); + + /* + * Declarative - What style programming + * + * */ + + int sum1 = IntStream.rangeClosed(0, 100) + .sum(); + System.out.println("Sum using declarative Approach is : "+ sum1); + } +} diff --git a/java-8/src/com/learnJavaMyVersion/imperativeVsDeclarativePrograming/ImperativeVsDeclarativeExample2.java b/java-8/src/com/learnJavaMyVersion/imperativeVsDeclarativePrograming/ImperativeVsDeclarativeExample2.java new file mode 100644 index 0000000..595bd39 --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/imperativeVsDeclarativePrograming/ImperativeVsDeclarativeExample2.java @@ -0,0 +1,35 @@ +package com.learnJavaMyVersion.imperativeVsDeclarativePrograming; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class ImperativeVsDeclarativeExample2 { + public static void main(String[] args) { + List integersList = Arrays.asList(1,2,3,3,4,5,5,6,7,8,8,9,10); + + /* + * Imperative + * */ + List uniqueList = new ArrayList<>(); + for (Integer integer : integersList){ + if(!uniqueList.contains(integer)){ + uniqueList.add(integer); + } + } + System.out.println("Imperative approach to remove duplicate is : "+uniqueList); + + /* + * Declarative + * */ + + List uniqueList1 = integersList.stream() + .distinct() + .collect(Collectors.toList()); + System.out.println("Declarative Approach to remove duplicate is : "+ uniqueList1); + + + } +} diff --git a/java-8/src/com/learnJavaMyVersion/lambdas/ComparatorLambdaExample.java b/java-8/src/com/learnJavaMyVersion/lambdas/ComparatorLambdaExample.java new file mode 100644 index 0000000..c2f0193 --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/lambdas/ComparatorLambdaExample.java @@ -0,0 +1,27 @@ +package com.learnJavaMyVersion.lambdas; + +import java.util.Comparator; + +public class ComparatorLambdaExample { + public static void main(String[] args) { + + + /* + * Prior to Java 8 + * */ + + Comparator comparator = new Comparator() { + @Override + public int compare(Integer o1, Integer o2) { + return o1.compareTo(o2); + } + }; + System.out.println("The Result of Comparator is : "+ comparator.compare(3,2)); + + /* + * Java 8 + * */ + Comparator comparatorLambda = ( a, b) -> a.compareTo(b); + System.out.println("The Result of Comparator using Lambdas is : "+ comparatorLambda.compare(3,2)); + } +} diff --git a/java-8/src/com/learnJavaMyVersion/lambdas/LambdaVariable1.java b/java-8/src/com/learnJavaMyVersion/lambdas/LambdaVariable1.java new file mode 100644 index 0000000..ed728ba --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/lambdas/LambdaVariable1.java @@ -0,0 +1,13 @@ +package com.learnJavaMyVersion.lambdas; + +import java.util.function.Consumer; + +public class LambdaVariable1 { + public static void main(String[] args) { + int i=0; + Consumer consumer =(i1) -> { // not possible to use variable i in the parameter value + // int i=2; -> not possible + System.out.println("Value is : "+ i1); + }; + } +} diff --git a/java-8/src/com/learnJavaMyVersion/lambdas/LambdaVariable2.java b/java-8/src/com/learnJavaMyVersion/lambdas/LambdaVariable2.java new file mode 100644 index 0000000..4affc50 --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/lambdas/LambdaVariable2.java @@ -0,0 +1,20 @@ +package com.learnJavaMyVersion.lambdas; + +import java.util.function.Consumer; + +public class LambdaVariable2 { + static int anotherValue=10; + public static void main(String[] args) { + int value = 4; //local variable + Consumer consumer =(i) -> { + // value++; not possible + anotherValue ++; // this possible + System.out.println(value + i); + System.out.println(anotherValue + i); + }; + // value =8; // not possible + anotherValue=11; // possible + consumer.accept(4); + + } +} diff --git a/java-8/src/com/learnJavaMyVersion/lambdas/RunableLambdaExample.java b/java-8/src/com/learnJavaMyVersion/lambdas/RunableLambdaExample.java new file mode 100644 index 0000000..aa7755a --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/lambdas/RunableLambdaExample.java @@ -0,0 +1,38 @@ +package com.learnJavaMyVersion.lambdas; + +public class RunableLambdaExample { + public static void main(String[] args) { + + + /* + * Prior java 8 + * */ + + Runnable runnable = new Runnable() { + @Override + public void run() { + System.out.println("Inside Runnable 1"); + } + }; + + new Thread(runnable).start(); + + /* + * Java 8 lambda syntax + * + * */ + // () -> {} + Runnable runnableLambda = () -> { + System.out.println("Inside Runnable 2"); + }; + + new Thread(runnableLambda).start(); + + // + Runnable runnableLambda1 = () -> System.out.println("Inside Runnable 3"); + new Thread(runnableLambda1).start(); + + // + new Thread(() -> System.out.println("Inside Runnable 4")).start(); + } +} diff --git a/java-8/src/com/learnJavaMyVersion/methodreference/ConsumerMethodReferenceExample.java b/java-8/src/com/learnJavaMyVersion/methodreference/ConsumerMethodReferenceExample.java new file mode 100644 index 0000000..f25b363 --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/methodreference/ConsumerMethodReferenceExample.java @@ -0,0 +1,17 @@ +package com.learnJavaMyVersion.methodreference; + +import com.learnJava.data.Student; +import com.learnJava.data.StudentDataBase; + +import java.util.function.Consumer; + +public class ConsumerMethodReferenceExample { + + static Consumer consumer = System.out::println; + static Consumer consumer2 = Student::printListOfActivities; + public static void main(String[] args) { + StudentDataBase.getAllStudents().forEach(consumer); + StudentDataBase.getAllStudents().forEach(consumer2); + + } +} diff --git a/java-8/src/com/learnJavaMyVersion/methodreference/FunctionMethodReferenceExample.java b/java-8/src/com/learnJavaMyVersion/methodreference/FunctionMethodReferenceExample.java new file mode 100644 index 0000000..fb20f4a --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/methodreference/FunctionMethodReferenceExample.java @@ -0,0 +1,16 @@ +package com.learnJavaMyVersion.methodreference; + +import java.util.function.Function; + +public class FunctionMethodReferenceExample { + + static Function toUpperCaseLambda = (s)->s.toUpperCase(); + + static Function toUpperMethodReference = String::toUpperCase; + + public static void main(String[] args) { + System.out.println(toUpperCaseLambda.apply("java8")); + System.out.println(toUpperMethodReference.apply("java8")); + + } +} diff --git a/java-8/src/com/learnJavaMyVersion/methodreference/RefactorMethodReferenceExample.java b/java-8/src/com/learnJavaMyVersion/methodreference/RefactorMethodReferenceExample.java new file mode 100644 index 0000000..b8ed7af --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/methodreference/RefactorMethodReferenceExample.java @@ -0,0 +1,19 @@ +package com.learnJavaMyVersion.methodreference; + +import com.learnJava.data.Student; +import com.learnJava.data.StudentDataBase; + +import java.util.function.Predicate; + +public class RefactorMethodReferenceExample { + static Predicate predicate = RefactorMethodReferenceExample::greaterThanGradeLevel; + + public static boolean greaterThanGradeLevel(Student student){ + return student.getGradeLevel() >=3; + } + + public static void main(String[] args) { + System.out.println(predicate.test(StudentDataBase.studentSupplier.get())); + + } +} diff --git a/java-8/src/com/learnJavaMyVersion/numericstreams/NumericStreamAggrigateExample.java b/java-8/src/com/learnJavaMyVersion/numericstreams/NumericStreamAggrigateExample.java new file mode 100644 index 0000000..37bfa9e --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/numericstreams/NumericStreamAggrigateExample.java @@ -0,0 +1,23 @@ +package com.learnJavaMyVersion.numericstreams; + +import java.util.OptionalDouble; +import java.util.OptionalInt; +import java.util.OptionalLong; +import java.util.stream.IntStream; +import java.util.stream.LongStream; + +public class NumericStreamAggrigateExample { + public static void main(String[] args) { + int sum = IntStream.rangeClosed(1, 50).sum(); + System.out.println("Sum is :"+ sum); + + OptionalInt max = IntStream.rangeClosed(1, 50).max(); + System.out.println(max.isPresent()? max.getAsInt():0); + + OptionalLong min = LongStream.rangeClosed(50, 100).min(); + System.out.println(min.isPresent() ? min.getAsLong():0); + + OptionalDouble average = IntStream.rangeClosed(1, 50).average(); + System.out.println(average.isPresent() ? average.getAsDouble() : 0); + } +} diff --git a/java-8/src/com/learnJavaMyVersion/numericstreams/NumericStreamMapExaple.java b/java-8/src/com/learnJavaMyVersion/numericstreams/NumericStreamMapExaple.java new file mode 100644 index 0000000..587c8a0 --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/numericstreams/NumericStreamMapExaple.java @@ -0,0 +1,36 @@ +package com.learnJavaMyVersion.numericstreams; + +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +public class NumericStreamMapExaple { + + public static List mapToObj(){ + return IntStream.rangeClosed(1,5) + .mapToObj((i)->{ + return new Integer(i); + }) + .collect(Collectors.toList()); + } + + public static Long mapToLong(){ + return IntStream.rangeClosed(1,5) + .mapToLong((i)->i) + .sum(); + + } + + public static double mapToDouble(){ + return IntStream.rangeClosed(1,5) + .mapToDouble((i)->i) + .sum(); + } + + public static void main(String[] args) { + System.out.println("mapToObj : "+ mapToObj()); + System.out.println("mapToLong : "+ mapToLong()); + System.out.println("mapToDouble : "+ mapToDouble()); + + } +} diff --git a/java-8/src/com/learnJavaMyVersion/numericstreams/NumericStreamRangesExample.java b/java-8/src/com/learnJavaMyVersion/numericstreams/NumericStreamRangesExample.java new file mode 100644 index 0000000..2b82005 --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/numericstreams/NumericStreamRangesExample.java @@ -0,0 +1,29 @@ +package com.learnJavaMyVersion.numericstreams; + +import java.util.stream.IntStream; +import java.util.stream.LongStream; + +public class NumericStreamRangesExample { + public static void main(String[] args) { + IntStream range = IntStream.range(1, 50); + System.out.println("Range count : " + range.count()); + IntStream.range(1,50) + .forEach(value -> System.out.print(value + ", ")); + System.out.println("\n Range closed coutn :" + IntStream.rangeClosed(1,50).count()); + + IntStream.rangeClosed(1,50).forEach(value -> System.out.print(value + ", ")); + System.out.println("\n Long Stream closed coutn : "+ LongStream.rangeClosed(1,50).count()); + LongStream.rangeClosed(1,50) + .forEach(value -> System.out.print(value + ", ")); + System.out.println("\n Ways to achieve InDouble support"); + IntStream.range(1,50) + .asDoubleStream() + .forEach(value -> System.out.print(value + ", ")); + System.out.println("\n "); + IntStream.range(1,50) + .asLongStream() + .forEach(value -> System.out.print(value + ", ")); + + + } +} diff --git a/java-8/src/com/learnJavaMyVersion/numericstreams/NumericStreamsBoxingUnBoxingExample.java b/java-8/src/com/learnJavaMyVersion/numericstreams/NumericStreamsBoxingUnBoxingExample.java new file mode 100644 index 0000000..c0116d9 --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/numericstreams/NumericStreamsBoxingUnBoxingExample.java @@ -0,0 +1,23 @@ +package com.learnJavaMyVersion.numericstreams; + +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +public class NumericStreamsBoxingUnBoxingExample { + public static List boxing(){ + return IntStream.rangeClosed(1,10) + .boxed() + .collect(Collectors.toList()); + } + public static int unBoxing(List integerList){ + return integerList.stream() + .mapToInt(Integer::intValue) + .sum(); + } + public static void main(String[] args) { + System.out.println("Boxing : "+ boxing()); + System.out.println("UnBoxing : "+ unBoxing(boxing())); + + } +} diff --git a/java-8/src/com/learnJavaMyVersion/numericstreams/NumericStreamsExample.java b/java-8/src/com/learnJavaMyVersion/numericstreams/NumericStreamsExample.java new file mode 100644 index 0000000..29b33b1 --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/numericstreams/NumericStreamsExample.java @@ -0,0 +1,22 @@ +package com.learnJavaMyVersion.numericstreams; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.IntStream; + +public class NumericStreamsExample { + public static int sumOfNNumbers(List integerList){ + return integerList.stream() + .reduce(0,(x,y) -> x+y); + } + public static int sumOfNNumbersIntStream(){ + return IntStream.rangeClosed(1,6) + .sum(); + } + public static void main(String[] args) { + List integerList = Arrays.asList(1,2,3,4,5,6); + System.out.println("Sum of N Numbers : "+ sumOfNNumbers(integerList)); + System.out.println("Sum of N Numbers using IntStream : "+ sumOfNNumbersIntStream()); + + } +} diff --git a/java-8/src/com/learnJavaMyVersion/streams/CollectionsVsStreams.java b/java-8/src/com/learnJavaMyVersion/streams/CollectionsVsStreams.java new file mode 100644 index 0000000..dcd5413 --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/streams/CollectionsVsStreams.java @@ -0,0 +1,33 @@ +package com.learnJavaMyVersion.streams; + +import java.util.ArrayList; +import java.util.stream.Stream; + +public class CollectionsVsStreams { + public static void main(String[] args) { + ArrayList names = new ArrayList<>(); + names.add("Adam"); + names.add("Jim"); + names.add("Jenny"); + names.remove(0); //possible + System.out.println(names); + + // not possible to modify names.stream(); + //names.stream(). + + //possibel to iterated n times + for(String name:names){ + System.out.println(name); + } + for(String name:names){ + System.out.println(name); + } + + // in streams not possible to iterate n times + Stream stringStream = names.stream(); + stringStream.forEach(System.out::println); + //stringStream.forEach(System.out::println); // will not allow to do second iteration on the stream. not allowed + + + } +} diff --git a/java-8/src/com/learnJavaMyVersion/streams/StreamLimitSkipExample.java b/java-8/src/com/learnJavaMyVersion/streams/StreamLimitSkipExample.java new file mode 100644 index 0000000..15627e2 --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/streams/StreamLimitSkipExample.java @@ -0,0 +1,42 @@ +package com.learnJavaMyVersion.streams; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; + +public class StreamLimitSkipExample { + public static Optional limit(List list){ + return list.stream() + .limit(3) + .reduce((x,y) -> x+y); + } + public static Optional skip(List list){ + return list.stream() + .skip(3) + .reduce((x,y) -> x+y); + } + public static void main(String[] args) { + List integerList = Arrays.asList(6,7,8,9,10); + Optional optional = limit(integerList); + if (optional.isPresent()){ + System.out.println("The limit result is : "+ optional.get()); + }else{ + System.out.println("No input is passed"); + } + Optional optional2 = limit(new ArrayList<>()); + if (optional2.isPresent()){ + System.out.println("The limit result is : "+ optional2.get()); + }else{ + System.out.println("No input is passed"); + } + System.out.println("********Skip********"); + Optional skip = skip(integerList); + if (skip.isPresent()){ + System.out.println("The skip result is : "+ skip.get()); + }else{ + System.out.println("No input is passed"); + } + + } +} diff --git a/java-8/src/com/learnJavaMyVersion/streams/StreamMapExample.java b/java-8/src/com/learnJavaMyVersion/streams/StreamMapExample.java new file mode 100644 index 0000000..63b74f0 --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/streams/StreamMapExample.java @@ -0,0 +1,30 @@ +package com.learnJavaMyVersion.streams; + +import com.learnJava.data.Student; +import com.learnJava.data.StudentDataBase; + +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +public class StreamMapExample { + + public static List namesList(){ + return StudentDataBase.getAllStudents().stream() //Stream + //Student as an input -> Student name + .map(Student::getName) // Stream + .map(String::toUpperCase) //Stream -> uppercase operation on each input + .collect(Collectors.toList()); //List + } + public static Set namesSet(){ + return StudentDataBase.getAllStudents().stream() //Stream + //Student as an input -> Student name + .map(Student::getName) // Stream + .map(String::toUpperCase) //Stream -> uppercase operation on each input + .collect(Collectors.toSet()); //Set + } + public static void main(String[] args) { + System.out.println(namesList()); + System.out.println(namesSet()); + } +} diff --git a/java-8/src/com/learnJavaMyVersion/streams/StreamMapReduceExample.java b/java-8/src/com/learnJavaMyVersion/streams/StreamMapReduceExample.java new file mode 100644 index 0000000..f61f9af --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/streams/StreamMapReduceExample.java @@ -0,0 +1,19 @@ +package com.learnJavaMyVersion.streams; + +import com.learnJava.data.Student; +import com.learnJava.data.StudentDataBase; + +public class StreamMapReduceExample { + + private static int noOfNoteBooks(){ + return StudentDataBase.getAllStudents().stream() + .filter((student) -> student.getGradeLevel() >= 3) + .filter((student) -> student.getGender().equals("female")) + .map(Student::getNoteBooks) + //.reduce(0,(a,b) -> a+b); + .reduce(0,Integer::sum); + } + public static void main(String[] args) { + System.out.println("noOfNotBooks : "+noOfNoteBooks()); + } +} diff --git a/java-8/src/com/learnJavaMyVersion/streams/StreamOfGenerateIterateExample.java b/java-8/src/com/learnJavaMyVersion/streams/StreamOfGenerateIterateExample.java new file mode 100644 index 0000000..cb13756 --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/streams/StreamOfGenerateIterateExample.java @@ -0,0 +1,23 @@ +package com.learnJavaMyVersion.streams; + +import java.util.Random; +import java.util.function.Supplier; +import java.util.stream.Stream; + +public class StreamOfGenerateIterateExample { + public static void main(String[] args) { + + Stream stringStream= Stream.of("adam","dan","julie"); + stringStream.forEach(System.out::println); + + Stream.iterate(1,x->x*2) + .limit(10) + .forEach(System.out::println); + + Supplier integerSupplier = new Random()::nextInt; + Stream.generate(integerSupplier) + .limit(5) + .forEach(System.out::println); + + } +} diff --git a/java-8/src/com/learnJavaMyVersion/streams/StreamReduceExample.java b/java-8/src/com/learnJavaMyVersion/streams/StreamReduceExample.java new file mode 100644 index 0000000..3d16436 --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/streams/StreamReduceExample.java @@ -0,0 +1,43 @@ +package com.learnJavaMyVersion.streams; + +import com.learnJava.data.Student; +import com.learnJava.data.StudentDataBase; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; + +public class StreamReduceExample { + public static int performMultiplication(List integerList){ + return integerList.stream() + .reduce(1,(a,b) -> a*b); + } + + public static Optional performMultiplicationWithoutIdentity(List integerList){ + return integerList.stream() + .reduce((a,b) -> a*b); + } + + public static Optional getHightestGpaStudent(){ + return StudentDataBase.getAllStudents().stream() + .reduce((s1,s2)->(s1.getGpa() > s2.getGpa()) ? s1 :s2); + } + public static void main(String[] args) { + List integerList = Arrays.asList(1,3,5,7); + System.out.println(performMultiplication(integerList)); + Optional result= performMultiplicationWithoutIdentity(integerList); + System.out.println(result.isPresent()); + System.out.println(result.get()); + Optional result1= performMultiplicationWithoutIdentity(new ArrayList<>()); + System.out.println(result1.isPresent()); + if(result1.isPresent()) { + System.out.println(result1.get()); + } + + Optional student = getHightestGpaStudent(); + if (student.isPresent()){ + System.out.println(student.get()); + } + } +} diff --git a/java-8/src/com/learnJavaMyVersion/streams/StreamsComparatorExample.java b/java-8/src/com/learnJavaMyVersion/streams/StreamsComparatorExample.java new file mode 100644 index 0000000..eb40bca --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/streams/StreamsComparatorExample.java @@ -0,0 +1,49 @@ +package com.learnJavaMyVersion.streams; + +import com.learnJava.data.Student; +import com.learnJava.data.StudentDataBase; + +import java.util.Comparator; +import java.util.List; +import java.util.stream.Collectors; + +public class StreamsComparatorExample { + + public static List sortStudentsByName(){ + + return StudentDataBase.getAllStudents() + .stream() + .sorted(Comparator.comparing(Student::getName)) + .collect(Collectors.toList()); + + + } + + public static List sortStudentsByGpa(){ + + return StudentDataBase.getAllStudents() + .stream() + .sorted(Comparator.comparing(Student::getGpa)) + .collect(Collectors.toList()); + + + } + public static List sortStudentsByGpaDesc(){ + + return StudentDataBase.getAllStudents() + .stream() + .sorted(Comparator.comparing(Student::getGpa).reversed()) + .collect(Collectors.toList()); + + + } + public static void main(String[] args) { + System.out.println("Students sorted by Name"); + sortStudentsByName().forEach(System.out::println); + System.out.println("Students sorted by Gpa"); + sortStudentsByGpa().forEach(System.out::println); + System.out.println("Students sorted by Gpa Desc"); + sortStudentsByGpaDesc().forEach(System.out::println); + + } +} diff --git a/java-8/src/com/learnJavaMyVersion/streams/StreamsExample.java b/java-8/src/com/learnJavaMyVersion/streams/StreamsExample.java new file mode 100644 index 0000000..130edd4 --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/streams/StreamsExample.java @@ -0,0 +1,31 @@ +package com.learnJavaMyVersion.streams; + +import com.learnJava.data.Student; +import com.learnJava.data.StudentDataBase; + +import java.util.List; +import java.util.Map; +import java.util.function.Predicate; +import java.util.stream.Collectors; + +public class StreamsExample { + public static void main(String[] args) { + //student name and their activities + Predicate studentPredicate =(student -> student.getGradeLevel() >= 3); + Predicate studentGpaPredicate =(student -> student.getGpa() >= 3.9); + Map> studentMap = StudentDataBase.getAllStudents().stream() + .peek(((student) ->{ + System.out.println("Before 1st filter "+ student); + })) + .filter(studentPredicate) // Stream + .peek(((student) ->{ + System.out.println("After 1st filter "+ student); + })) + .filter(studentGpaPredicate)//Stream + .peek(((student) ->{ + System.out.println("After 2 nd filter "+ student); + })) + .collect(Collectors.toMap(Student::getName,Student::getActivities)); //Map + System.out.println(studentMap); + } +} diff --git a/java-8/src/com/learnJavaMyVersion/streams/StreamsFilterExample.java b/java-8/src/com/learnJavaMyVersion/streams/StreamsFilterExample.java new file mode 100644 index 0000000..7faa250 --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/streams/StreamsFilterExample.java @@ -0,0 +1,21 @@ +package com.learnJavaMyVersion.streams; + +import com.learnJava.data.Student; +import com.learnJava.data.StudentDataBase; + +import java.util.List; +import java.util.stream.Collectors; + +public class StreamsFilterExample { + public static List filterStudents(){ + return StudentDataBase.getAllStudents().stream() + .filter((student -> student.getGender().equals("female"))) + .filter(student -> student.getGpa() >= 3.9) + .collect(Collectors.toList()); + } + public static void main(String[] args) { + System.out.println("filterStudents "); + filterStudents().forEach(System.out::println); + + } +} diff --git a/java-8/src/com/learnJavaMyVersion/streams/StreamsFindAnyFirstExample.java b/java-8/src/com/learnJavaMyVersion/streams/StreamsFindAnyFirstExample.java new file mode 100644 index 0000000..4864d75 --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/streams/StreamsFindAnyFirstExample.java @@ -0,0 +1,35 @@ +package com.learnJavaMyVersion.streams; + +import com.learnJava.data.Student; +import com.learnJava.data.StudentDataBase; + +import java.util.Optional; + +public class StreamsFindAnyFirstExample { + public static Optional findAnyStudent(){ + return StudentDataBase.getAllStudents().stream() + .filter(student -> student.getGpa() >= 3.9) + .findAny(); + } + public static Optional findFirstStudent(){ + return StudentDataBase.getAllStudents().stream() + .filter(student -> student.getGpa() >= 3.9) + .findFirst(); + } + public static void main(String[] args) { + Optional studentOptionalFindAny=findAnyStudent(); + if (studentOptionalFindAny.isPresent()){ + System.out.println("Found the student : "+studentOptionalFindAny.get()); + }else{ + System.out.println("Student not found"); + } + System.out.println("*******Find First******************"); + Optional studentOptionalFindFrist=findFirstStudent(); + if (studentOptionalFindFrist.isPresent()){ + System.out.println("Found the student : "+studentOptionalFindFrist.get()); + }else{ + System.out.println("Student not found"); + } + + } +} diff --git a/java-8/src/com/learnJavaMyVersion/streams/StreamsFlatMapExample.java b/java-8/src/com/learnJavaMyVersion/streams/StreamsFlatMapExample.java new file mode 100644 index 0000000..0b4c553 --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/streams/StreamsFlatMapExample.java @@ -0,0 +1,34 @@ +package com.learnJavaMyVersion.streams; + +import com.learnJava.data.Student; +import com.learnJava.data.StudentDataBase; + +import java.util.List; +import java.util.stream.Collectors; + +public class StreamsFlatMapExample { + + public static List printStudentActivities(){ + + return StudentDataBase.getAllStudents().stream() // Stream + .map(Student::getActivities) //Stream> + .flatMap(List::stream) //Stream + .distinct() // removes duplicate + .sorted() // sort them + .collect(Collectors.toList()); + } + + public static Long getStudentActivitiesCount(){ + + return StudentDataBase.getAllStudents().stream() // Stream + .map(Student::getActivities) //Stream> + .flatMap(List::stream) //Stream + .distinct() // Stream -> with duplicates removed + .count(); + + } + public static void main(String[] args) { + System.out.println(printStudentActivities()); + System.out.println(getStudentActivitiesCount()); + } +} diff --git a/java-8/src/com/learnJavaMyVersion/streams/StreamsMatchExample.java b/java-8/src/com/learnJavaMyVersion/streams/StreamsMatchExample.java new file mode 100644 index 0000000..ee3f684 --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/streams/StreamsMatchExample.java @@ -0,0 +1,23 @@ +package com.learnJavaMyVersion.streams; + +import com.learnJava.data.StudentDataBase; + +public class StreamsMatchExample { + public static boolean allMatch(){ + return StudentDataBase.getAllStudents().stream() + .allMatch(student -> student.getGpa() >= 3.5); + } + public static boolean anyMatch(){ + return StudentDataBase.getAllStudents().stream() + .anyMatch(student -> student.getGpa() >= 4.0); + } + public static boolean nonMatch(){ + return StudentDataBase.getAllStudents().stream() + .noneMatch(student -> student.getGpa() >= 4.1); + } + public static void main(String[] args) { + System.out.println("Result of all Match :"+ allMatch()); + System.out.println("Resutl of any Match : "+ anyMatch()); + System.out.println("Result of non Match : "+ nonMatch()); + } +} diff --git a/java-8/src/com/learnJavaMyVersion/streams/StreamsMinMaxExample.java b/java-8/src/com/learnJavaMyVersion/streams/StreamsMinMaxExample.java new file mode 100644 index 0000000..00b54f3 --- /dev/null +++ b/java-8/src/com/learnJavaMyVersion/streams/StreamsMinMaxExample.java @@ -0,0 +1,65 @@ +package com.learnJavaMyVersion.streams; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; + +public class StreamsMinMaxExample { + public static int findMaxValue(List integerList){ + return integerList.stream() + .reduce(0,(x,y) -> x >y ? x : y); + } + public static Optional findMaxValueOptional(List integerList){ + return integerList.stream() + .reduce((x,y) -> x >y ? x : y); + } + public static int findMinValue(List integerList){ + return integerList.stream() + .reduce(0,(x,y) -> x < y ? x : y); + } + public static Optional findMinValueOptional(List integerList){ + return integerList.stream() + .reduce((x,y) -> x < y ? x : y); + } + public static void main(String[] args) { + + List integerList = Arrays.asList(6,7,8,9,10); + int maxValue= findMaxValue(integerList); + System.out.println("Max value is : "+ maxValue); + System.out.println("Max value is : "+ findMaxValue(new ArrayList<>())); + + Optional optionalInteger = findMaxValueOptional(integerList); + if (optionalInteger.isPresent()){ + System.out.println("Max value optional : "+ optionalInteger.get()); + }else{ + System.out.println("No max Value found"); + } + Optional optionalInteger2 = findMaxValueOptional(new ArrayList<>()); + if (optionalInteger2.isPresent()){ + System.out.println("Max value optional : "+ optionalInteger.get()); + }else{ + System.out.println("No max Value found"); + } + //Min + System.out.println("*************Min****************"); + int minValue= findMinValue(integerList); + System.out.println("Min value is : "+ minValue); + System.out.println("Min value is : "+ findMinValue(new ArrayList<>())); + + Optional optionalMin= findMinValueOptional(integerList); + if (optionalMin.isPresent()){ + System.out.println("Min value optional : "+ optionalMin.get()); + }else{ + System.out.println("No Min Value found"); + } + Optional optionalMin2 = findMinValueOptional(new ArrayList<>()); + if (optionalMin2.isPresent()){ + System.out.println("Min value optional : "+ optionalMin2.get()); + }else{ + System.out.println("No Min Value found"); + } + + + } +}