Java

Java List Contains

The Java list interface and the java.util.list package represent the organized sequence of the elements. The sequence in which the members appear internally in a Java List determines how they can be added, retrieved, iterated, evaluated, and removed. Here, we will determine whether the list includes a particular item. The List.contains() is a convenient way to quickly determine whether a List includes a particular element or not. It returns true if an element is found when searching for it in the list; alternatively, it yields false.

Example 1:

The list() method of Java only accepts a single parameter which is the element to check its existence in the specified list. The program is implemented to check the integer list element by the contains() method.


First, the “ListContainsCase1” class of the program is deployed. Then, the main() method of the class is implemented. The main() method is defined with the list interface where we create the “values” object. We establish the empty list using the ArrayList() method where the size of the list is specified. Once the list is established, we insert the numbers using the add() method.

After that, we generate the Boolean “output” object and set it with the contains() method. The contains() method is called with the “values” list object and inputs a numeric value whose existence is to be examined from the provided number list. When the Boolean output is retrieved from the contains() method, the if-statement is used to show the message whether the list contains that specified number in the list or not. Next, we take another case scenario where the non-existence element is specified to the contains() method. We use the contains() method again in the declared object which is “output”. Its results are passed to the if-else block which shows the statement upon the true or false output.

The number “85” is the element of the array list which is evaluated by the contains() method and prints the message of the existence of the number. On the other hand, the number “85” is not part of the list. So, the else statement prints the “non-existence” message of the number:

Example 2:

The working of the contains() method is demonstrated in the previous example with the list of integer values. The value from the list can also be verified using another method termed indexOf(). The method outputs the list position of the provided element’s first entry.


 

 

The “ListContainsCase2” class is defined with the main() method where we use the ArrayList() method from the list interface. The list class is the generic type “Employee” which is another class. The ArrayList() method is set inside the “elements” list variable. The ArrayList is then added with the elements for the “Employee” class. There, we add one integer value and the other string values which are the names of the employees.

After that, we declare another variable, “emp”, which we call one of the sets of “Employees”. Then, we pass this “emp” variable as an argument of the contains() method to examine. Next, we declare the “idx” int variable where the indexOf() method is initialized. The indexOf() also takes the “emp” variable as input to get the index value of the employee name.

The values which are inserted in the previous ArrayList are the values of the “Employees” class attributes. The “Employees” class is constructed with the “emp_ID” and “emp_Name” attributes. Then, we use the “Employees” class constructor parameters with the “this” keyword to invoke them. After that, we have the “getID()” and “getName()”getter methods to get the emp_ID  and emp_Name. Also, the “setID()” and “setName()”setter methods are defined where the parameters of the “Employees” class are set with the “emp_ID” and “emp_Name” variables, respectively.  Then, we use the override implementation in the “Employees” class.

The Employee list is generated which is specified with “emp_ID” and “emp_Name”. The contains() method is applied to this list and returns the true value which means that the specified employee id and name are present in the list. Moreover, the index value is also fetched for the specified list values from the indexOf() method which also assures that the id and name are present in the list.

Example 3:

Now, the real scenario of the Java contains() method is deployed in the following program. The value which will be checked by the contains() method is taken from the user.


The “ListContainsCase3” class is developed with the main() method where the Scanner class is implemented. The Scanner class allows the user to enter the value on the console. We call the scanner(system.in) in the “sc” object. After that, we use the concurrentLinkedQueue() method to add the integer stream. The added number to the add() method is “1” which is then checked via the contains() method.

The console asks to enter the smallest, odd number value from the user which we already have in the stream. When the user adds the smallest odd number, it is verified from the if-else condition where we have the contains() method.  The contains() method examines if the smallest odd number provided to the console is located in the “stream” object of collection. Then, it executes the statements of the if-else block.

The entered number into the console is “1” which is the smallest, odd number given to the collection. The contains() method shows the “Correct Answer” message as shown in the following:

Example 4:

The containsAll() is also the method to validate that the list contains all of the elements in the input collection. If it finds it, it returns true. Otherwise, it returns false.


The “ListContainCase4” class main() method is declared with the “str” variable. The “str” variable represents the list which is called the ArrayList() method. For the ArrayList() method, we provide some string values. After that, we call the containsAll() method inside the “if” condition. The containsAll() method doesn’t take a certain string value from the list. It takes the “str” object of the list which has the entire string values. If the containsAll() method is equal to the Boolean true, the if statement displays that entire string values are part of the list.

The entire string which is verified with the string object is equal to the true value which is shown by the executed statement:

Conclusion

The contains() method is intended to determine whether the desired element is available in the list view. We used the contains() method with the integer type and string type list to evaluate the availability of the specific string and integer elements in the list. Furthermore, we employed the indexOf() method which is also used for this purpose to find that element from the list. Both these methods are compatible with finding the elements from the list.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.