Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

To return actor older than a certain age limit using Java Stream filter function

I want to return actor older than a certain age limit using Java Stream filter function, but my code returned an error message

Line 63: error: cannot find symbol [in Actor.java]
.filter(actor -> actor.getAge > ageLimit)
symbol: variable getAge
location: variable actor of type Actor

My code is as below:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import java.util.*;
import java.io.*;
import java.nio.*;
import java.math.*;
import java.time.chrono.IsoChronology;
import java.time.LocalDate;
import java.util.stream.Collectors;

class Actor {
    String name;
    LocalDate birthday;
    
    Actor(String nameArg, LocalDate birthdayArg) {
        name = nameArg;
        birthday = birthdayArg;
    }

    public int getAge() {
        return birthday
            .until(IsoChronology.INSTANCE.dateNow())
            .getYears();
    }

    public String getName() {
        return name;
    }


    public static List<Actor> createCast() {
        
        List<Actor> cast = new ArrayList();
        cast.add(
            new Actor(
            "Fred", 
            IsoChronology.INSTANCE.date(1980, 6, 20)));
        cast.add(
            new Actor(
            "0mar", 
            IsoChronology.INSTANCE.date(1990, 12, 10)));
        return cast;
    } 

    public void printActor() {
        System.out.println(name + ", " + this.getAge()); }
    }


class cast {
     interface CheckPerson {
         boolean test (Actor p);
     }

    public static void printActors (List<Actor> cast) {
        for (Actor p: cast) {
            p.printActor();
        }
    }

    public static List<Actor> getActorsolderThan(List<Actor> cast, int ageLimit) {

        List<Actor> result = cast.stream()  // convert list to stream 
            .filter(actor -> actor.getAge > ageLimit) //TODO
            .collect(Collectors. toList());

        return result;
    }
}

The test code is as below:

//Test Code: 
List<Actor> cast = Actor.createCast();
List<Actor> castolderThan = Cast.getActorsolderThan(cast, 30); 
Cast.printActors(cast);

May I ask how exactly should I use filter function to return actor older than a certain age limit by editing the code on the filter line in the getActorsolderThan method?

>Solution :

I think this line should be like this

 List<Actor> result = cast.stream()  // convert list to stream
                .filter(actor -> actor.getAge() > 100) //TODO
                .collect(Collectors. toList());

getAge is a function not a class variable

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading