Home > Net >  Check if one List<String> contains specific string from another list
Check if one List<String> contains specific string from another list

Time:02-18

I have a list of emails of List<String> type, and another one of keywords also List<String>. Each email should contain a keyword - order is important - meaning, that first email should contain first keyword and so on.

How can I check (other than for loop) that each email snippet contains a keyword?

CodePudding user response:

First, it may be needed to check the size of both lists, then to compare corresponding list items, IntStream should be used:

public static boolean allKeywordsFound(List<String> emails, List<String> keywords) {
    return emails.size() == keywords.size() &&
        IntStream.range(0, emails.size())
            .allMatch(i -> emails.get(i).contains(keywords.get(i)));
}

CodePudding user response:

I see that others correctly answered your question but here's my take on the issue. I presume you want the emails to be checked in order so here's a piece of code that uses Stream API instead of a for loop, I also put together the emails list and the result into a Map since you didn't specify whether you want the resulting boolean value to be for all the emails together or if you want a boolean value for each email containing the same-position keyword:

//mock data initialization
List<String> emails = new ArrayList<>();
List<String> keywords = new ArrayList<>();

//mock data initialization
emails.add("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua");
emails.add("eu lobortis elementum nibh tellus molestie nunc non blandit massa enim nec dui nunc mattis enim ut tellus elementum sagittis");
emails.add("Dignissim suspendisse in est ante in nibh mauris");

//mock data initialization
keywords.add("consectetur");
keywords.add("Foo");
keywords.add("Dignissim");

//initialized a list to contain whether a keyword exists for each email
List<Boolean> exists = new ArrayList<>();

//loaded it with boolean values (the exists List has the same order as the emails list)
emails.forEach(email -> exists.add(email
            .contains(keywords
                    .get(emails
                            .indexOf(email)))));

//since I don't know what you wanna do with the result, I decided to just put them together in a Map
//with the email string as the key and the existence variable as a value
LinkedHashMap mapOfTruth = new LinkedHashMap();
emails.forEach(email -> mapOfTruth.put(email, exists.get(emails.indexOf(email))));

Output

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua = true
eu lobortis elementum nibh tellus molestie nunc non blandit massa enim nec dui nunc mattis enim ut tellus elementum sagittis = false
Dignissim suspendisse in est ante in nibh mauris = true

CodePudding user response:

This code using Java streams/maps checks if each email contains their respective keyword.

boolean allEmailsContainKeyword(List<String> emails, List<String> keywords) {
    return !emails.stream().map(email -> email.contains(keywords.get(emails.indexOf(email)))).collect(Collectors.toList()).contains(false);
}
  • Related