Commit e3ac972c authored by Ravinder Pannala's avatar Ravinder Pannala

Second commit

parent 3be07cb7
This diff is collapsed.
......@@ -17,4 +17,17 @@ public class EmployeeUtils {
return empList;
}
public static List<Employee> fetchEmployee1() {
List<Employee> empList = new ArrayList<>();
empList.add(new Employee(3434, "Raghu", "developer", 10001.00, "BA"));
empList.add(new Employee(123, "ashoke", "manager", 10001.00, "DEV"));
empList.add(new Employee(424, "bharat", "lead", 10002.00, "QA"));
empList.add(new Employee(621, "chandu", "director", 10005.00, "BA"));
empList.add(new Employee(93, "danish", "developer", 10006.00, "DEV"));
empList.add(new Employee(448, "eenadu", "developer", 10001.00, "BA"));
empList.add(new Employee(432, "Ayaan", "developer", 10001.00, "BA"));
return empList;
}
}
......@@ -31,7 +31,7 @@ public class AnyMatchEx {
//Count the number of elements in stream
long count = list.stream().count();
System.out.println("Count the number of elements avaialble in Stream"+ count);
System.out.println("Count the number of elements available in Stream"+ count);
//Distinct elements from the stream
list.stream().distinct().forEach(System.out::println);
......
package com.java8.features.FunctionalInterfaces.streams;
import com.java8.features.Utils.Address;
import com.java8.features.Utils.Student;
import com.java8.features.Utils.StudentUtils;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.DoubleStream;
import java.util.stream.Stream;
......@@ -11,10 +17,18 @@ public class FlatMapEx {
public static void main(String[] args) {
//Input : [[1,2,3,4],[5,6,7,8] output :[1,2,3,4,5,6,7,8]
List<List<Integer>> list = Arrays.asList(Arrays.asList(1, 2, 3, 4), Arrays.asList(5, 6, 7, 8));
list.stream().flatMap(List::stream).forEach(System.out::println);//1,2,3,4,5,6,7,8
List<Student> students = StudentUtils.fetchAllStudent();
students.stream().map(Student::getFirstName).collect(Collectors.toList());
List<Address> collect = students.stream().flatMap(s -> s.getAddresses().get().stream()).collect(Collectors.toList());
System.out.println(collect);
}
}
package com.java8.features.FunctionalInterfaces.streams;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Peek {
/*
This method exists mainly to support debugging, where you want to see the elements
as they flow past a certain point in a pipeline
*/
public static void main(String[] args) {
List<String> collect = Stream.of("one", "two", "three", "four")
.filter(e -> e.length() > 3)
.peek(e -> System.out.println("Filtered value: " + e))
.map(String::toUpperCase)
.peek(e -> System.out.println("Mapped value: " + e))
.collect(Collectors.toList());
}
}
......@@ -18,6 +18,13 @@ public class SkipAndLimit {
//Limit the Elemets from list
List<Integer> collect = list.stream().limit(1).collect(Collectors.toList());
List<Integer> collect3 =list.stream().limit(1).collect(Collectors.toList());
List<Integer> collect2= list.stream().limit(1).collect(Collectors.toList());
System.out.println("Limit the n elements from list"+ collect);
System.out.println("Limit the n elements from list"+ collect3);
System.out.println("Limit the n elements from list"+ collect2);
List<Integer> collect4= list.parallelStream().limit(1).collect(Collectors.toList());
System.out.println("Limit the n elements from list"+ collect4);
}
}
package com.java8.features.FunctionalInterfaces.streams;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class SortedEx {
public static void main(String[] args) {
List<String> stringList= Arrays.asList("Ravi","Ayyan","Zoo");
//Sorting elements in asc order
stringList.stream().sorted().forEach(System.out::println);
//Sorting elemets in desc order
stringList.stream().sorted(Collections.reverseOrder()).forEach(System.out::println);
//Sorting elements using comparator.comparing
Function<String,String> function = (s)->{
return s;
};
Comparator<String> comparator = Comparator.comparing(function);
stringList.stream().sorted(comparator).forEach(System.out::println);
//Sort the elements in desc order
stringList.stream().sorted(comparator.reversed()).forEach(System.out::println);
//Sort List of Integers and null values as first
List<Integer> list = Arrays.asList(4,null,null,2,6,1,3);
List<Integer> collect = list.stream().sorted(Comparator.comparing(i -> i, Comparator.nullsFirst(Comparator.naturalOrder()))).collect(Collectors.toList());
System.out.println("print null values first and Compare elements"+ collect);
List<Integer> collect1 = list.stream().sorted((Comparator.comparing(i -> i, Comparator.nullsLast(Comparator.reverseOrder())))).collect(Collectors.toList());
System.out.println("print null values last and Compare elements"+ collect1);
}
}
package com.java8.features.FunctionalInterfaces.streams;
import com.java8.features.Employee;
import com.java8.features.EmployeeUtils;
import com.java8.features.Utils.Student;
import com.java8.features.Utils.StudentUtils;
import java.util.*;
import java.util.stream.Collectors;
public class SortedEx2 {
public static void main(String[] args) {
Map<String,Integer> map = new HashMap<>();
map.put("Hello",4);
map.put("Ravinder",5);
map.put("Apple",1);
map.put("Zoo",3);
map.put("Pannala",6);
//Sort the map using Keys
LinkedHashMap<String, Integer> collect = map.entrySet().stream().
sorted(Map.Entry.comparingByKey())
.collect(Collectors.toMap
(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2, LinkedHashMap::new));
System.out.println("Comparing by Keys asc order: ---"+collect);
//Sort the Keys in Desc orders
LinkedHashMap<String, Integer> collect2 = map.entrySet().stream().
sorted(Map.Entry.comparingByKey(Comparator.reverseOrder())).
collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2, LinkedHashMap::new));
System.out.println("Comparing by Keys in desc order: ---"+collect2);
//Sort the Map using values
LinkedHashMap<String, Integer> collect1 = map.entrySet().stream().
sorted(Map.Entry.comparingByValue()).
collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
System.out.println("Comparing by values: ---"+collect1);
//Sort the values in desc order
LinkedHashMap<String, Integer> collect3 = map.entrySet().stream().
sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).
collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
System.out.println("Comparing by values in desc: ---"+collect3);
}
}
package com.java8.features.FunctionalInterfaces.streams;
import com.java8.features.Employee;
import com.java8.features.EmployeeUtils;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class SortedEx3 {
public static void main(String[] args) {
List<Employee> employees = EmployeeUtils.fetchEmployee1();
Map<Employee, Employee> collect = employees.stream().collect(Collectors.toMap(e -> e, e -> e));
//Sort employees key in asc order
//Sort employees values in asc order
Map<Employee, Employee> result = collect.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue(Comparator.comparing(Employee::getFirstName)))
.collect(Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(oldValue, newValue) -> oldValue, LinkedHashMap::new));
System.out.println("Sort tje "+ result);
}
}
......@@ -14,10 +14,11 @@ public class StreamCreation {
public static void main(String[] args) {
//The empty() method is used upon creation to avoid returning null for streams with no element
Stream<Object> empty = Stream.empty();
System.out.println(empty.findFirst());//Optional.empty
//Create stream with of
//Stream.of(T…t) method can be used to create a stream with the specified t values, where t are the elements
Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5);
//Create Stream with Collection
......@@ -44,6 +45,10 @@ public class StreamCreation {
limit.forEach(System.out::println);
//Create Stream with Iterate
/*
The iterate() method returns an infinite sequential ordered Stream produced by
iterative application of a function f to an initial element seed
*/
UnaryOperator<Integer> integerUnaryOperator = i->i*10;
Stream<Integer> limit1 = Stream.iterate(1, integerUnaryOperator).limit(10);
limit1.forEach(System.out::println);
......
package com.java8.features.FunctionalInterfaces.streams;
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ToArray {
public static void main(String[] args) {
Stream<String> stream = Stream.of("Ravi", "Ayaan", "Raju");
Object[] string = stream.toArray();
System.out.println(Arrays.toString(string));
//Filter the stream and store in array
Object[] objects = stream.filter(s -> s.length() > 4).toArray();
}
}
package com.java8.features.FunctionalInterfaces.Collectors;
package com.java8.features.Joiner;
import java.util.Arrays;
......
package com.java8.features.LamdaExpressions;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
public class SupplierEx {
public static void main(String[] args) {
String name = "Ravinder";
Supplier<Boolean> booleanSupplier = () -> name.length() > 4;
Supplier<Integer> integerSupplier = () -> name.indexOf('R');
Supplier<String> supplier = ( )-> name.toLowerCase();
Supplier<List> supplier1 = () ->new ArrayList();
System.out.println(booleanSupplier.get());
System.out.println(integerSupplier.get());
System.out.println(supplier.get());
System.out.println(supplier1.get());
}
}
package com.java8.features.LamdaExpressions.predicate;
public class Test {
}
package com.java8.features.Optional;
import com.java8.features.Employee;
import com.java8.features.EmployeeUtils;
import com.java8.features.Utils.Student;
import com.java8.features.Utils.StudentUtils;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class Optional2 {
/*
optionalExmaple.displayFirstEmployeeFromList();
optionalExmaple.groupEmployeesByDepartment();
optionalExmaple.getEmployeesMatchingWithString();
optionalExmaple.changeStoreIdBasedOnCurrentLength();
optionalExmaple.getEmployeesNotMatchingDepartment();
optionalExmaple.arrangeEmployeesByFirstName();
optionalExmaple.getEmployeeWhoseIdIsHighest();
optionalExmaple.printAllEmployeeNamesWithPipe();
optionalExmaple.getNthElementFromEmployee(5);
optionalExmaple.fetchEmployeeMatchingWithIds();
optionalExmaple.fetchEmployeesByGender();
optionalExmaple.printMalesAndFemalesList();
*/
Optional<List<Student>> studentList = StudentUtils.fetchStudent();
public static void main(String[] args) {
Optional2 optional2 = new Optional2();
optional2.displayFirstStudentFromList();
optional2.groupStudentsByDepartment();
optional2.getStudentMatchingWithString();
optional2.arrangeStudentsByFirstName();
optional2.printMalesAndFemalesList();
}
public void displayFirstStudentFromList() {
Student student = studentList.orElseGet(Collections::emptyList).stream().limit(1).findFirst().get();
student.displayStudent();
}
public void groupStudentsByDepartment() {
Map<Optional<String>, Long> collect = studentList.orElseGet(Collections::emptyList).stream().collect(Collectors.groupingBy(Student::getDepartmentName, Collectors.counting()));
System.out.println(collect);
}
public void getStudentMatchingWithString() {
Map<Optional<String>, List<Student>> studentGropByDepMap = studentList.orElseGet(Collections::emptyList).stream().collect(Collectors.groupingBy(Student::getDepartmentName));
List<Student> collect = studentGropByDepMap.values().stream().flatMap(List::stream).collect(Collectors.toList());
List<Student> student = this.biFunction.apply("RAVI", collect);
System.out.println(student);
}
public void arrangeStudentsByFirstName() {
List<Student> collect = studentList.orElseGet(Collections::emptyList).stream().sorted(Comparator.comparing(s->s.getFirstName().get())).collect(Collectors.toList());
System.out.println(collect);
}
public void printMalesAndFemalesList(){
List<Student> female = studentList.orElseGet(Collections::emptyList).stream().filter(s -> s.getGender().equals("FEMALE")).map(std -> this.fullNameFunction.apply(std)).collect(Collectors.toList());
System.out.println("Female--->"+female);
}
public BiFunction<String, List<Student>, List<Student>> biFunction = (searchName, stdList) -> {
List<Student> collect = stdList.stream().filter(std -> this.predicate.test(std, searchName)).collect(Collectors.toList());
return collect;
};
public BiPredicate<Student, String> predicate = (std, searchName) -> {
boolean b = std.getFirstName().get().toUpperCase().equals(searchName) || std.getFirstName().get().toLowerCase().equals(searchName);
return b;
};
public Function<Student,Student> fullNameFunction =(std)->{
String fullName = new StringBuilder().append(std.getFirstName()).append("-").append(std.getLastName()).toString();
std.setFullName(fullName);
return std;
};
}
package com.java8.features.Optional;
import java.util.Optional;
import java.util.function.Supplier;
public class OptionalEx {
public static void main(String[] args) {
//Empty Optional Object
Optional<Object> empty = Optional.empty();
//String name=null; Null Pointer Exception will get
String name="Ravi";
Optional<String> name1 = Optional.of(name);
System.out.println(name1.get());
String s= "Hello";
Optional<String> s1 = Optional.ofNullable(s); //If s value available it will s value other wise it give Optional.empty
System.out.println(s1.get()); //If you get value if it is not present java.util.NoSuchElementException
String str1 = null;
String value = Optional.ofNullable(str1).orElse("Ravi");
System.out.println("If value is not present -->"+value);
Supplier<String> stringSupplier = ()->"Pannala"; // If value is not present then it will give supplier value
String s2 = Optional.ofNullable(str1).orElseGet(stringSupplier);
System.out.println("If value is not present then it will give supplier value-->"+s2);
//if value is not present then it will give supplier object
Supplier<NullPointerException> exceptionSupplier = ()-> new NullPointerException("Value is not resent");
String s3 = Optional.ofNullable(str1).orElseThrow(exceptionSupplier);
System.out.println("if value is not present then it will give supplier object-->"+s3);
Optional<String> s4 = Optional.of(s);
if(s4.isPresent()){
System.out.println("Value present--->"+s4.get());
}
}
}
package com.java8.features.Utils;
import java.util.Optional;
public class Address {
private Optional<String> addressText;
private Optional<Long> contactNumber;
public Address(String address, long contactNumber) {
this.addressText = Optional.ofNullable(address);
this.contactNumber = Optional.ofNullable(contactNumber);
}
public Optional<String> getAddressText() {
return addressText;
}
public void setAddressText(Optional<String> addressText) {
this.addressText = addressText;
}
public Optional<Long> getContactNumber() {
return contactNumber;
}
public void setContactNumber(Optional<Long> contactNumber) {
this.contactNumber = contactNumber;
}
@Override
public String toString() {
return "Address{" +
"addressText=" + addressText +
", contactNumber=" + contactNumber +
'}';
}
}
package com.java8.features.Utils;
import java.util.List;
import java.util.Optional;
public class Student {
private String firstName;
private String lastName;
private String fullName;
private List<Address> addresses;
private Long salary;
private String departmentName;
private Integer studentId;
private String gender;
public void setGender(String gender) {
this.gender = gender;
}
public Optional<String> getGender() {
return Optional.ofNullable(gender);
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public void setAddresses(List<Address> addresses) {
this.addresses = addresses;
}
public void setSalary(Long salary) {
this.salary = salary;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
public void setStudentId(Integer studentId) {
this.studentId = studentId;
}
public Optional<String> getFirstName() {
return Optional.ofNullable(firstName);
}
public Optional<String> getLastName() {
return Optional.ofNullable(lastName);
}
public Optional<String> getFullName() {
return Optional.ofNullable(fullName);
}
public Optional<List<Address>> getAddresses() {
return Optional.ofNullable(addresses);
}
public Optional<Long> getSalary() {
return Optional.ofNullable(salary);
}
public Optional<String> getDepartmentName() {
return Optional.ofNullable(departmentName);
}
public Optional<Integer> getStudentId() {
return Optional.ofNullable(studentId);
}
public Student(String firstName, String lastName, String fullName, List<Address> addresses, Long salary, String departmentName, Integer studentId,String gender) {
this.firstName = firstName;
this.lastName = lastName;
this.fullName = fullName;
this.addresses = addresses;
this.salary = salary;
this.departmentName = departmentName;
this.studentId = studentId;
this.gender = gender;
}
public Student() {
}
public void displayStudent() {
System.out.println(new StringBuilder().append("StudentId : ").append(this.studentId)
.append("Student Name:: ").append(this.firstName).append(this.lastName)
.append(" and Salary :: ").append(this.salary)
.append("Full Name").append(this.fullName)
.append(" Dept ").append(this.departmentName)
.toString());
}
@Override
public String toString() {
return "Student{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", fullName='" + fullName + '\'' +
", addresses=" + addresses +
", salary=" + salary +
", departmentName='" + departmentName + '\'' +
", studentId=" + studentId +
'}';
}
}
package com.java8.features.Utils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
public class StudentUtils {
public static Optional<List<Student>> fetchStudent() {
List<Student> studentList = new ArrayList<Student>();
Address stuAddress1 = new Address("Hyderabad, address1", 0);
Address stuAddress2 = new Address("Hyderabad, address2", 0);
Address stdAddress3 = new Address("Hyderabad, address3", 0);
List<Address> stdAddresses = Arrays.asList(stuAddress1, stuAddress2, stdAddress3);
Student student = new Student("RAVI", "Pannala", null, stdAddresses, 234000L, "CSE", 41451,"MALE");
studentList.add(student);
Address std2Address1 = new Address("Vikarabad, address1", 37468787);
Address std2Address2 = new Address("Kondapur, address2", 387468);
Address std2Address3 = new Address("Hyderabad, address3", 0);
List<Address> std2Addresses = Arrays.asList(std2Address1, std2Address2, std2Address3);
Student student2 = new Student("Ayaan", "Pannala", null, std2Addresses, 43000l, "ECE", 41453,"MALE");
studentList.add(student2);
Address std3Address1 = new Address("Miyapur, address1", 0);
Address std3Address2 = new Address("Hitech city, address2", 37473657);
Address std3Address3 = new Address("Gachibowli, address3", 0);
List<Address> std3Addresses = Arrays.asList(std3Address1, std3Address2, std3Address3);
Student student3 = new Student("Charvik", "Pannala", null, std3Addresses, 32342L, "MEC", 41454,"MALE");
studentList.add(student3);
Address std4Address1 = new Address("Anantapur, address1", 0);
Address std4Address2 = new Address("Kurnool, address3", 0);
List<Address> std4Addresses = Arrays.asList(std4Address1, std4Address2, std3Address3);
Student student4 = new Student("Lavanya", "Etikyla", null, std4Addresses, 34534L, "EEE", 41456,"FEMALE");
studentList.add(student4);
return Optional.of(studentList);
}
public static List<Student> fetchAllStudent() {
List<Student> studentList = new ArrayList<Student>();
Address stuAddress1 = new Address("Hyderabad, address1", 0);
Address stuAddress2 = new Address("Hyderabad, address2", 0);
Address stdAddress3 = new Address("Hyderabad, address3", 0);
List<Address> stdAddresses = Arrays.asList(stuAddress1, stuAddress2, stdAddress3);
Student student = new Student("RAVI", "Pannala", null, stdAddresses, 234000L, "CSE", 41451,"MALE");
studentList.add(student);
Address std2Address1 = new Address("Vikarabad, address1", 37468787);
Address std2Address2 = new Address("Kondapur, address2", 387468);
Address std2Address3 = new Address("Hyderabad, address3", 0);
List<Address> std2Addresses = Arrays.asList(std2Address1, std2Address2, std2Address3);
Student student2 = new Student("Ayaan", "Pannala", null, std2Addresses, 43000l, "ECE", 41453,"MALE");
studentList.add(student2);
Address std3Address1 = new Address("Miyapur, address1", 0);
Address std3Address2 = new Address("Hitech city, address2", 37473657);
Address std3Address3 = new Address("Gachibowli, address3", 0);
List<Address> std3Addresses = Arrays.asList(std3Address1, std3Address2, std3Address3);
Student student3 = new Student("Charvik", "Pannala", null, std3Addresses, 32342L, "MEC", 41454,"MALE");
studentList.add(student3);
Address std4Address1 = new Address("Anantapur, address1", 0);
Address std4Address2 = new Address("Kurnool, address3", 0);
List<Address> std4Addresses = Arrays.asList(std4Address1, std4Address2, std3Address3);
Student student4 = new Student("Lavanya", "Etikyla", null, std4Addresses, 34534L, "EEE", 41456,"FEMALE");
studentList.add(student4);
return studentList;
}
}
package com.java8.features.foreach;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
public class forEachEx {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1,2,3,4,5);
Consumer<Integer> consumer= (i)->System.out.println("iterating elements using forEach method-->"+i);
list.stream().forEach(consumer);
Consumer<Integer> consumer1 = (i)->System.out.println("Iterating elements parallee stream -->"+i);
list.parallelStream().forEach(consumer1);
}
}
package com.java8.features.foreach;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
public class foreachOrderedEx {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1,2,3,4,5);
Consumer<Integer> consumer= (i)->System.out.println("iterating elements using forEachOrdered method-->"+i);
list.stream().forEachOrdered(consumer);
Consumer<Integer> consumer1 = (i)->System.out.println("Iterating elements parallel stream -->"+i);
list.parallelStream().forEachOrdered(consumer1);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment