#Java - Code Snippets for '#Lambda Expression' - 16 code snippet(s) found |
|
Sample 1. Get count of elements greater than 1 using Lambda Expression | |
|
// Declare and Initialize the Collection
Set<Integer> intSet = new HashSet<Integer>();
// Add Elements
intSet.add(1);
intSet.add(2);
intSet.add(3);
intSet.add(4);
// Use Stream, Predicate and Filter to get the count of elements more than 1
System.out.println(intSet.stream().filter(moreThan2Pred).count()); // Prints 3
|
|
Like Feedback lambda expression collections set hashset generics stream predicate filter |
|
|
Sample 2. Get all elements greater than 2, sort them and then push them to a new set, using Lambda Expression | |
|
// Declare and Initialize the Collection
Set<Integer> intSet = new HashSet<Integer>();
// Add Elements
intSet.add(1);
intSet.add(2);
intSet.add(3);
intSet.add(4);
// Set the predicate or the condition for filtering the elements.
Predicate<Integer> moreThan2Pred = (p) -> (p > 1);
// Use Filter to refine the element set, sort to Sort and Collectors.toSet to get a set out of Stream.
intSet = intSet.stream().filter(moreThan2Pred).sorted().collect(Collectors.toSet());
System.out.println(intSet); // Prints [2, 3, 4]
|
|
Like Feedback lambda expression collections set hashset generics stream predicate filter sort java.util.hashset java.util.stream.Collectors |
|
|
Sample 3. Find, if all elements of a collection matches the specified condition ( Using Lambda expressions ) | | Admin info@buggybread.com |
|
|
// Declare and Initialize the Collection
Set<Integer> intSet = new HashSet<Integer>();
// Add Elements
intSet.add(1);
intSet.add(2);
intSet.add(3);
intSet.add(4);
// Specify the Predicate or the Condition using Lambda expressions
Predicate<Integer> moreThan2Pred = (p) -> (p > 2);
// Use the stream method allMatch to see if all elements fulfils the Predicate.
|
|
Like Feedback lambda expression collections set hashset generics predicate |
|
|
Sample 4. Combine two Summaries and Generate a new Summary using Lambda Expression | |
|
// Populate a List using Set elements.
// Declare and Initialize the Collection
Set<Integer> intSet = new HashSet<Integer>();
Set<Integer> intSet2 = new HashSet<Integer>();
// Add Elements
intSet.add(1);
intSet.add(2);
intSet.add(3);
intSet.add(4);
intSet2.add(1);
intSet2.add(2);
intSet2.add(3);
intSet2.add(4);
// Use the stream and collectors to Summarize all Integer elements
IntSummaryStatistics summary = intSet.stream().collect(Collectors.summarizingInt(p->((Integer)p)));
summary.combine(intSet2.stream().collect(Collectors.summarizingInt(p->((Integer)p))));
System.out.println(summary); // Prints IntSummaryStatistics{count=8, sum=20, min=1, average=2.500000, max=4}
|
|
Like Feedback lambda expression collections set hashset generics stream collectors Collectors.summarizingInt summary.combine java.util.hashset java.util.stream.Collectors java.util.IntSummaryStatistics |
|
|
|
Sample 5. Find the Average of all collection elements using Lambda Expressions | |
|
// Declare and Initialize the Collection
Set<Integer> intSet = new HashSet<Integer>();
// Add Elements
intSet.add(1);
intSet.add(2);
intSet.add(3);
intSet.add(4);
// Use the stream and Collector to find the average of all elements.
System.out.println(intSet.stream().collect(Collectors.averagingInt(p->((Integer)p)))); Prints 2.5
|
|
Like Feedback lambda expression collections set hashset generics stream collector |
|
|
Sample 6. Count number of collection elements using Lambda Expressions | |
|
// Declare and Initialize the Collection
Set<Integer> intSet = new HashSet<Integer>();
// Add Elements
intSet.add(1);
intSet.add(2);
intSet.add(3);
intSet.add(4);
// Use the stream method count to see if all elements fulfils the Predicate.
System.out.println(intSet.stream().count); // Prints 4.
|
|
Like Feedback lambda expression collections set hashset generics predicate |
|
|
Sample 7. Group Elements by even or Odd using Lambda Expressions | |
|
// Declare and Initialize the Collection
Set<Integer> intSet = new HashSet<Integer>();
// Add Elements
intSet.add(1);
intSet.add(2);
intSet.add(3);
intSet.add(4);
// Use the stream and Collector to Group by Even and Odd
System.out.println(intSet.stream().collect(Collectors.groupingBy(p->((Integer)p)%2))); Prints {0=[2, 4], 1=[1, 3]}
|
|
Like Feedback lambda expression collections set hashset generics stream collectors Collectors.groupingBy |
|
|
Sample 8. Sum all collection elements using Lambda Expressions | |
|
// Declare and Initialize the Collection
Set<Integer> intSet = new HashSet<Integer>();
// Add Elements
intSet.add(1);
intSet.add(2);
intSet.add(3);
intSet.add(4);
// Use the stream and collectors to sum all elements.
System.out.println(intSet.stream().collect(Collectors.summingInt(p->(Integer)p)));
|
|
Like Feedback lambda expression collections set hashset generics stream collectors Collectors.groupingBy |
|
|
Sample 9. Get the complete Summary of Collection Elements using Lambda Expressions | |
|
// Populate a List using Set elements.
// Declare and Initialize the Collection
Set<Integer> intSet = new HashSet<Integer>();
// Add Elements
intSet.add(1);
intSet.add(2);
intSet.add(3);
intSet.add(4);
// Use the stream and collectors to Summarize all Integer elements
System.out.println(intSet.stream().collect(Collectors.summarizingInt(p->((Integer)p)))); // Prints IntSummaryStatistics{count=4, sum=10, min=1, average=2.500000, max=4}
|
|
Like Feedback lambda expression collections set hashset generics stream collectors Collectors.summarizingInt |
|
|
|
Sample 10. Calculate square of a number using Lambda Expression | |
|
public interface Calc {
int square(int value);
}
public static void main(String[] args){
Calc calc = (int val) -> val * val;
System.out.println(calc.square(5));
}
|
|
Like Feedback lambda Expression java 8 |
|
|
Sample 11. 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 class BuggyBread1 {
public static void main(String args[]) {
// Declare and Initialize the Collection
Set<Integer> intSet = new HashSet<Integer>();
// Add Elements
intSet.add(1);
intSet.add(2);
intSet.add(3);
intSet.add(4);
intSet.add(5);
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;
}
}
|
|
Like Feedback java 8 lambda expressions lambda filter Collectors factorial prime number |
|
|
Sample 12. Find, if any element of the collection matches the specified condition ( Using Lambda Expressions ) | | Admin info@buggybread.com |
|
|
// Declare and Initialize the Collection
Set<Integer> intSet = new HashSet<Integer>();
// Add Elements
intSet.add(1);
intSet.add(2);
intSet.add(3);
intSet.add(4);
// Specify the Predicate or the Condition using Lambda expressions
Predicate<Integer> moreThan2Pred = (p) -> (p > 2);
// Use the stream method anyMatch to see if all elements fulfils the Predicate.
System.out.println(intSet.stream().anyMatch(moreThan2Pred)); // Prints True.
|
|
Like Feedback lambda expression collections set hashset generics predicate |
|
|
Sample 13. Find, if no element of the collection matches the specified condition ( Inverse of anyMatch ) | |
|
// Declare and Initialize the Collection
Set<Integer> intSet = new HashSet<Integer>();
// Add Elements
intSet.add(1);
intSet.add(2);
intSet.add(3);
intSet.add(4);
// Specify the Predicate or the Condition using Lambda expressions
Predicate<Integer> moreThan2Pred = (p) -> (p > 2);
// Use the stream method noneMatch to see if all elements fulfils the Predicate.
System.out.println(intSet.stream().noneMatch(moreThan2Pred)); // Prints False.
|
|
Like Feedback lambda expression collections set hashset generics predicate |
|
|
Sample 14. Populate a List using Set elements and Lambda Expression | |
|
// Declare and Initialize the Collection
Set<Integer> intSet = new HashSet<Integer>();
// Add Elements
intSet.add(1);
intSet.add(2);
intSet.add(3);
intSet.add(4);
// Use the stream and collectors to get List out of Set
List li = intSet.stream().collect(Collectors.toList());
System.out.println(li); // Prints [1, 2, 3, 4]
|
|
Like Feedback lambda expression collections set hashset generics stream collectors Collectors.toList |
|
|
|
Sample 15. Create a Map using Set elements using Lambda Expression | |
|
// Declare and Initialize the Collection
Set<Integer> intSet = new HashSet<Integer>();
// Add Elements
intSet.add(1);
intSet.add(2);
intSet.add(3);
intSet.add(4);
// Use the stream and collectors to get a Map out of Set
System.out.println(intSet.stream().collect((Collectors.toMap(p->(Integer)p,q->((Integer)q)*500)))); // Prints {1=500, 2=1000, 3=1500, 4=2000}
|
|
Like Feedback lambda expression collections set hashset generics stream collectors |
|
|
Sample 16. Get the Maximum number out of the Integer List, using Lambda Expression | |
|
// Declare and Initialize the Collection
List<Integer> intList = new ArrayList<Integer>();
// Add Elements
intList.add(1);
intList.add(2);
intList.add(3);
intList.add(4);
System.out.println(intList.stream().reduce(Math::max).get()); // Prints 1
|
|
Like Feedback lambda expression collections set hashset generics stream reducer Math::max |
|
|