bobuntu
bobuntu

Reputation: 55

Cannot resolve symbol (Java 8) lambdas

I'm kinda new to lambdas in Java 8. Here is a short snippet of my code:

public void analyzeQueries() {
  while (queries.size() > 0) {
    int rating;
    InvestmentQuery query = getFirstPriority(queries);
    RatingCacheElement cacheElement = stockToRating.get(x -> x.stockID == query.stockID);

    if (cacheElement != null)
      rating = cacheElement.rating;
    else {
      rating = calculateRating(query.stockID);
      stockToRating.add(new RatingCacheElement(query.stockID, rating));
    }
    if (rating > 80)
      stockTrader.enqueueStock(query);
  }
}

The complier output says "Error:(23, 54) java: incompatible types: int is not a functional interface". x.stockID is also being flagged with "Cannot resolve symbol". Using Android Studio 2.3.3, Gradle 1.14.1.

Upvotes: 1

Views: 4252

Answers (1)

Tagir Valeev
Tagir Valeev

Reputation: 100209

From your snippet it seems that stockToRating is a List<RatingCacheElement>. The List.get method accepts int (that is, the index of list element). If you want to find an element by condition, consider using Stream API:

RatingCacheElement cacheElement = stockToRating.stream()
   .filter(x -> x.stockID == query.stockID).findFirst().orElse(null);

Here you say to filter a stream of list items by condition, then find the first element matching the filter or return null if nothing is found.

Upvotes: 2

Related Questions