1

I have one list that contains Company objects.

List<Company> companiesList

and every Company has a getName() method that returns company name.

List<Company> companiesList has couple of companies inside and I would like to compare this list with a list of string that contains company names

And this is my comparison list

List<String> searchList = Arrays.asList("abc", "xyz");

I have my method that gets the companies and with stream and filters it with some conditions from DB and I would like to add another filter that returns me the companies which equal to the strings in searchList

So basically the idea is to compare each element in companiesList with getName() and check if that exists in searchList list

public List<Company> getCompanies(String country, List<String> searchList, String version) {
   List<Company> result = countriesByCountryCache.getUnchecked(country)
      .stream()
      .filter(s -> version.compareTo(s.getVersion()) >= 0)
      //here to filter like for each element, i want to compare element.getName() and check if it exists in searchList and collect it
      .collect(Collectors.toList());

   return result;
}

I know it has been asked many times and there are many examples but I couldn't find a proper, correct solution that works. Thanks in advance!

3 Answers 3

2

You can just add another condition in you .filter() to only return the results which exists in your searchList once you filter the version.

It's slightly better to convert the searchList to a HashSet as you'll bring down the complexity of searching the companies from O(n) to O(1) and it'll also take care of removing any duplicate values you might have. It's even better to pass in the HashSet instead of a list (if you have control over the interface design).

Here is a snippet where I'm first converting the searchList to a set and then adding a new condition in .filter() to only return the companies which are present in the searchList.

public List<Company> getCompanies(String country, List<String> searchList, String version) {
  // Convert the given search list to a set
  final Set<String> searchQueries = new HashSet<>(searchList);
  List<Company> result = countriesByCountryCache.getUnchecked(country)
    .stream()
    .filter(s -> version.compareTo(s.getVersion()) >= 0 && searchQueries.contains(s.getName()))
    .collect(Collectors.toList());

  return result;
}
Sign up to request clarification or add additional context in comments.

Comments

2
public List<Company> getCompanies(String country, List<String> searchList, String version) {

List<Company> result = countriesByCountryCache.getUnchecked(country)
.stream()
.filter(s -> version.compareTo(s.getVersion()) >= 0 && searchList.contains(s.getName())
.collect(Collectors.toList());

return result;
}

Please check if above code works.

Comments

1

Add A filter/edit the existing filter in your stream which basically looks for existence of the countryName in searchList for each iteration.

   List<Company> result = 
       countriesByCountryCache.getUnchecked(country)
            .stream()
            //your filters...
            .filter(s -> searchList.contains(s.getName())
            .collect(Collectors.toList());
            return result;

2 Comments

Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.
You can still edit your answer to add some explanation - code-only answers tend to be deleted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.