Monday, December 4, 2017

Java 8 : Find any , first ,max, min ,first few(limit) ,(first few from multiple lists) .


How to find any/first/max/min/first few(limit)/(first few from multiple lists) elements ?








package com.tvidushi.java8.find;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class CommonOperations {
/**
* If you need any element of collection
*/
public static void findAny() {
List<String> list = Arrays.asList("First","Second","Third");
list.stream()
.findAny()
.ifPresent(s->System.out.println("Find Any :"+s));
}
/**
* If you need first elements of collection
*/
public static void findFirst() {
Arrays.asList("Apple","Mango","Banana").stream()
.findFirst()
.ifPresent(s->System.out.println("The first element of collection is :"+s));
}
/**
* If you need need only few elements of collection
*/
public static void limitedOutput() {
Arrays.asList("Passenger-1","Passenger-2","Passenger-3","Passenger-4","Passenger-5").stream()
.limit(3)
.forEach(s->System.out.println(s));
}
/**
* If you need need only few elements from multiple lists
*/
public static void mergeOutput() {
List<String> list1 = Arrays.asList("Passenger-1","Passenger-2","Passenger-3","Passenger-4","Passenger-5");
List<String> list2 = Arrays.asList("Passenger-6","Passenger-7","Passenger-8","Passenger-9","Passenger-10");
Stream.concat(list1.stream().limit(3), list2.stream().limit(2))
.collect(Collectors.toList()).forEach(x->System.out.println(x));
}
/**
* If you need maximum element in a list
*/
public static void maxNumber() {
Integer max = Arrays.asList(172,124,142,157,167).stream().max((a,b)->a.compareTo(b)).get();
System.out.println("Max: number is :"+ max);
}
/**
* If you need minimum element in a list
*/
public static void minNumber() {
Optional<Integer> min = Arrays.asList(172,124,142,157,167)
.stream()
.min((a,b)->a.compareTo(b));
System.out.println("Min: number is :"+ min);
}
/**
* Find topper of the class
*/
public static void maxScore() {
List<Student> list = Student.getStudentList();
Student max = list.stream().max(Comparator.comparing(Student::getMarks)).get();
System.out.println("Roll no "+max.getRollno()+" "+max.getName()+" has scored the maximum marks "+max.getMarks());
}
/**
* Find least scoring person
*/
public static void minScore() {
List<Student> list = Student.getStudentList();
Student min = list.stream().min(Comparator.comparing(Student::getMarks)).get();
System.out.println("Roll no "+min.getRollno()+" "+min.getName()+" has scored the maximum marks "+min.getMarks());
}
public static void main(String[] args) {
findAny() ;
findFirst();
limitedOutput() ;
maxNumber();
minNumber();
mergeOutput();
maxScore();
minScore();
}
static class Student {
public int rollno;
public String name;
public int marks;
public Student(int rollno,String name,int marks ){
this.rollno = rollno;
this.name = name;
this.marks = marks;
}
public static List<Student> getStudentList(){
return Arrays.asList(new Student(1, "Ayush", 20),
new Student(2, "Bob", 30),
new Student(3, "Catherine", 24),
new Student(4, "Dinesh", 21),
new Student(5, "Avinash", 20),
new Student(6, "Nash", 30),
new Student(7, "Sara", 34),
new Student(8, "Rob", 21));
}
public int getRollno() {
return rollno;
}
public void setRollno(int rollno) {
this.rollno = rollno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMarks() {
return marks;
}
public void setMarks(int marks) {
this.marks = marks;
}
}
}


Output :


No comments:

Post a Comment