How to check if an arraylist contains a value in Java

Ads
 

How to check if an arraylist contains a value in Java

Hi,

I have an array list object in Java with few values. I want to check if it contains a particular value. I don't want to write it in for loop. So what is the best way to do this?

How to check if an arraylist contains a value in Java?

Thanks

View Answers

July 30, 2019 at 12:17 AM

Hi,

One can use arraylist contains() method which is used to find if given data contains in the array list object.

The use of contains() method of ArrayList class is the easiest method to find if an object/string/value contains in the given ArrayList object.

Here is complete program example:

package my.package;

import java.util.ArrayList;
import java.util.List;

public class ArrayListContainsExample {

    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<String>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");
        list.add("6");
        list.add("7");
        list.add("8");
        list.add("9");
        list.add("10");

        //Check if arraylist contains 5
        boolean result = list.contains("5"); 
        if (result){
            System.out.println("List contains: 5"); 
        }else{
            System.out.println("List does not contains: 5");
        }

        //Check if arraylist contains 50
        result = list.contains("50"); 
        if (result){
            System.out.println("List contains: 50"); 
        }else{
            System.out.println("List does not contains: 50");
        }        
    }

}

Hope this helps you.

Thanks

Ads









Related Tutorials/Questions & Answers:
Java arraylist contains
contains() method is used to check whether the element is present or not. It returns boolean value i.e. true or false. Example of Java Arraylist Contains import java.util.ArrayList; import java.util.List; public
how to check particular value is avilable in hashmap - Java Beginners
how to check particular value is avilable in hashmap  Hi friends, i created a hashmap,in that hashmap key is tagginglist and value is tagsname,these tags is in the form arraylist.then how i will check particular tags
Advertisements
how to search the string arraylist contains database rows?
how to search the string arraylist contains database rows?  i need... the row values into string arraylist. now i want to print the common rows existed. how do i do
How to check if a string contains a substring in iOS?
have learned how to find it a string contains in the source string. Download... in iOS. In Java programming language you can use the indexOf method... on the 'Check If Exists' button application will find the string and show
How to find maximum value in ArrayList
How to find maximum value in ArrayList  **Sir i am writing below code but its not working.i don't know how to implement logic my requirement.My requirement is find maximum value in userdefined objects and dispaly that object
How to find maximum value in ArrayList
How to find maximum value in ArrayList  **Sir i am writing below code but its not working.i don't know how to implement logic my requirement.My requirement is find maximum value in userdefined objects and dispaly that object
How to find maximum value in ArrayList
How to find maximum value in ArrayList  **Sir i am writing below code but its not working.i don't know how to implement logic my requirement.My requirement is find maximum value in userdefined objects and dispaly that object
How to find maximum value in ArrayList
How to find maximum value in ArrayList  **Sir i am writing below code but its not working.i don't know how to implement logic my requirement.My requirement is find maximum value in userdefined objects and dispaly that object
How to find maximum value for userdefined objects in ArrayList
How to find maximum value for userdefined objects in ArrayList  Hi sir,I have requirement i.e finding maximum value in userdefined objects... value but can't store that object into another object.Please help me how
How to find maximum value for userdefined objects in ArrayList
How to find maximum value for userdefined objects in ArrayList  Hi sir,I have requirement i.e finding maximum value in userdefined objects... value but can't store that object into another object.Please help me how
How to find maximum value for userdefined objects in ArrayList
How to find maximum value for userdefined objects in ArrayList  Hi sir,I have requirement i.e finding maximum value in userdefined objects... value but can't store that object into another object.Please help me how
how to display each arraylist value on new page in jsp
how to display each arraylist value on new page in jsp  hi!!!! i want to display each arraylist value on new page in jsp????? and also want to access the arraylist values using next button..... any help would be appreciated
How to obtain the selected data item from a list box when listbox contains objects wrapped under Arraylist?
How to obtain the selected data item from a list box when listbox contains objects wrapped under Arraylist?  I have a jsp page containing a list box... page. The listbox gets populated by the data contained in the arraylist. Now, I
How to create new arraylist using Java
someone provides online example how to create new arraylist in java programming...How to create new arraylist using Java  hi, I want to develop... arraylist Java. The Java ArrayList object is created with the reference of List
How to sort ArrayList in java
How to sort ArrayList in java In this section you will learn how to sort ArrayList in java. ArrayList support dynamic array that can grow as needed. Array... which sort the Arraylist. Now here is the code to sort the ArrayList in java
How to check for Empty textboxed and fill it with a value on a button click
How to check for Empty textboxed and fill it with a value on a button... is a code which check for Empty textbox and fill it with a value on a button... should check if it is empty and should get added to the second target text box
Java arraylist of arraylist
Java arrayList can make the use of other arrayList.  In one arrayList other arrayList can be added as the object  It works as two dimension array. Example of Java Arraylist of Arraylist import
Java ArrayList
Java ArrayList   How to declare unique ArrayList
How to convert Arraylist into String Array Java
How to convert Arraylist into String Array Java  Hi, I am beginners of Java programming. Can somebody Suggest me how to convert arraylist to string... information with example. You can find the online example of how to convert arraylist
How to check a file exists or not in java.
Description: This example demonstrate how to check a file exits at a given path or not.  The method isFile() of the File class check for its existence...;new File("C:" + File.separator + "java
How to check a file is hidden or not in java
Description: This example demonstrate how to check a file is hidden at a given path or not.  The method isHidden() of the File class check for its...;+ "java-tutorial"         
Find max and min value from Arraylist
Find max and min value from Arraylist In this tutorial, you will learn how to find the maximum and minimum value element from the ArrayList. Java provides direct methods to get maximum and minimum value from any collection class i.e
check null exception java
check null exception java  check null exception java - How to check the null exception on Java?   The null pointer exception in java occurs... (contains no value). Whenever you'll perform any action on null objects the JVM throw
How to get enum value in Java
How to get enum value in Java  Hi all, i am looking for a solution to get an enum value from index in Java. Please suggest. Thanks
How to Convert ArrayList to Array?
How to Convert ArrayList to Array?  Hi, I am trying to learn to Convert ArrayList to Array. How it is possible? How to Convert ArrayList to Array... ArrayList to Array. Check more examples at Convert ArrayList to Array. Thanks
How to add key value pair in Java
How to add key value pair in Java  Hi, I have to store the key and value pair in Java. How to store key value pair in Java? Thanks   Hi, You can use the put method of Hashtable. Check the tutorial Creating a Hash
ArrayList in java
ArrayList in java  sir i have a class Student.it contains id,name,marks of students and a parameteraised constructor.now i created an arraylist...); a.add(s3); my question is...how can we sort the students by their marks?how
java arraylist
help how to handle this through java arraylist. xml : <?xml version='1.0...java arraylist  Hi, Thanks in advance.. From the xml given below i need to create arraylist whose name is same as type value(e.g : list1,list2
How to convert EBCDIC format value into ASCII format value in java
How to convert EBCDIC format value into ASCII format value in java  how to convert EBCDIC data format into ASCII format data using java Use Case:I my file contains ASCII format values as well as EBCDIC format values.I need
How to check if a file is read-only in java
How to check if a file is read-only in java A read-only file is any file... setReadOnly() method and can check this using canWrite() method of File class. If the value return from the canWrite() method is false, means that file
arraylist in java
arraylist in java  arraylist in java   Array List Example in java

Ads