Introduction
Sorting an array is a fundamental task in programming. Java provides built-in methods to sort arrays efficiently. In Java 8, you can sort arrays using both traditional methods and the new Stream API. This guide will cover how to sort an array using Java 8.
Problem Statement
Write a Java program that:
- Takes an array of elements as input.
- Sorts the array in ascending and descending order.
- Displays the sorted array.
Example:
- Input:
int[] array = {5, 2, 9, 1, 3}
- Output (Ascending):
[1, 2, 3, 5, 9]
- Output (Descending):
[9, 5, 3, 2, 1]
Solution Steps
- Define the Input Array: Define the array that needs to be sorted.
- Sort in Ascending Order: Use the
Arrays.sort()
method or Streams to sort the array. - Sort in Descending Order: Use
Arrays.sort()
with a custom comparator or reverse a sorted stream. - Display the Sorted Array: Print the sorted array.
Method 1: Using Arrays.sort()
for Ascending Order
Java Program
import java.util.Arrays;
public class ArraySortExample1 {
public static void main(String[] args) {
// Step 1: Define the input array
int[] array = {5, 2, 9, 1, 3};
// Step 2: Sort the array in ascending order using Arrays.sort()
Arrays.sort(array);
// Step 3: Display the sorted array
System.out.println("Sorted array in ascending order: " + Arrays.toString(array));
}
}
Output
Sorted array in ascending order: [1, 2, 3, 5, 9]
Explanation
- The
Arrays.sort()
method sorts the array in place in ascending order. Arrays.toString(array)
is used to display the array as a string.
Method 2: Using Java 8 Streams for Ascending and Descending Order
Java Program
import java.util.Arrays;
import java.util.Comparator;
import java.util.stream.Collectors;
public class ArraySortExample2 {
public static void main(String[] args) {
// Step 1: Define the input array
Integer[] array = {5, 2, 9, 1, 3};
// Step 2: Sort the array in ascending order using Streams
Integer[] sortedArrayAsc = Arrays.stream(array)
.sorted() // Sort in ascending order
.toArray(Integer[]::new); // Convert Stream back to array
// Step 3: Display the sorted array in ascending order
System.out.println("Sorted array in ascending order: " + Arrays.toString(sortedArrayAsc));
// Step 4: Sort the array in descending order using Streams
Integer[] sortedArrayDesc = Arrays.stream(array)
.sorted(Comparator.reverseOrder()) // Sort in descending order
.toArray(Integer[]::new); // Convert Stream back to array
// Step 5: Display the sorted array in descending order
System.out.println("Sorted array in descending order: " + Arrays.toString(sortedArrayDesc));
}
}
Output
Sorted array in ascending order: [1, 2, 3, 5, 9]
Sorted array in descending order: [9, 5, 3, 2, 1]
Explanation
- Sorting in Ascending Order: The
sorted()
method sorts the stream elements in natural order (ascending). - Sorting in Descending Order: The
sorted(Comparator.reverseOrder())
sorts the stream elements in reverse order (descending). - The stream is converted back to an array using
toArray(Integer[]::new)
.
Note: We used Integer[]
instead of int[]
because the Stream API works with reference types, not primitives.
Method 3: Using Arrays.sort()
with a Custom Comparator (Descending Order)
Java Program
import java.util.Arrays;
import java.util.Collections;
public class ArraySortExample3 {
public static void main(String[] args) {
// Step 1: Define the input array
Integer[] array = {5, 2, 9, 1, 3};
// Step 2: Sort the array in descending order using Arrays.sort() and Collections.reverseOrder()
Arrays.sort(array, Collections.reverseOrder());
// Step 3: Display the sorted array in descending order
System.out.println("Sorted array in descending order: " + Arrays.toString(array));
}
}
Output
Sorted array in descending order: [9, 5, 3, 2, 1]
Explanation
Arrays.sort(array, Collections.reverseOrder())
sorts the array in descending order using a custom comparator (reverseOrder()
).Arrays.toString(array)
displays the sorted array.
Output Example
Example 1:
For the input int[] array = {5, 2, 9, 1, 3}
, the output is:
Sorted array in ascending order: [1, 2, 3, 5, 9]
Sorted array in descending order: [9, 5, 3, 2, 1]
Example 2:
For the input Integer[] array = {10, 7, 8, 6, 3}
, the output is:
Sorted array in ascending order: [3, 6, 7, 8, 10]
Sorted array in descending order: [10, 8, 7, 6, 3]
Conclusion
In Java 8, you can sort arrays using the Arrays.sort()
method for basic sorting in ascending order, or you can use Streams for more flexibility, including sorting in descending order. Both approaches are efficient and easy to implement, and you can choose the one that best fits your use case.
Comments
Post a Comment
Leave Comment