01/08/2023
important with solution.
Hello, guys! 👋 Welcome to my page! 🎉
I'm excited to have you here on this platform where we'll explore the fascinating world of coding and programming together! 💻🌐
I have writing here some logical program using java 8 feature Stream API.
-----------------------------------------------------------------------
Q.1 Write a program to sort number using stream.
List list= Arrays.asList(1,4,2,5,6,3,10);
List sortedlist=list.stream().sorted().collect(Collectors.toList());
System.out.println(sortedlist);//[1, 2, 3, 4, 5, 6, 10]
-----------------------------------------------------------------------
Q.2 write a program to print even number from list
List evenNumber=list.stream().filter(i->i%2==0).collect(Collectors.toList());
System.out.println(evenNumber); //[4, 2, 6,10]
-----------------------------------------------------------------------
Q.3 write a program to print even number from list and multiply with 5
List evenNumber=list.stream().filter(i->i%2==0).map(m->m*5).collect(Collectors.toList());
System.out.println(evenNumber); //[20, 10, 30, 50]
-----------------------------------------------------------------------
Q.3 Write a program to count number in stream
System.out.println(list.stream().count()); //7
-----------------------------------------------------------------------
Q.4 write a program to print number start with 1
List startwithone= list.stream().filter(f->f.toString().startsWith("1")).collect(Collectors.toList());
System.out.println(startwithone); //[1, 10]
-----------------------------------------------------------------------
Q.5 write a program to find duplicate element from list using stream api.
List list = Arrays.asList(1, 2, 3, 2, 4,2, 5, 6, 4, 7, 8, 8, 9);
Set set= new HashSet();
list.stream().filter(i>!set.add(i)).distinct().forEach(System.out::println);
-----------------------------------------------------------------------
Q.6 write a program to sort employee thier name
List employees = Arrays.asList(new Employee(1,"John", 4500),new Employee(2,"Alice", 6000),
new Employee(3,"Bob", 3500),new Employee(4,"Jane", 8000));
employees.sort(Comparator.comparing(Employee::getName));
for(Employee e1:employees) {
System.out.println("empName==>"+e1.getName());
}
-----------------------------------------------------------------------
Q.7 write a program to get employee where sal greater than 5000
List filteredEmployees = employees.stream().filter(e -> e.getSalary() > 5000).collect(Collectors.toList());
for (Employee e1: filteredEmployees) {
System.out.println("Name: " + e1.getName() + ", Salary: " + e1.getSalary());
}
-----------------------------------------------------------------------
Q.8 write a program to count the occurrences of each character and print the character along with its frequency.
String inputstr="programming";
Map map = inputstr.chars().mapToObj(c->(char)c)
.collect(Collectors.groupingBy(Function.identity(),
Collectors.counting()));
System.out.println(map);
}
----------------------------------------------------------------------
Q,9 write a program to print asc order in java
List list=Arrays.asList("pritam","rakesh","rajput","deepak");
Collections.sort(list,Comparator.naturalOrder());
System.out.println(list);
-----------------------------------------------------------------------
Q,10 . write a program to print desc order
List list= Arrays.asList(1,2,3,4,5,71,7,0);
Collections.sort(list,Comparator.reverseOrder());
System.out.println(list);
----------------------------------------------------------------------