The filter method of the Stream
class introduced in Java 8 allows the developer to eliminate elements from a stream. Therefore, only the desired elements are preserved in the output stream. To learn how to eliminate elements using the filter method, follow these seven steps.
Take our Introduction to Java Training course for free.
See the Course Outline and Registerpublic class MusicalInstrument {
private String name;
private String type;
public MusicalInstrument(String name, String type) {
setName(name);
setType(type);
}
public String getName() {
return name;
}
public void setName (String name) {
this.name=name;
}
public String getType() {
return type;
}
public void setType (String type) {
this.type=type;
}
public String toString() {
return name + " is a " + type + " instrument";
}
}
MusicalInstrument.java
.
import java.util.stream.*;
import java.util.*;
public class UseFilterMethod {
private static List musicalInstruments=new ArrayList<>();
static {
musicalInstruments.add(new MusicalInstrument("Trumpet","brass"));
musicalInstruments.add(new MusicalInstrument("Tuba","brass"));
musicalInstruments.add(new MusicalInstrument("Timpani","percussion"));
musicalInstruments.add(new MusicalInstrument("Piano","keyboard"));
}
public static void main (String args[]) {
System.out.println("Musical instruments in the collection that start with \"T\":");
musicalInstruments
.stream()
.filter(instrument->instrument.getName().startsWith("T"))
.forEach(instrument->System.out.println(instrument) );
System.out.println("Percussion musical instruments in the collection that start with \"T\":");
musicalInstruments
.stream()
.filter(instrument->instrument.getName().startsWith("T") && instrument.getType().equals("percussion"))
.forEach(instrument->System.out.println(instrument) );
System.out.println("Brass musical instruments in the collection along with keyboard instruments:");
musicalInstruments
.stream()
.filter(instrument->instrument.getName().startsWith("T") || instrument.getType().equals("keyboard"))
.forEach(instrument->System.out.println(instrument) );
}
}
System.out.println
that indicates what musical instruments will be displayed. I have highlighted the three filter methods in the image below:ChainStreamOperations.java
.Webucator provides instructor-led training to students throughout the US and Canada. We have trained over 90,000 students from over 16,000 organizations on technologies such as Microsoft ASP.NET, Microsoft Office, XML, Windows, Java, Adobe, HTML5, JavaScript, Angular, and much more. Check out our complete course catalog.
Stephen has over 30 years' experience in training, development, and consulting in a variety of technology areas including Java, C, C++, XML, JavaScript, AJAX, Tomcat, JBoss, Oracle, and DB2. His background includes design and implementation of business solutions on client/server, Web, and enterprise platforms. Stephen is a published writer in both technical and non-technical endeavors. Stephen received an undergraduate degree in Computer Science and Physics from Florida State University.