Java lambda – finds whether any string element of a list matches any element part of another list

I have two string lists

A = {"apple","mango","pineapple","banana",... }

B = {"app","framework",...}

What I'm looking for is: any element of B is at least a partial match (substring / contains / startswith) with any element of A For example, the first element of B 'app' matches at least one element 'apple part'

Other closely matched topics on stackoverflow do not consider 2 lists

Is there an elegant way to express a solution using java lambda?

I think this is a general problem in the search domain So if there is any help or interesting reading on this topic, I will be glad to receive instructions

Solution

It depends on your elegance, but try this:

List<String> r = list1
  .parallelStream()
  .filter( w1->{
      return list2
        .parallelStream()
        .anyMatch( w2->w1.contains(w2) ); 
      }
   )
  .collect(Collectors.toList());

Anymatch (and filter) can be short circuited and found at W1 After the first match of contains (W2), the flow of the second list is aborted. If found, it returns true, providing some efficiency Use this option to filter the first stream In parallel flow

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>