Commit db7753dd authored by Uma Vani Ravuri's avatar Uma Vani Ravuri

java8 code practies

parents
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Java8PractiesProgram</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8
package com.java8.practies;
import java.util.Comparator;
import java.util.List;
public class Employee implements Comparator<Employee>{
String id;
String firstName;
String lastName;
String departmentNames;
public Employee() {
super();
// TODO Auto-generated constructor stub
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getDepartmentNames() {
return departmentNames;
}
public void setDepartmentNames(String departmentNames) {
this.departmentNames = departmentNames;
}
public Employee(String id, String firstName, String lastName, String departmentNames) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.departmentNames = departmentNames;
}
@Override
public String toString() {
return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", departmentNames="
+ departmentNames + "]";
}
@Override
public int compare(Employee o1, Employee o2) {
// TODO Auto-generated method stub
return o1.firstName.compareTo(o2.firstName);
}
}
package com.java8.practies;
import java.util.Comparator;
public class EmployeeComparator implements Comparator<Employee> {
@Override
public int compare(Employee o1, Employee o2) {
return o1.getFirstName().compareTo(o2.getFirstName());
}
}
package com.java8.practies;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class TestEmployee {
public static void main(String[] args) {
ArrayList<Employee> list = new ArrayList<Employee>();
list.add(new Employee("1", "Uma", "Vani", "IT"));
list.add(new Employee("2", "Sujit", "Krishnan", "CSE"));
list.add(new Employee("3", "Brayan", "Bra", "IT"));
list.add(new Employee("4", "Allice", "Ali", "CSE"));
list.add(new Employee("5", "Sco te", "Scote", "IT"));
list.add(new Employee("6", "Rajesh", "Krishnan", "ECE"));
list.add(new Employee("7", "Ramana", "Raju", "IT"));
list.add(new Employee("8", "Raju", "Krishnan", "EEE"));
list.add(new Employee("9", "Rishi", "Vani", "IT"));
list.add(new Employee("10", "Dattu", "Krishnan", "EEE"));
//1)list of employees, get the first employee from the list and return his fullName
list.stream().limit(1)
.forEach(x -> System.out.println("First employee Full Name is::" + x.firstName + x.lastName));
//given a list of employees. Can you create a map with the count of employees each department has ?
//with key as department name and count of employees as value.
Map<String, Long> mapDepartment = list.stream()
.collect(Collectors.groupingBy(Employee::getDepartmentNames, Collectors.counting()));
mapDepartment.forEach((k, v) -> System.out.println((k + ":" + v)));
//Given a map with the department name as key and value as list of employees belonging to that department.
//when a search string is given, need to list out the employees whose firstname or lastname is matching(match should be case insensitive)
Map<String, List<Employee>> mapEmployee = list.stream()
.collect(Collectors.groupingBy(Employee::getDepartmentNames, Collectors.toList()));
mapEmployee.forEach((k, v) -> System.out.println((k + ":" + v)));
//Consider a list of employees, and if a department name is given as argument, list out the employees which doesn't belong to that department.
List<Employee> filterEmp = list.stream().filter(x -> !x.getDepartmentNames().equals("IT"))
.collect(Collectors.toList());
filterEmp.forEach(x -> System.out.println(" Employee list whose Department is not IT" + x));
// 6)Consider a list of employees, sort the employees by their firstName and
// return the sorted list of employees.
List<Employee> sortedEmployee = list.stream().sorted(new EmployeeComparator()).collect(Collectors.toList());
sortedEmployee.forEach(x -> System.out.println("Sorted Employee based on firstName" + x));
//Consider a list of employees, return the employee whose empId is highest
Employee e = list.stream().max((e1, e2) -> e1.getId().compareTo(e2.getId())).get();
System.out.println("Maximum Id Employee details" + e);
//Consider a list of employees, concat the fullName of all the employees with pipe (|) and return the concatenated string.
String joinedString = list.stream().map(x -> x.firstName + x.lastName).collect(Collectors.joining("|"));
System.out.println("Maximum Id Employee details-----------" + joinedString);
//Consider a list of 10 employees, get the 8th employee and print his full name and department name.
list.stream().sorted((e1, e2) -> e1.getId().compareTo(e2.getId())).skip(8).limit(1)
.forEach(System.out::println);
//consider list of employees and list of employee ids. return the list of employees matching with the given list of employee ids.
List<Employee> checkForMatches = new ArrayList<>();
checkForMatches.add(new Employee("2", "siri", "raja", "CSE"));
checkForMatches.add(new Employee("7", "roja", "rani", "CSE"));
checkForMatches.add(new Employee("11", "roja", "rani", "CSE"));
List<Employee> filteredList = list.stream()
.filter(employee -> checkForMatches.stream().anyMatch(dept -> employee.getId().equals(dept.getId())))
.collect(Collectors.toList());
filteredList.forEach(x -> System.out.println("Employee matches" + x));
//Store ids are of 4 digit strings.if length of given storeId is less than 4 digits,you need to prefix with zeros and return a 4 digit storeId.
list.stream().forEach(x->System.out.println("4 Digits employee Id::"+String.format("%04d", Integer.parseInt(x.id))));
}
}
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