简体   繁体   中英

Java - Search List of Strings if contains another List of Strings

I have list of user tweets ( usersTweets ), i need to find tweets which contain specific keys words ( listOfkeyWord ), and add matches to ( includeKeyWord ) List.

    String keyWord = "one,two";
    List<String> usersTweets = new ArrayList<String>();
    usersTweets.add("three nine one two");
    usersTweets.add("seven one");
    usersTweets.add("three nine ten one");
    usersTweets.add("....");

    List<String> includeKeyWord = new LinkedList<String>();

    if (keyWord.contains(",")) {
        List<String> listOfkeyWord = new ArrayList<String>(Arrays.asList(keyWord.split(",")));
        for (String tweet : usersTweets) {
            if (tweet.contains(listOfkeyWord)) {
                includeKeyWord.add(tweet);
            }
        }
    }

Note:

How to check every tweet if it is containing a list of keys words.In this example i want have in ( includeKeyWord ) only String "three nine one two" because it has words "one" and "two".

Basically you want to check if tweet has all the words, so you need to iterate through list of those words and check if your tweet has them all, you can do this:

public boolean hasAllTheWords(String yourString, List<String> words){
  for(String word : words)
    if(!yourString.contains(word))
      return false
  return true;
}

Now instead of tweet.contains(words) you do hasAllTheWords(tweet, words) . You can also convert your tweet to a list of words, and then what you already have would work ( tweet.contains(words) ), so you can try this too:

Arrays.asList(tweet.split(" ")).contains(words)

And if you try that solution, remember to take care of punctuations ( , . ... ! ? etc.)

Here you are(a full example):

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Example {

    /**
     * Constructor
     */
    public Example() {

        //List of usersTweets
        List<String> usersTweets = new ArrayList<>();
        usersTweets.add("three nine one two");
        usersTweets.add("seven one");
        usersTweets.add("three nine ten one");
        usersTweets.add("....");

        //The keyWord
        String keyWord = "one,two";


        //make keyWord a List of words
        List<String> listOfkeyWord = new ArrayList<>(Arrays.asList(keyWord.split(",")));

        // Filter every word in the list usersTweets
        //if it contains all the words from listOfKeyWord 
        //then collect all them into a List
        List<String> includeKeyWord = usersTweets.stream()
                                                 .filter(tweet -> listOfkeyWord.stream()
                                                                               .allMatch(tweet::contains)) //word -> tweet.contains(word))
                                                 .collect(Collectors.toList());
    }

    public static void main(String[] args) {
        new Example();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM