iondevx
iondevx

Reputation: 17

Comparing two List elements with stream in Java and return

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!

Upvotes: 1

Views: 1347

Answers (3)

Dinesh Narayan
Dinesh Narayan

Reputation: 105

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;

Upvotes: 1

ninja.coder
ninja.coder

Reputation: 9658

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

Upvotes: 2

Raushan
Raushan

Reputation: 161

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.

Upvotes: 2

Related Questions