Java8 was
released in March 2014 and comes up with very interesting features like lambda
expressions, streaming, collectors, writing applications that use multi core
functionality. By using all these features, you can write programs in less
number of lines and effectively.
For example
Prior to
java8, to sort list of employee objects. You have to write following code.
Collections.sort(employees,
new Comparator<Employee>() {
@Override
public int compare(Employee emp1,
Employee emp2) {
return
emp1.getFirstName().compareTo(emp2.getFirstName());
}
});
Same
functionality can be achieved using following snippet in Java8.
employees.sort((employee1,
employee2) -> employee1.getFirstName().compareTo(employee2.getFirstName()));
(or)
employees.sort(comparing(Employee::getFirstName));
Find
complete application below.
import java.util.Objects; public class Employee { private int id; private String firstName; private String lastName; private int age; private String city; public Employee(int id, String firstName, String lastName, int age, String city) { super(); this.id = id; this.firstName = firstName; this.lastName = lastName; this.age = age; this.city = city; } public int getId() { return id; } public void setId(int 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 int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } @Override public boolean equals(Object employee) { if (Objects.isNull(employee)) return false; if (!(employee instanceof Employee)) return false; Employee emp = (Employee) employee; return id == emp.id; } @Override public int hashCode() { return Objects.hash(id, firstName, lastName, age); } @Override public String toString() { return String.format("%s(%s,%d)", firstName, city, age); } }
import static java.util.Comparator.comparing; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class SortEmployee { public static void main(String args[]) { Employee emp1 = new Employee(1, "Hari Krishna", "Gurram", 26, "Bangalore"); Employee emp2 = new Employee(2, "Joel", "Chelli", 27, "Hyderabad"); Employee emp3 = new Employee(3, "Shanmukh", "Kummary", 28, "Chennai"); Employee emp4 = new Employee(4, "Harika", "Raghuram", 27, "Chennai"); Employee emp5 = new Employee(5, "Sudheer", "Ganji", 27, "Bangalore"); Employee emp6 = new Employee(6, "Rama Krishna", "Gurram", 27, "Bangalore"); Employee emp7 = new Employee(7, "PTR", "PTR", 27, "Hyderabad"); Employee emp8 = new Employee(8, "Siva krishna", "Ponnam", 28, "Hyderabad"); List<Employee> employees = new ArrayList<>(); employees.add(emp1); employees.add(emp2); employees.add(emp3); employees.add(emp4); employees.add(emp5); employees.add(emp6); employees.add(emp7); employees.add(emp8); List<Employee> employeesList1 = new ArrayList<>(employees); List<Employee> employeesList2 = new ArrayList<>(employees); /* Prior to Java8 */ Collections.sort(employees, new Comparator<Employee>() { @Override public int compare(Employee emp1, Employee emp2) { return emp1.getFirstName().compareTo(emp2.getFirstName()); } }); /* Java8 style 1 */ employeesList1.sort((employee1, employee2) -> employee1.getFirstName() .compareTo(employee2.getFirstName())); /* Java8 style 2 */ employeesList2.sort(comparing(Employee::getFirstName)); System.out.println(employees +"\n"); System.out.println(employeesList1 +"\n"); System.out.println(employeesList2 +"\n"); } }
Output
[Hari Krishna(Bangalore,26), Harika(Chennai,27), Joel(Hyderabad,27), PTR(Hyderabad,27), Rama Krishna(Bangalore,27), Shanmukh(Chennai,28), Siva krishna(Hyderabad,28), Sudheer(Bangalore,27)] [Hari Krishna(Bangalore,26), Harika(Chennai,27), Joel(Hyderabad,27), PTR(Hyderabad,27), Rama Krishna(Bangalore,27), Shanmukh(Chennai,28), Siva krishna(Hyderabad,28), Sudheer(Bangalore,27)] [Hari Krishna(Bangalore,26), Harika(Chennai,27), Joel(Hyderabad,27), PTR(Hyderabad,27), Rama Krishna(Bangalore,27), Shanmukh(Chennai,28), Siva krishna(Hyderabad,28), Sudheer(Bangalore,27)]
Lets start
exploring features on Java8 one by one.
No comments:
Post a Comment