The line List<Banana>list3=filter(list, new SmallBanana());
doesn't compile with a message:
No enclosing instance of type Test is accessible. Must qualify the allocation with an enclosing instance of type Test (e.g. x.new A() where x is an instance of Test).
I wanted to know how can i pass that instead of the interface itself if it's possible.
class Banana {
int length;
int width;
Banana(int length, int width) {
this.length=length;this.width=width;
}
public int getLenght() {return length;}
public int getWidth(){return width;}
}
public class Test {
public static void main(String[] args){
List<Banana>list = new ArrayList<>();
List<Banana>list2=filter(list, (a)->a.length>15&&a.width>4);
List<Banana>list3=filter(list, new SmallBanana());
}
interface Filter{
boolean test(Banana banana);
}
public static List<Banana> filter(List<Banana>list, Filter f){
List<Banana>newBanana = new ArrayList<>();
for(Banana y : list){
if(f.test(y))
newBanana.add(y);
}
return newBanana;
}
class SmallBanana implements Filter{
public boolean test(Banana banana){
return banana.length<15&&banana.width<4;
}
}
}