Java – How to use Stream Filter in Java 8 with List & Array Examples
Java
215
java8
10
stream
11
filter
7
Male avatar

loveprogramming viết ngày 20/05/2021

https://ozenero.com/java-how-to-use-stream-filter-in-java-8-with-list-array-examples

Java – How to use Stream Filter in Java 8 with List & Array Examples

Java 8 provides an extremely powerful abstract concept Stream with many useful mechanics for consuming and processing data in Java Collection. In the tutorial, We will use lots of examples to explore more the helpful of Stream API with filtering function on the specific topic: "Filter Collection with Java 8 Stream".

What will we do?

  • How to filter List with traditional approach?
  • Use Java 8 Stream to filter List and Array Objects
  • Apply filter() function with other util functions of Stream API in practice

Now let's do examples for more details!

Related posts:

Java Filter List by Traditional Approach with Looping

Before Java 8, for filtering a List Objects, we use the basic approach with looping technical solution.

Looping Example - Filter Integer List

  • Example: How to get all even number in a list?

List intList = Arrays.asList(1, 2, 3, 4, 5, 7, 10, 11, 16);

for(Integer i: intList) {
    if(i%2==0) {
        System.out.println(i);
    }
}
/*
    2
    4
    10
    16
 */

Looping Example - Filter String List

  • Example: How to get all string that contains "Java" sub-string in a string List?
List<String> strList = Arrays.asList("Java", "Python", "Java Stream", "Java Tutorial", "Nodejs Tutorial");

List<String> newStrList = new ArrayList<String>();

for(String str: strList) {
    if(str.contains("Java")) {
        newStrList.add(str);
    }
}

System.out.println(newStrList);
/*
[Java, Java Stream, Java Tutorial]
*/

Looping Example - Filter Custom Object List

  • Create a Customer class:

More at:

https://ozenero.com/java-how-to-use-stream-filter-in-java-8-with-list-array-examples

Java – How to use Stream Filter in Java 8 with List & Array Examples

Bình luận


White
{{ comment.user.name }}
Bỏ hay Hay
{{comment.like_count}}
Male avatar
{{ comment_error }}
Hủy
   

Hiển thị thử

Chỉnh sửa

Male avatar

loveprogramming

545 bài viết.
97 người follow
Kipalog
{{userFollowed ? 'Following' : 'Follow'}}
Cùng một tác giả
Male avatar
1 0
Tutorial Link: (Link) (Ảnh) Django is a Pythonbased free and opensource web framework that follows the modeltemplateview architectural pattern. A...
loveprogramming viết 7 tháng trước
1 0
Male avatar
1 0
https://loizenai.com/angular11nodejspostgresqlcrudexample/ Angular 11 Node.js PostgreSQL Crud Example (Ảnh) Tutorial: “Angular 11 Node.js Postg...
loveprogramming viết 6 tháng trước
1 0
Male avatar
1 0
Angular Spring Boot jwt Authentication Example Github https://loizenai.com/angularspringbootjwt/ (Ảnh) Tutorial: ” Angular Spring Boot jwt Authe...
loveprogramming viết 6 tháng trước
1 0
Bài viết liên quan
Male avatar
0 0
https://grokonez.com/java/java8/howtousejava8streamcollectorsgroupingbyexamples How to use Java 8 Stream Collectors.groupingBy() Examples no_toc]...
loveprogramming viết 2 tháng trước
0 0
{{like_count}}

kipalog

{{ comment_count }}

bình luận

{{liked ? "Đã kipalog" : "Kipalog"}}


Male avatar
{{userFollowed ? 'Following' : 'Follow'}}
545 bài viết.
97 người follow

 Đầu mục bài viết

Vẫn còn nữa! x

Kipalog vẫn còn rất nhiều bài viết hay và chủ đề thú vị chờ bạn khám phá!