How to check if an arraylist contains a value in Java

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:
How to check if an arraylist contains a value in Java
to do this? How to check if an arraylist contains a value in Java? Thanks  ...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
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
Advertisements
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 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 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 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 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
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
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
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 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
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
arraylist in java
arraylist in java  arraylist in java   Array List Example in java
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
Collection : ArrayList Example
This tutorial contains description of Collection ArrayList with example
Java ArrayList Example
Java ArrayList Example  How can we use array list in java program..."); array.add("Arun"); System.out.println("ArrayList Size...++){ System.out.println("ArrayList Element"+i+" :"+array.get(i
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
Determining If a Preference Node Contains a Specific Value
node 'Java Types' contains the specified value 'Hello' or not. If it finds... Determining If a Preference Node Contains a Specific Value... to determine whether the Preference code contains the specified value
Core Java-ArrayList
Core Java-ArrayList  How do i find duplicates in ArrayList. First i add some elements to ArrayList, then how do i find the duplicates and show the duplicate elements. Give an example
Java arraylist duplicate elements search
Java arraylist duplicate elements search  hi, please help me finding the no of duplicates elements in an arraylist example an arraylist contains elements like: mac raj mohan frank mac tom raj mohan frank result: mac 2 raj
arraylist
arraylist   Using arraylist class , what type of exception are throws, how can write a java programe   An ArrayList can throw... ArraylistException { void buildAlphabet() { ArrayList list = new ArrayList
write a program to create an arraylist and listeterator.and value should be enter through keyboard.
write a program to create an arraylist and listeterator.and value should be enter through keyboard.  write a program to create an arraylist and listeterator.and value should be enter through keyboard
write a program to create an arraylist and listeterator.and value should be enter through keyboard.
write a program to create an arraylist and listeterator.and value should be enter through keyboard.  write a program to create an arraylist and listeterator.and value should be enter through keyboard

Ads