-3

I am trying to return False if result = false. I can't change the return type. linkList needs to return false if result = false. Thanks

private static class FilteringFlatApplyFunction<T> implements FlatApplyFunction<T,T> {        
    private final Predicate pred; 
    LinkedList<T> linkList = new LinkedList<T>();

    public FilteringFlatApplyFunction(Predicate<T> p) {                              
        this.pred = p;
        this.linkList = linkList;
    }

    @Override
    public List<T> apply(T x){               
        boolean result = pred.check(x);              
        if (result = true) {
            linkList.add(x);
            return linkList;
        }                
        if (result = false) {
             linkList = false;              
        }               
        return linkList;                              
    }
}
Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161
  • 3
    Re `if (result = true)` and `if (result = false)`: `=` is **assignment**, not comparison; comparison is `==`: http://stackoverflow.com/questions/39385382/why-doesnt-my-equality-comparison-using-a-single-equals-work-correctly-in-j (Also: Don't use `==` or `!=` with booleans [partially for this reason], you already have a boolean. Just `if (result)` and `if (!result)`.) – T.J. Crowder Mar 07 '17 at 06:41

5 Answers5

6

Quite simply, you can't. The method signature says quite clearly that it is to return a List<T>, and a List<T> is what you shall return.

You could return null instead.

Joe C
  • 15,324
  • 8
  • 38
  • 50
0

You can't return boolean to a List, what you can do is return null.

While you're checking the condition, what you can do is, create another method that encapsulate your apply method and return if you're list is null then it's false. Like below:

if(linkList == null)
   return false;
else
   return true;

Besides, Use == for comparison.

As below:

@Override
public List<T> apply(T x){               
    boolean result = pred.check(x);              
    if(result == true) {
        linkList.add(x);
        return linkList;
    }
    if(result == false){
         return null;              
    }
    return linkList;
}

OR in case of booleans you need not to use ==, like below

@Override
public List<T> apply(T x){               
    boolean result = pred.check(x);              
    if(result){
        linkList.add(x);
        return linkList;
    }
    if(result){
         linkList = false;              
    }
    return linkList;
}
Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161
Parth Soni
  • 11,158
  • 4
  • 32
  • 54
0

You can't return false if the method specifically returns a List. What you can do is return `null':

@Override
public List<T> apply(T x){               
    boolean result = pred.check(x);              
    if (result == true) {
        linkList.add(x);
        return linkList;
    }
    if (result == false) {
         linkList = null;              
    }
    return linkList;
}

Then in the calling method do the following:

List<T> list = apply(x);
boolean result;
if (list == null){
   result = false;
}
Wasi Ahmad
  • 35,739
  • 32
  • 114
  • 161
Luke Bajada
  • 1,737
  • 14
  • 17
0

Sorry bud. You can't return different types. Returning NULL or maybe returning an object are your best chances.

0

if result is false you return null so i think you dont need to do the test if result is false!!!

private static class FilteringFlatApplyFunction<T> implements FlatApplyFunction<T,T> {        
    private final Predicate pred; 
    LinkedList<T> linkList = new LinkedList<T>();

    public FilteringFlatApplyFunction(Predicate<T> p) {                              
        this.pred = p;
        this.linkList = linkList;
    }

    @Override
    public List<T> apply(T x){               
        boolean result = pred.check(x);              
        if (result) {
            linkList.add(x);  
        }                         
        return linkList;                              
    }
}
Younes Ouchala
  • 300
  • 1
  • 4
  • 15