Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if an arraylist contains two strings

I have an pojo class like the one below

public CategoryModel {

public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

}

I have an arraylist created like the one below.

List<CategoryModel> variantCategoryModelList = new ArrayList<>();
CategoryModel cat1= new CategoryModel();
cat1.setName(TEST1);
CategoryModel cat2= new CategoryModel();
cat2.setName(TEST1);
list.add(cat1);
list.add(cat2);

I have to check, if the value "TEST1" & "TEST2" present in the list and return "true" if both values present in the "list" and I tried something like the one below, though my "list" has both the values, its returning false.Could you please help me check what I am doing wrong btw I am using JDK 11.

final Optional<CategoryModel> optionalData = variantCategoryModelList.stream().
                filter(valueData -> TEST1.equalsIgnoreCase(valueData.getName())
                        && TEST2.equalsIgnoreCase(valueData.getName())).findFirst();

        if(optionalData.isPresent()){
            return true;
        }
like image 460
techy360 Avatar asked Jan 23 '26 06:01

techy360


2 Answers

You could map your CategoryModel to name and collect to list of strings and call List.containsAll :

return variantCategoryModelList.stream()
                               .map(CategoryModel::getName)
                               .collect(Collectors.toList())
                               .containsAll(Arrays.asList("TEST1","TEST2"));
like image 184
Eritrean Avatar answered Jan 24 '26 19:01

Eritrean


Set would be a more natural (and faster) data structure:

return variantCategoryModelList.stream()
                       .map(CategoryModel::getName)
                       .collect(Collectors.toSet())
                       .containsAll(Set.of("TEST1", "TEST2"));

Your problem was and (&&) instead of or.

So:

Set<String> soughtNames = Set.of("TEST1", "TEST2");
return variantCategoryModelList.stream()
                       .filter(cm -> soughtNames.contains(cm.getName()))
                       .distinct()
                       .count() == 2L;

As @fps commented, distinct() is needed on a list to prevent ["Test1", "Test1"] to be accepted, or ["Test1", "Test1", "Test2"] failing.

This is obviously inefficient as it will - having found 2 entries -, still walk to the end.

You want:

Set<String> soughtNames = Set.of("TEST1", "TEST2");
return soughtNames.stream()
    .allMatch(soughtName ->
         variantCategoryModelList.stream()
             .anyMatch(cm -> soughtName.equals(cm.getName()));

Or a bit retro-style:

return
    variantCategoryModelList.stream()
             .anyMatch(cm -> "TEST1".equals(cm.getName())) &&
    variantCategoryModelList.stream()
             .anyMatch(cm -> "TEST2".equals(cm.getName()));
like image 37
Joop Eggen Avatar answered Jan 24 '26 21:01

Joop Eggen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!