Skip to content

Add LambdaExpressionUtils.java – Unique Lambda-Based Helper Functions #6288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Functional/LambdaExpressionUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* LambdaExpressionUtils.java
* Unique Java utility functions using Lambda expressions.
* Demonstrates Function, Predicate, Consumer, and Supplier.
*/

import java.util.Random;
import java.util.function.*;

public class LambdaExpressionUtils {

public static void main(String[] args) {
// Reverse a string
Function<String, String> reverse = str -> new StringBuilder(str).reverse().toString();
System.out.println("Reversed: " + reverse.apply("lambda"));

// Check palindrome
Predicate<String> isPalindrome = str -> str.equalsIgnoreCase(new StringBuilder(str).reverse().toString());
System.out.println("Is Palindrome: " + isPalindrome.test("madam"));

// Print message in all caps with exclamation
Consumer<String> shout = s -> System.out.println(s.toUpperCase() + "!");
shout.accept("functional interface");

// Check if number is even
Function<Integer, Boolean> isEven = n -> n % 2 == 0;
System.out.println("Is 10 even? " + isEven.apply(10));

// Get random greeting
Supplier<String> randomGreeting = () -> {
String[] greetings = {"Hello", "Hi", "Hey", "Hola"};
return greetings[new Random().nextInt(greetings.length)];
};
System.out.println("Random Greeting: " + randomGreeting.get());
}

}
38 changes: 38 additions & 0 deletions Search/BinarySearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* BinarySearch.java
* This program implements recursive Binary Search on a sorted array.
* Time Complexity: O(log n)
*/

public class BinarySearch {

// Recursive method to perform binary search
public static int binarySearch(int[] arr, int left, int right, int target) {
if (left > right) {
return -1; // Base case: element not found
}

int mid = left + (right - left) / 2;

if (arr[mid] == target) {
return mid; // Found the target
} else if (target < arr[mid]) {
return binarySearch(arr, left, mid - 1, target); // Search in the left half
} else {
return binarySearch(arr, mid + 1, right, target); // Search in the right half
}
}

public static void main(String[] args) {
int[] arr = {3, 8, 15, 23, 42, 56};
int target = 23;

int result = binarySearch(arr, 0, arr.length - 1, target);

if (result != -1) {
System.out.println("Target " + target + " found at index: " + result);
} else {
System.out.println("Target " + target + " not found in the array.");
}
}
}
30 changes: 30 additions & 0 deletions Search/LinearSearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* LinearSearch.java
* This program implements the Linear Search algorithm.
* Time Complexity: O(n)
*/

public class LinearSearch {

public static int linearSearch(int[] arr, int target) {
// Traverse each element in the array
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
return i; // Found the target at index i
}
}
return -1; // Target not found
}

public static void main(String[] args) {
int[] numbers = {5, 8, 12, 20, 35};
int target = 20;

int index = linearSearch(numbers, target);
if (index != -1) {
System.out.println("Target " + target + " found at index: " + index);
} else {
System.out.println("Target " + target + " not found in the array.");
}
}
}
40 changes: 40 additions & 0 deletions Sorting/BubbleSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* BubbleSort.java
* This program implements the Bubble Sort algorithm.
* Time Complexity: O(n^2)
*/

public class BubbleSort {

public static void bubbleSort(int[] arr) {
int n = arr.length;
boolean swapped;
for (int i = 0; i < n - 1; i++) {
swapped = false;

for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// swap arr[j] and arr[j+1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;

swapped = true;
}
}

// If no two elements were swapped, array is sorted
if (!swapped)
break;
}
}

public static void main(String[] args) {
int[] arr = {64, 25, 12, 22, 11};
bubbleSort(arr);
System.out.println("Sorted array:");
for (int num : arr) {
System.out.print(num + " ");
}
}
}
33 changes: 33 additions & 0 deletions Sorting/InsertionSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* InsertionSort.java
* This program implements the Insertion Sort algorithm.
* Time Complexity: O(n^2) in worst case, O(n) in best case (already sorted).
*/

public class InsertionSort {

public static void insertionSort(int[] arr) {
// Loop from the second element to the end
for (int i = 1; i < arr.length; i++) {
int key = arr[i]; // Store the current element to be inserted
int j = i - 1;

// Move elements of arr[0..i-1], that are greater than key,
// to one position ahead of their current position
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j]; // Shift element to the right
j = j - 1;
}
arr[j + 1] = key; // Insert the key into its correct position
}
}

public static void main(String[] args) {
int[] arr = {29, 10, 14, 37, 13};
insertionSort(arr);
System.out.println("Sorted array:");
for (int num : arr) {
System.out.print(num + " ");
}
}
}
69 changes: 69 additions & 0 deletions Sorting/MergeSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* MergeSort.java
* This program implements the Merge Sort algorithm using recursion.
* Time Complexity: O(n log n)
*/

public class MergeSort {

// Method to recursively divide and sort the array
public static void mergeSort(int[] arr, int left, int right) {
if (left < right) {
int mid = (left + right) / 2;

// Recursively sort the left half
mergeSort(arr, left, mid);

// Recursively sort the right half
mergeSort(arr, mid + 1, right);

// Merge the two sorted halves
merge(arr, left, mid, right);
}
}

// Method to merge two sorted halves
public static void merge(int[] arr, int left, int mid, int right) {
// Sizes of subarrays
int n1 = mid - left + 1;
int n2 = right - mid;

// Temp arrays
int[] L = new int[n1];
int[] R = new int[n2];

// Copy data to temp arrays
for (int i = 0; i < n1; ++i)
L[i] = arr[left + i];
for (int j = 0; j < n2; ++j)
R[j] = arr[mid + 1 + j];

// Merge temp arrays back into arr
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k++] = L[i++];
} else {
arr[k++] = R[j++];
}
}

// Copy remaining elements of L[]
while (i < n1)
arr[k++] = L[i++];

// Copy remaining elements of R[]
while (j < n2)
arr[k++] = R[j++];
}

public static void main(String[] args) {
int[] arr = {38, 27, 43, 3, 9, 82, 10};
mergeSort(arr, 0, arr.length - 1);

System.out.println("Sorted array:");
for (int value : arr) {
System.out.print(value + " ");
}
}
}
51 changes: 51 additions & 0 deletions Strings/AnagramChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* AnagramChecker.java
* Checks whether two input strings are anagrams using a HashMap.
* Time Complexity: O(n), Space Complexity: O(n)
*/

import java.util.HashMap;

public class AnagramChecker {

public static boolean areAnagrams(String str1, String str2) {
// Remove spaces and convert to lowercase
str1 = str1.replaceAll("\\s", "").toLowerCase();
str2 = str2.replaceAll("\\s", "").toLowerCase();

// Quick length check
if (str1.length() != str2.length()) {
return false;
}

// Count characters in first string
HashMap<Character, Integer> map = new HashMap<>();
for (char c : str1.toCharArray()) {
map.put(c, map.getOrDefault(c, 0) + 1);
}

// Subtract character counts using second string
for (char c : str2.toCharArray()) {
if (!map.containsKey(c)) {
return false;
}
map.put(c, map.get(c) - 1);
if (map.get(c) == 0) {
map.remove(c);
}
}

return map.isEmpty();
}

public static void main(String[] args) {
String s1 = "Listen";
String s2 = "Silent";

if (areAnagrams(s1, s2)) {
System.out.println("\"" + s1 + "\" and \"" + s2 + "\" are anagrams.");
} else {
System.out.println("\"" + s1 + "\" and \"" + s2 + "\" are NOT anagrams.");
}
}
}