javafilterjava-stream

Getting "cannot find symbol" compilation error when returning actors older than a certain age limit using Java Stream filter function


I want to return actor older than a certain age limit using the 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:

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);

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