Skip to content

Commit 8f5b91c

Browse files
author
Rajeev Kumar Singh
committed
Comparator and Comparable
1 parent d6cd880 commit 8f5b91c

File tree

8 files changed

+141
-7
lines changed

8 files changed

+141
-7
lines changed

java-arraylist-examples/src/IterateOverArrayListExample.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static void main(String[] args) {
1111
tvShows.add("Friends");
1212
tvShows.add("Prison break");
1313

14-
System.out.println("=== Iterate using Java 8 forEach loop ===");
14+
System.out.println("=== Iterate using Java 8 forEach and lambda ===");
1515
tvShows.forEach(tvShow -> {
1616
System.out.println(tvShow);
1717
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import java.time.LocalDate;
2+
import java.util.ArrayList;
3+
import java.util.Collections;
4+
import java.util.Comparator;
5+
import java.util.List;
6+
7+
public class ComparableExample {
8+
public static void main(String[] args) {
9+
List<Employee> employees = new ArrayList<>();
10+
11+
employees.add(new Employee(1010, "Rajeev", 100000.00, LocalDate.of(2010, 7, 10)));
12+
employees.add(new Employee(1004, "Chris", 95000.50, LocalDate.of(2017, 3, 19)));
13+
employees.add(new Employee(1007, "David", 134000.00, LocalDate.of(2017, 9, 28)));
14+
15+
System.out.println("Employees (Before Sorting) : " + employees);
16+
17+
// This will use the `compareTo()` method of the `Employee` class to compare two employees and sort them.
18+
Collections.sort(employees);
19+
20+
System.out.println("Employees (After Sorting) : " + employees);
21+
22+
// This will use the same `compareTo()` method of the `Employee` class but will reverse the order
23+
Collections.sort(employees, Comparator.reverseOrder());
24+
System.out.println("Employees (After Sorting with reverseOrder)" + employees);
25+
}
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.time.LocalDate;
2+
import java.util.ArrayList;
3+
import java.util.Collections;
4+
import java.util.Comparator;
5+
import java.util.List;
6+
7+
public class ComparatorExample {
8+
public static void main(String[] args) {
9+
List<Employee> employees = new ArrayList<>();
10+
11+
employees.add(new Employee(1010, "Rajeev", 100000.00, LocalDate.of(2010, 7, 10)));
12+
employees.add(new Employee(1004, "Chris", 95000.50, LocalDate.of(2017, 3, 19)));
13+
employees.add(new Employee(1015, "David", 134000.00, LocalDate.of(2017, 9, 28)));
14+
employees.add(new Employee(1009, "Steve", 100000.00, LocalDate.of(2016, 5, 18)));
15+
16+
System.out.println("Employees : " + employees);
17+
18+
// Sort employees by Name
19+
Collections.sort(employees, Comparator.comparing(Employee::getName));
20+
System.out.println("\nEmployees (Sorted by Name) : " + employees);
21+
22+
// Sort employees by Salary
23+
Collections.sort(employees, Comparator.comparingDouble(Employee::getSalary));
24+
System.out.println("\nEmployees (Sorted by Salary) : " + employees);
25+
26+
// Sort employees by JoiningDate
27+
Collections.sort(employees, Comparator.comparing(Employee::getJoiningDate));
28+
System.out.println("\nEmployees (Sorted by JoiningDate) : " + employees);
29+
30+
// Sort employees by descending order of Name
31+
Collections.sort(employees, Comparator.comparing(Employee::getName).reversed());
32+
System.out.println("\nEmployees (Sorted by Name Descending Order) : " + employees);
33+
34+
// Chaining multiple Comparators
35+
// Sort by Salary. If Salary is same then sort by Name
36+
Collections.sort(employees, Comparator.comparingDouble(Employee::getSalary).thenComparing(Employee::getName));
37+
System.out.println("\nEmployees (Sorted by Salary and Id) : " + employees);
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import java.time.LocalDate;
2+
3+
class Employee implements Comparable<Employee> {
4+
private int id;
5+
private String name;
6+
private double salary;
7+
private LocalDate joiningDate;
8+
9+
public Employee(int id, String name, double salary, LocalDate joiningDate) {
10+
this.id = id;
11+
this.name = name;
12+
this.salary = salary;
13+
this.joiningDate = joiningDate;
14+
}
15+
16+
public int getId() {
17+
return id;
18+
}
19+
20+
public void setId(int id) {
21+
this.id = id;
22+
}
23+
24+
public String getName() {
25+
return name;
26+
}
27+
28+
public void setName(String name) {
29+
this.name = name;
30+
}
31+
32+
public double getSalary() {
33+
return salary;
34+
}
35+
36+
public void setSalary(double salary) {
37+
this.salary = salary;
38+
}
39+
40+
public LocalDate getJoiningDate() {
41+
return joiningDate;
42+
}
43+
44+
public void setJoiningDate(LocalDate joiningDate) {
45+
this.joiningDate = joiningDate;
46+
}
47+
48+
// Compare Two Employees based on their ID
49+
/**
50+
* @param anotherEmployee - The Employee to be compared.
51+
* @return A negative integer, zero, or a positive integer as this employee
52+
* is less than, equal to, or greater than the supplied employee object.
53+
*/
54+
@Override
55+
public int compareTo(Employee anotherEmployee) {
56+
return this.getId() - anotherEmployee.getId();
57+
}
58+
59+
@Override
60+
public String toString() {
61+
return "Employee{" +
62+
"id=" + id +
63+
", name='" + name + '\'' +
64+
", salary=" + salary +
65+
", joiningDate=" + joiningDate +
66+
'}';
67+
}
68+
}

java-hashmap-examples/src/IterateOverHashMap.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static void main(String[] args) {
1111
employeeSalary.put("Mark", 95000.00);
1212
employeeSalary.put("Steven", 134000.00);
1313

14-
System.out.println("=== Iterating over a HashMap using Java 8 forEach loop ===");
14+
System.out.println("=== Iterating over a HashMap using Java 8 forEach and lambda ===");
1515
employeeSalary.forEach((employee, salary) -> {
1616
System.out.println(employee + " => " + salary);
1717
});
@@ -24,7 +24,7 @@ public static void main(String[] args) {
2424
System.out.println(entry.getKey() + " => " + entry.getValue());
2525
}
2626

27-
System.out.println("\n=== Iterating over the HashMap's entrySet using Java 8 forEach loop ===");
27+
System.out.println("\n=== Iterating over the HashMap's entrySet using Java 8 forEach and lambda ===");
2828
employeeSalary.entrySet().forEach(entry -> {
2929
System.out.println(entry.getKey() + " => " + entry.getValue());
3030
});

java-hashset-examples/src/IterateOverHashSetExample.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static void main(String[] args) {
1212
programmingLanguages.add("PHP");
1313
programmingLanguages.add("Ruby");
1414

15-
System.out.println("=== Iterate over a HashSet using Java 8 forEach loop ===");
15+
System.out.println("=== Iterate over a HashSet using Java 8 forEach and lambda ===");
1616
programmingLanguages.forEach(programmingLanguage -> {
1717
System.out.println(programmingLanguage);
1818
});

java-linkedhashmap-examples/src/IterateOverLinkedHashMapExample.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ public static void main(String[] args) {
1111
userCityMapping.put("David", "Paris");
1212
userCityMapping.put("Jesse", "California");
1313

14-
System.out.println("=== Iterating over a LinkedHashMap using Java 8 forEach loop ===");
14+
System.out.println("=== Iterating over a LinkedHashMap using Java 8 forEach and lambda ===");
1515
userCityMapping.forEach((user, city) -> {
1616
System.out.println(user + " => " + city);
1717
});
1818

1919

20-
System.out.println("\n=== Iterating over the LinkedHashMap's entrySet using Java 8 forEach loop ===");
20+
System.out.println("\n=== Iterating over the LinkedHashMap's entrySet using Java 8 forEach and lambda ===");
2121
userCityMapping.entrySet().forEach(entry -> {
2222
System.out.println(entry.getKey() + " => " + entry.getValue());
2323
});

java-linkedlist-examples/src/IterateOverLinkedListExample.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static void main(String[] args) {
1111
humanSpecies.add("Homo Erectus");
1212
humanSpecies.add("Home Habilis");
1313

14-
System.out.println("=== Iterate over a LinkedList using Java 8 forEach loop ===");
14+
System.out.println("=== Iterate over a LinkedList using Java 8 forEach and lambda ===");
1515
humanSpecies.forEach(name -> {
1616
System.out.println(name);
1717
});

0 commit comments

Comments
 (0)