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.


Disclaimer:-

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.

12. Advanced

Comments: 0

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?