Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opposite of .contains (does not contain)

Tags:

java

contains

I have an if statement:

if (inventory.contains("bread")) 

But now I want to check

  • if inventory contains "bread"
  • but does not contain "water".

How can I do this?

like image 947
Tizer1000 Avatar asked Mar 31 '13 13:03

Tizer1000


People also ask

Does not contain in list Java?

contains() method can be used to check if a Java ArrayList contains a given item or not. This method has a single parameter i.e. the item whose presence in the ArrayList is tested. Also it returns true if the item is present in the ArrayList and false if the item is not present.

What is contain method?

The contains() method checks whether a string contains a sequence of characters. Returns true if the characters exist and false if not.

What is contain character?

Contains(Char) Returns a value indicating whether a specified character occurs within this string. Contains(String) Returns a value indicating whether a specified substring occurs within this string. Contains(Char, StringComparison)

Does list have a Contains method?

The contains() method of List interface in Java is used for checking if the specified element exists in the given list or not. Parameters: This method accepts a single parameter obj whose presence in this list is to be tested.


1 Answers

It seems that Luiggi Mendoza and joey rohan both already answered this, but I think it can be clarified a little.

You can write it as a single if statement:

if (inventory.contains("bread") && !inventory.contains("water")) {     // do something } 
like image 127
Itsan Alias Avatar answered Sep 20 '22 08:09

Itsan Alias