Dhanush Java Record 5
Dhanush Java Record 5
Ex no: 8.4
PROGRAM USING LAMBDA EXPRESSIONS
Date:
Question
Write a Java program to create a list of strings and convert lowercase letters to uppercase letters
using stream map () function.
Aim
To write a java program to create list of strings and convert them to uppercase letters.
import java.util.*;
import java.util.stream.Collectors;
public class StreamCase {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.print("Enter no. of strings: ");
int n=sc.nextInt();
ArrayList<String> l=new ArrayList<String>(n);
int i;
System.out.println("Enter the strings: ");
for(i=0;i<n;i++)
l.add(sc.next());
System.out.println("Before stream: ");
System.out.println(l);
List<String> li=l.stream().map(String::toUpperCase).collect(Collectors.toList());
System.out.println("After stream: ");
System.out.println(li);
sc.close();
}
}
717822T117-B.Dhanushraj
21PE04-Advanced Java Programming
Output
Result
Thus, the java program was executed successfully and the output was verified.
ALGORITHM 15
PROGRAM 30
EXECUTION 30
OUTPUT & RESULT 15
VIVA 10
TOTAL 100
INITIAL OF FACULTY
717822T117-B.Dhanushraj
21PE04-Advanced Java Programming
Ex no: 9.1
PROGRAM USING STREAMS
Date:
Question
Write a Java program to demonstrate working of Stream.
• Create ArrayList emp that will store list of employee objects.
• Display the employee objects whose salary is less than 10000. (Hint: Use stream,filter concepts).
• Increment 2000 for the employee who is getting the salary less than 5000 using map.
• Sort the employee based on the salary in descending order (i.e highest salary should come on top).
Aim
To write java program to create arraylist employee and display the list using filters.
import java.util.*;
public class Employee {
private int empId;
private String empName;
private int salary;
public Employee(int empId,String empName,int salary){
this.empId=empId;
this.empName=empName;
this.salary=salary;
}
public int getEmpId(){ return this.empId; }
public void setEmpId(int empId){ this.empId=empId; }
public String getEmpName(){ return this.empName; }
public void setEmpName(String empName){ this.empName=empName; }
public int getSalary(){ return this.salary; }
public void setSalary(int salary){ this.salary=salary; }
public Employee incSalary(){
this.salary+=2000;
return this;
}
@Override
public String toString(){
return "Employee [ "+this.getEmpId()+", "+this.getEmpName()+", "+this.getSalary()+" ]";
}
public static void main(String[] args) {
List<Employee> l=Arrays.asList(new Employee(101,"Harry",1000),new Employee
(102, "Potter",5000) ,new Employee(103,"Dora",8000),new Employee (104, "Shinchan" ,3000) ,new
Employee(105,"Patlu",6000));
System.out.println("Employee list:");
l.forEach(System.out::println);
System.out.println();
List<Employee> lsal=l.stream().filter(li->li.getSalary()<10000).toList();
System.out.println("Employees earning less than 10000:");
lsal.forEach(System.out::println);
717822T117-B.Dhanushraj
21PE04-Advanced Java Programming
System.out.println();
List<Employee> incSal=l.stream().filter(ls->ls.getSalary()<=5000)
.map(Employee::incSalary).toList();
System.out.println("Employees salary after incrementing 2000:");
incSal.forEach(System.out::println);
System.out.println();
List<Employee> sortedList=l.stream().sorted(Comparator.comparingDouble
(Employee::getSalary).reversed()).toList();
System.out.println("Employee list in sorted order of salary:");
sortedList.forEach(System.out::println);
}
}
Output
Result
Thus, the java program was executed successfully and the output was verified.
717822T117-B.Dhanushraj
21PE04-Advanced Java Programming
Ex no: 9.2
PROGRAM USING STREAMS
Date:
Question
Implement following UML diagram and Write a Java program for Comparison Based Stream
Operations using below requirements.
Aim
To write a java program to create Student list and sort the list based on cgpa .
import java.util.*;
public class Student {
private int id;
private String name;
private double cgpa;
public Student(int id,String name,double cgpa) {
this.id=id;
this.name=name;
this.cgpa=cgpa;
}
public int getId(){ return this.id; }
public void setId(int id){ this.id=id; }
public String getName(){ return this.name; }
public void setString(String name){ this.name=name; }
public double getCgpa(){ return this.cgpa; }
public void setCgpa(double cgpa){ this.cgpa=cgpa; }
717822T117-B.Dhanushraj
21PE04-Advanced Java Programming
@Override
public String toString(){
return "Student [ "+this.getId()+", "+this.getName()+", "+this.getCgpa()+" ] ";
}
public static void main(String[] args) {
List<Student> l=Arrays.asList(new Student(101,"Shinchan",8.8),new Student(102,
"Dora",9.19),new Student(103,"Harry",8.5),new Student(104,"Patlu",7));
System.out.println("Students list: ");
l.forEach(System.out::println);
System.out.println();
List<Student> sortedList=l.stream().sorted(Comparator.comparingDouble
(Student::getCgpa)).toList();
System.out.println("Students list after sorting: ");
sortedList.forEach(System.out::println);
System.out.println();
double minCgpa=l.stream().mapToDouble(Student::getCgpa).max()
.orElse(Double.MIN_VALUE);
double maxCgpa=l.stream().mapToDouble(Student::getCgpa).min()
.orElse(Double.MAX_VALUE);
System.out.println("Maximum cgpa: "+maxCgpa);
System.out.println();
System.out.println("Minimum cgpa: "+minCgpa);
}
}
Output
Result
Thus, the java program was executed successfully and the output was verified.
ALGORITHM 15
PROGRAM 30
EXECUTION 30
OUTPUT & RESULT 15
VIVA 10
TOTAL 100
INITIAL OF FACULTY
717822T117-B.Dhanushraj
21PE04-Advanced Java Programming
Ex no: 10
PROGRAM USING FILTERS
Date:
Question
Create a LinkedList and add 100 random numbers. Find and display number which is divisible by 8
from LinkedList. Count the numbers which is divisible by 11.(Hint:Use stream, filter, count ).
Aim
To write a java program that generates list of 100 numbers and display the number divisible by 8.
Result
Thus, the java program was executed successfully and the output was verified.
ALGORITHM 15
PROGRAM 30
EXECUTION 30
OUTPUT & RESULT 15
VIVA 10
TOTAL 100
INITIAL OF FACULTY
717822T117-B.Dhanushraj