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 code
import 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 code
import 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 code
import 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 code
import 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 code
import 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 code
import 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.
Start the conversation!
Be the first to share your thoughts
Quick answers to common questions about our courses, quizzes, and learning platform