If you have any query feel free to chat us!
Happy Coding! Happy Learning!
Here are some Java stream examples that demonstrate various stream operations:
Filtering and Collecting:
javaCopy codeimport java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class StreamFilterExample {
public static void main(String[] args) {
List<String> fruits = Arrays.asList("apple", "banana", "cherry", "orange", "grape");
// Filter fruits that start with 'a' and collect them into a new list
List<String> filteredFruits = fruits.stream()
.filter(fruit -> fruit.startsWith("a"))
.collect(Collectors.toList());
System.out.println(filteredFruits); // Output: [apple]
}
}
Mapping:
javaCopy codeimport java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class StreamMapExample {
public static void main(String[] args) {
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
// Map names to their lengths and collect the lengths into a new list
List<Integer> nameLengths = names.stream()
.map(name -> name.length())
.collect(Collectors.toList());
System.out.println(nameLengths); // Output: [5, 3, 7]
}
}
Sorting:
javaCopy codeimport java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class StreamSortExample {
public static void main(String[] args) {
List<String> fruits = Arrays.asList("apple", "banana", "cherry", "orange", "grape");
// Sort fruits in alphabetical order and collect them into a new list
List<String> sortedFruits = fruits.stream()
.sorted()
.collect(Collectors.toList());
System.out.println(sortedFruits); // Output: [apple, banana, cherry, grape, orange]
}
}
Aggregation:
javaCopy codeimport java.util.Arrays;
import java.util.List;
public class StreamAggregationExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
// Calculate the sum of all numbers
int sum = numbers.stream()
.reduce(0, (a, b) -> a + b);
System.out.println("Sum: " + sum); // Output: Sum: 15
}
}
Distinct Elements:
javaCopy codeimport java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class StreamDistinctExample {
public static void main(String[] args) {
List<String> colors = Arrays.asList("red", "blue", "green", "red", "yellow");
// Find distinct colors and collect them into a new list
List<String> distinctColors = colors.stream()
.distinct()
.collect(Collectors.toList());
System.out.println(distinctColors); // Output: [red, blue, green, yellow]
}
}
Counting:
javaCopy codeimport java.util.Arrays;
import java.util.List;
public class StreamCountExample {
public static void main(String[] args) {
List<String> fruits = Arrays.asList("apple", "banana", "cherry", "orange", "grape");
// Count the number of fruits in the list
long count = fruits.stream()
.count();
System.out.println("Count: " + count); // Output: Count: 5
}
}
These examples showcase some common operations that can be performed using Java streams. The stream API provides a powerful and expressive way to manipulate data in a functional and declarative style. It is particularly useful for processing large datasets and simplifying complex data manipulation tasks.
Under Section 107 of the copyright act 1976, allowance is made for fair use for purposes such as criticism, comment, news reporting, scholarship, and research. Fair use is a use permitted by copyright statute that might otherwise be infringing. Non-profit, educational, or personal use tips the balance in favor of fair use.
Comments: 0