0% found this document useful (0 votes)
1K views

This Study Resource Was

1. The document discusses Java 8 lambda expressions and functions. It provides code examples for sorting a list of employees by name using a lambda expression, determining if a number is prime or composite, calculating factorials, and storing string elements from a split string into a list. 2. Methods are defined to sort a list of employee objects by name, determine if a number is prime or composite, calculate the factorial of a number, and store string elements from a split string into a list using lambda expressions and functions. 3. Code examples demonstrate using lambda expressions and functions like Predicate, BiConsumer, and Consumer to perform tasks like sorting, testing conditions, accepting parameters, and adding to a list.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

This Study Resource Was

1. The document discusses Java 8 lambda expressions and functions. It provides code examples for sorting a list of employees by name using a lambda expression, determining if a number is prime or composite, calculating factorials, and storing string elements from a split string into a list. 2. Methods are defined to sort a list of employee objects by name, determine if a number is prime or composite, calculate the factorial of a number, and store string elements from a split string into a list using lambda expressions and functions. 3. Code examples demonstrate using lambda expressions and functions like Predicate, BiConsumer, and Consumer to perform tasks like sorting, testing conditions, accepting parameters, and adding to a list.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

java8 Innards-lambda expressions////////////////////

import java.io.*;
import java.util.*;

class Employee
{
//Create the Constructor here
int id;
String name;
int age;
Employee(String name,int id,int age){

this.name=name;
this.id=id;
this.age=age;
}
}

class SortEmployees
{
void sortEmployees(ArrayList<Employee> empList)
{

m
er as
//Enter your Code here

co
Collections.sort(empList,(o1,o2) -> { return o1.name.compareTo(o2.name);

eH w
});

o.
for(Employee e:empList)
rs e
System.out.println(e.name+" "+e.id+" "+e.age);
ou urc
}
}
o
aC s

//////////////////////////////
v i y re

import java.io.*;
import java.util.function.*;
import java.util.function.Predicate;
import java.util.function.BiConsumer;
import java.util.function.IntPredicate;
ed d

import java.util.stream.IntStream;
ar stu

import java.util.stream.LongStream;

class PrimeComposite_Factorial
{
void primeOrComposite(int n)
sh is

{
Th

//Enter your Code here


if(n>=0 && n<=20){
Predicate<Integer> pre = (n1)->{
// boolean isPrime = true;

for(int i = 2; i <= n1/2; i++)


{
// condition for nonprime number
if(n1 % i == 0)
{
//isPrime = false;
//break;
return false;//Composite
}
}

return true;//Prime

This study source was downloaded by 100000821315344 from CourseHero.com on 09-18-2021 16:13:05 GMT -05:00

https://www.coursehero.com/file/94160721/java8-innardstxt/
// if (isPrime == true && n1!=1)
// return true;
// else
// return false;
};
// System.out.println( pre.test(n));

BiConsumer<Boolean,Integer> c = (pre1,n2) -> {if(pre1==true && n2!


=1)
System.out.println("Prime");
else if(pre1==false && n2!=1)
System.out.println("Composite");
else
System.out.println("Neither
Prime Nor Composite");
} ;
c.accept(pre.test(n),n);}}

void findFactorial(int n)
{
//Enter your Code here
Consumer<Integer> cc=(n1)->{

m
int c, f = 1;

er as
if (n1 < 0)

co
System.out.println("Number should be non-

eH w
negative.");
else

o.
{
rs e
for (c = 1; c <= n; c++)
ou urc
f = f*c;

System.out.println(f);
}
o

};
aC s

cc.accept(n);
v i y re

}
}
ed d
ar stu

///////////////////////
import java.io.*;
import java.lang.*;
import java.util.*;
sh is

import java.util.stream.IntStream;
import java.util.stream.Stream;
Th

import java.util.function.Consumer;
import java.util.function.Supplier;

class StoreElementsInCollection
{
static void storeElements(String input)
{
//Enter your Code here
String[] str = input.split(",");
List<String> l = new ArrayList<>();

Consumer<String> c=(s)->{
l.add(s);
};
for(int i =0;i<str.length;i++){

This study source was downloaded by 100000821315344 from CourseHero.com on 09-18-2021 16:13:05 GMT -05:00

https://www.coursehero.com/file/94160721/java8-innardstxt/
c.accept(str[i]);
}

// Supplier<Void> supp=()->{
// for(int i=0;i<l.size();i++){
// System.out.println(l.get(i));
// }
// };
// supp.get();
for(int i=0;i<l.size();i++){
// System.out.println(l.get(i)+l.size());

if(l.get(i)=="cheese Sandwich"||l.get(i)=="corn sandwich"||


l.get(i)=="mix veg sandwich"){
// l.forEach(System.out::println);
System.out.println(l.get(i));
}}

m
}

er as
co
public class StoreElementsInCollectionMain

eH w
o.
rs e
ou urc
o
aC s
v i y re
ed d
ar stu
sh is
Th

This study source was downloaded by 100000821315344 from CourseHero.com on 09-18-2021 16:13:05 GMT -05:00

https://www.coursehero.com/file/94160721/java8-innardstxt/
Powered by TCPDF (www.tcpdf.org)

You might also like