Skip to content

Commit fc5fe72

Browse files
committed
feat(LambdaExpressionDemo): add demo of sorting collections using lambdas
What - Added LambdaExpressionDemo class. - Created a List<Integer> with values [2, 3, 1]. - Sorted integers in descending order using a lambda comparator (b - a). - Created a List<String> with values ["banana", "apple", "date"]. - Sorted strings by length using a lambda comparator (a.length() - b.length()). - Printed sorted results. Why - Demonstrates how to use lambda expressions to define custom comparators inline. - Replaces verbose anonymous classes with concise, readable lambdas. - Shows sorting of both numeric and string data with different criteria. How - list.sort((a, b) -> b - a): - Subtracts a from b to achieve descending order. - words.sort((a, b) -> a.length() - b.length()): - Compares based on string length, resulting in ascending order by length. - Print the lists after sorting to verify order. Logic - Inputs: - list = [2, 3, 1]. - words = ["banana", "apple", "date"]. - Outputs: - Integers sorted descending → [3, 2, 1]. - Strings sorted by length ascending → ["date", "apple", "banana"]. - Flow: 1. Populate lists. 2. Apply sort() with lambdas. 3. Print sorted collections. - Edge cases: - If comparator returns 0, elements considered equal (order may remain stable). - If lists are empty, sort() is a no-op. - Complexity: O(n log n) sorting. - Concurrency: Not thread-safe while sorting shared mutable lists. - Error handling: Not needed here. Real-life applications - Sorting employees by salary, then by name. - Sorting files by size or timestamp. - Sorting strings by custom metrics (length, frequency of characters, etc.). - Inline comparators reduce boilerplate in such tasks. Notes - Lambdas improve readability compared to explicit Comparator classes. - For clarity, use Comparator.comparing(...) when criteria are more complex. - Arithmetic-based comparator (b - a) works for small integers but may overflow; safer is Integer.compare(b, a). Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent d57d6c3 commit fc5fe72

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

Section25CollectionFramework/src/ListDemo/ArrayListLinkedListStack/ListEDSortUsingLamda.java renamed to Section 25 Collections Frameworks/List Interface/ArrayList/Lamda Expression/src/LambdaExpressionDemo.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
package ListDemo.ArrayListLinkedListStack;
2-
31
import java.util.ArrayList;
42
import java.util.Arrays;
53
import java.util.List;
64

7-
public class ListEDSortUsingLamda {
5+
public class LambdaExpressionDemo {
86
public static void main(String[] args) {
97
List<Integer> list = new ArrayList<>();
108
list.add(2);
@@ -18,4 +16,4 @@ public static void main(String[] args) {
1816
words.sort((a,b)->a.length()-b.length());
1917
System.out.println("Sorted strings by length: " + words);
2018
}
21-
}
19+
}

0 commit comments

Comments
 (0)