Filter list in list Java 8
Ngày đăng:
23/12/2021
Trả lời:
0
Lượt xem:
251
In this tutorial, we are going to show you how to filter a stream of objects in Java 8. We can filter the string values and as well as objects using the Java 8 Stream filter. Show Java 8 Stream Filter :The filter() in Java 8 is a method which is coming from the Stream interface, which returns the Stream object consisting of the elements of this stream that matches the given Predicate(action). Legacy way of filtering the list in Java : The similar way of filtering the list of strings using Java 8 Stream Filter. ListStream Filter with Strings :Java8_Streams_filter.java package com.onlinetutorialspoint.java8;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Java8_Streams_filter {
public static void main(String[] args) {
ListRecommended: How to use Java8 foreach Stream Filter with Objects :We can filter the list of user-defined objects using the Stream filter in Java. Here I am going to create a Subject POJO class to filter the elements. Student.java (POJO) Subject.java class Subject {
private int subId;
private String subName;
public Subject(int subId, String subName) {
super();
this.subId = subId;
this.subName = subName;
}
public int getSubId() {
return subId;
}
public String getSubName() {
return subName;
}
}Stream Filter with Objects Example :Java8_Streams_filter.java package com.onlinetutorialspoint.java8;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Java8_Streams_filter {
public static void main(String[] args) {
ListOutput: JavaHappy Learning Related Posts
Previous Next Share a word.FacebookTwitterRedditLinkedInTumblrPinterestVkEmail Leave A Comment Cancel replyComment
Δ |