## Search Java Code Snippets

Help us in improving the repository. Add new snippets through 'Submit Code Snippet ' link.

#Java - Code Snippets for '#Factorial' - 1 code snippet(s) found

Sample 1. Write a Program that gets a set of numbers , filters out the non prime numbers , calculate the factorial of each prime number and then finds the average of all factorials using Lambda expressions

public static void main(String args[]) {

// Declare and Initialize the Collection
Set<Integer> intSet = new HashSet<Integer>();

double averageOfNonPrimeFactorials = intSet.stream().filter(p->checkIfPrime(p)).collect(Collectors.averagingInt(p->calculateFactorial(p)));

System.out.println(averageOfNonPrimeFactorials );
}

static private boolean checkIfPrime(int num){
for(int count=2;count < num;count++){
if(num % count == 0){
return false;
}
}
return true;
}

static private int calculateFactorial(int num){
int factorial = 1;
for(int count=num;count > 0;count--){
factorial = factorial * count;
}
return factorial;
}
}

java 8  lambda expressions  lambda  filter  Collectors  factorial  prime number

Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com