Java Stream Examples

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 83:-  Java Stream Examples

Here are some Java stream examples that demonstrate various stream operations:

  1. 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]    } }

  2. 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]    } }

  3. 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]    } }

  4. 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    } }

  5. 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]    } }

  6. 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.

12. Advanced

0 Comments

Start the conversation!

Be the first to share your thoughts

Frequently Asked Questions About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support