Author: Editorial Team
-
Java program to find the second largest number in an integer array
Output: Using reduce() (Without Sorting) This approach finds the largest and second largest in one pass, making it more efficient (O(n) time complexity).
-
Java 8 program to get the three maximum and three minimum numbers from a given list of integers
You can use Java 8 Streams to get the three maximum and three minimum numbers from a given list of integers by using sorted(), limit(), and distinct() if needed. Output: Explanation: Handling Duplicates If you want unique numbers, use distinct() before sorting:
-
Merge two unsorted arrays into a single sorted array without duplicates
You can merge two unsorted arrays into a single sorted array without duplicates using Java 8 Streams. The key steps are: For Integer Object Arrays (Integer[]) Output: For Primitive int Arrays (int[]): Since IntStream doesn’t support distinct() on boxed values directly, use: Output:
-
Merge two unsorted arrays into a single sorted array using Java 8 streams
You can merge two unsorted arrays and sort them using Java 8 Streams with Stream.concat() and sorted(). Output: Explanation: Descending Order If you want the merged array sorted in reverse order, just use:
-
Java 8 program to find the maximum and minimum of a list of integers
You can use Java 8 Streams to find the maximum and minimum values from a list of integers using the max() and min() methods with Comparator.naturalOrder(). Output: Alternative Using mapToInt (Primitive Stream):
-
Java program to print the numbers from a given list of integers that are multiples of 5
You can use Java 8 Streams to filter and print numbers that are multiples of 5 using the filter() method. Output:
-
Java program to join a list of strings with ‘[‘ as prefix, ‘]’ as suffix, and ‘,’ as delimiter
You can use Java 8 Streams with Collectors.joining() to join a list of strings with a specified prefix, suffix, and delimiter. Output: Explanation:
-
Sort a given list of decimals in reverse order in java 8
You can sort a list of decimal numbers in reverse order using Java 8 Streams and sorted(Comparator.reverseOrder()). Output: Explanation: This approach is efficient and works for Double, Float, or BigDecimal.
-
Find the frequency of each element in an array or a list
You can find the frequency of each element in an array or list using Java 8 Streams and the Collectors.groupingBy() collector. Example 1: Finding Frequency in a List Output: Example 2: Finding Frequency in an Array Output:
-
Find the frequency of each character in a string using Java 8 streams
You can use Java 8 Streams to count the frequency of each character in a string by using the Collectors.groupingBy() collector. Here’s how: Output: Explanation:
