Home > Back-end >  Filtering data using Java 8
Filtering data using Java 8

Time:09-16

I have a list of Schools List<Schools> and want to filer out the blacklisted schools. I want to filter out the blacklisted schools using Java 8. How should I do it. Is Java 8 streams the best way. <Please, forgive my punctuations, some keyboard keys are not working>. Ex if List<Schools> contain School A, School B, School C. And BlackListed school is B. Final Result should be A and C.

Schools.java

public class Schools{
public String className;
public String schoolCode;
}

BlackListedSchools.java

public class BlackListedSchools
{
public School school;
public School getSchool()
{
return this.school;
}
}


public School
{
  public String className;
    public String schoolCode;
}

CodePudding user response:

List schools = new ArrayList<>(); BlackListedSchools B = new BlackListSchools(); List schoolRes = schools.stream().filter(t->t.schoolCode != B.school.schoolCode).collect(Collectors.toList()

CodePudding user response:

I have created all the required java classes as per your question. You can use and implement the below code-

School.java

public class School {
    
      private String schoolName;
      private String schoolCode;
    
      public School(String schoolName, String schoolCode) {
        this.schoolName = schoolName;
        this.schoolCode = schoolCode;
      }
    
      public String getSchoolName() {
        return schoolName;
      }
    
      public void setSchoolName(String schoolName) {
        this.schoolName = schoolName;
      }
    
      public String getSchoolCode() {
        return schoolCode;
      }
    
      public void setSchoolCode(String schoolCode) {
        this.schoolCode = schoolCode;
      }
    
      @Override
      public String toString() {
        return "School{"  
            "schoolName='"   schoolName   '\''  
            ", schoolCode='"   schoolCode   '\''  
            '}';
      }

BlacklistedSchool.java

public class BlackListedSchool {

  private School school;

  public School getSchool() {
    return school;
  }

  public void setSchool(School school) {
    this.school = school;
  }

  @Override
  public String toString() {
    return "BlackListedSchool{"  
        "school="   school  
        '}';
  }
}

Test.class

public class TestSchool {
  public static void main(String[] args) {

    School schoolA = new School("A","A001");
    School schoolB = new School("B","B001");
    School schoolC = new School("C","C001");

    BlackListedSchool blackListedSchool = new BlackListedSchool();
    blackListedSchool.setSchool(schoolB);

    List<School> listOfSchools = new ArrayList<>();
    listOfSchools.add(schoolA);
    listOfSchools.add(schoolB);
    listOfSchools.add(schoolC);

    List<School> notBlackListedSchool =
        listOfSchools.stream().filter(sc -> !sc.getSchoolCode().equals(blackListedSchool.getSchool().getSchoolCode())).collect(Collectors.toList());

    System.out.println(notBlackListedSchool);

  }
}

Output ::

[School{schoolName='A', schoolCode='A001'}, School{schoolName='C', schoolCode='C001'}]
  • Related