Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of filter mode in Java

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces what is the use of the filter mode in Java, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to know about it.

Realize

We will create a Person object, a Criteria interface, and an entity class that implements that interface to filter the list of Person objects. CriteriaPatternDemo, our demo class uses Criteria objects to filter the list of Person objects based on various standards and their combinations.

Step 1

Create a class and apply standards to it.

Person.javapublic class Person {private String name; private String gender; private String maritalStatus; public Person (String name,String gender,String maritalStatus) {this.name = name; this.gender = gender; this.maritalStatus = maritalStatus;} public String getName () {return name;} public String getGender () {return gender;} public String getMaritalStatus () {return maritalStatus;}}

Step 2

Create an interface for the standard (Criteria).

Criteria.javaimport java.util.List; public interface Criteria {public ListmeetCriteria (Listpersons);}

Step 3

Create an entity class that implements the Criteria interface.

CriteriaMale.javaimport java.util.ArrayList;import java.util.List; public class CriteriaMale implements Criteria {@ Override public ListmeetCriteria (Listpersons) {ListmalePersons = new ArrayList (); for (Person person: persons) {if (person.getGender (). EqualsIgnoreCase ("MALE")) {malePersons.add (person);}} return malePersons;}} CriteriaFemale.javaimport java.util.ArrayList;import java.util.List Public class CriteriaFemale implements Criteria {@ Override public ListmeetCriteria (Listpersons) {ListfemalePersons = new ArrayList (); for (Person person: persons) {if (person.getGender (). EqualsIgnoreCase ("FEMALE")) {femalePersons.add (person);}} return femalePersons;}} CriteriaSingle.javaimport java.util.ArrayList;import java.util.List Public class CriteriaSingle implements Criteria {@ Override public ListmeetCriteria (Listpersons) {ListsinglePersons = new ArrayList (); for (Person person: persons) {if (person.getMaritalStatus (). EqualsIgnoreCase ("SINGLE")) {singlePersons.add (person);}} return singlePersons;}} AndCriteria.javaimport java.util.List; public class AndCriteria implements Criteria {private Criteria criteria; private Criteria otherCriteria Public AndCriteria (Criteria criteria, Criteria otherCriteria) {this.criteria = criteria; this.otherCriteria = otherCriteria;} @ Override public ListmeetCriteria (Listpersons) {ListfirstCriteriaPersons = criteria.meetCriteria (persons); return otherCriteria.meetCriteria (firstCriteriaPersons);}} OrCriteria.javaimport java.util.List; public class OrCriteria implements Criteria {private Criteria criteria; private Criteria otherCriteria; public OrCriteria (Criteria criteria, Criteria otherCriteria) {this.criteria = criteria; this.otherCriteria = otherCriteria } @ Override public ListmeetCriteria (Listpersons) {ListfirstCriteriaItems = criteria.meetCriteria (persons); ListotherCriteriaItems = otherCriteria.meetCriteria (persons); for (Person person: otherCriteriaItems) {if (! firstCriteriaItems.contains (person)) {firstCriteriaItems.add (person);}} return firstCriteriaItems;}}

Step 4

Use different standards (Criteria) and their combination to filter the list of Person objects.

CriteriaPatternDemo.javaimport java.util.ArrayList; import java.util.List; public class CriteriaPatternDemo {public static void main (String [] args) {Listpersons = new ArrayList (); persons.add (new Person ("Robert", "Male", "Single")); persons.add (new Person ("John", "Male", "Married"); persons.add (new Person ("Laura", "Female", "Married")) Persons.add (new Person ("Diana", "Female", "Single"); persons.add (new Person ("Mike", "Male", "Single")); persons.add (new Person ("Bobby", "Male", "Single")); Criteria male = new CriteriaMale (); Criteria female = new CriteriaFemale (); Criteria single = new CriteriaSingle (); Criteria singleMale = new AndCriteria (single, male) Criteria singleOrFemale = new OrCriteria (single, female); System.out.println ("Males:"); printPersons (male.meetCriteria (persons)); System.out.println ("\ nFemales:"); printPersons (female.meetCriteria (persons)); System.out.println ("\ nSingle Males:"); printPersons (singleMale.meetCriteria (persons)); System.out.println ("\ nSingle Or Females:") PrintPersons (singleOrFemale.meetCriteria (persons));} public static void printPersons (Listpersons) {for (Person person: persons) {System.out.println ("Person: [Name:" + person.getName () + ", Gender:" + person.getGender () + ", MaritalStatus:" + person.getMaritalStatus () + "]);}

Step 5

Execute the program and output the result:

Males: Person: [Name: Robert, Gender: Male, Marital Status: Single] Person: [Name: John, Gender: Male, Marital Status: Married] Person: [Name: Mike, Gender: Male, Marital Status: Single] Person: [Name: Bobby, Gender: Male, Marital Status: Single] Females: Person: [Name: Laura, Gender: Female, Marital Status: Married] Person: [Name: Diana, Gender: Female, Marital Status: Single] Single Males: Person: [Name: Robert, Gender: Male Marital Status: Single] Person: [Name: Mike, Gender: Male, Marital Status: Single] Person: [Name: Bobby, Gender: Male, Marital Status: Single] Single Or Females: Person: [Name: Robert, Gender: Male, Marital Status: Single] Person: [Name: Diana, Gender: Female, Marital Status: Single] Person: [Name: Mike, Gender: Male, Marital Status: Single] Person: [Name: Bobby, Gender: Male, Marital Status: Single] Person: [Name: Laura, Gender: Female Marital Status: Married] Thank you for reading this article carefully. I hope the article "what is the use of the filter mode in Java" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report