kesav
kesav

Reputation: 17

How to check a value is equal to any of the three values

I need to check for each element the metadata data of book is (BOOK or LANGUAGE or FORMAT). With the code below I get this error:

Cannot invoke equalsIgnoreCase(String) on the primitive type boolean
List<MetaDataTypeEntity> bookMdtTypeEntity=(List<MetaDataTypeEntity>)
CollectionUtils.emptyIfNull(getMetaDataTypes()).stream()
    .filter(data-> data.getMetaDataTypeName().equalsIgnoreCase("BOOK").equalsIgnoreCase("LANGUAGE").equalsIgnoreCase("FORMAT"))
    .findAny().orElse(null);

I need to go inside of every object in list and check conditions BOOK And FORMAT LANGUAGE hierarchy. Please give me a code suggestion.

Upvotes: 1

Views: 228

Answers (2)

Ryuzaki L
Ryuzaki L

Reputation: 40078

You can store three values in Set

Set<String> values = Set.of("BOOK","LANGUAGE","FORMAT");   //from java 9

And then use contains

MetaDataTypeEntity bookMdtTypeEntity= CollectionUtils.emptyIfNull(getMetaDataTypes()).stream()
.filter(data-> values.contains(data.getMetaDataTypeName().toUpperCase()))
.findAny().orElse(null);

As @Holger suggested you can also use TreeSet for ignore case search, and also the most important thing findAny returns Optional<MetaDataTypeEntity> so the casting will fail since you are returning MetaDataTypeEntity and casting it into List<MetaDataTypeEntity>

Set<String> set = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
            set.add("BOOK");
            set.add("LANGUAGE");
            set.add("FORMAT");

MetaDataTypeEntity bookMdtTypeEntity= CollectionUtils.emptyIfNull(getMetaDataTypes()).stream()
.filter(data-> values.contains(data.getMetaDataTypeName()))
.findAny().orElse(null);

Upvotes: 1

i.bondarenko
i.bondarenko

Reputation: 3572

You need to change your condition, .equalsIgnoreCase("BOOK") returns boolean so you can't use .equalsIgnoreCase("LANGUAGE") on boolean.

Try following code:

List<MetaDataTypeEntity> bookMdtTypeEntity=(List<MetaDataTypeEntity>)
        CollectionUtils.emptyIfNull(getMetaDataTypes()).stream()
                .filter(data-> data.getMetaDataTypeName().equalsIgnoreCase("BOOK")
                        || data.getMetaDataTypeName().equalsIgnoreCase("LANGUAGE")
                        || data.getMetaDataTypeName().equalsIgnoreCase("FORMAT"))
                .findAny().orElse(null);

Upvotes: 0

Related Questions