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









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
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
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?
in iOS. In Java programming language you can use the indexOf method... clicks on the 'Check If Exists' button application will find the string and show... how to run the example code: In the .h file you can define the function
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
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
Java ArrayList
Java ArrayList   How to declare unique ArrayList
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
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 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
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
arraylist in java
arraylist in java  arraylist in java   Array List Example in java
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
Determining If a Preference Node Contains a Specific Value
checks whether the Preference node 'Java Types' contains the specified value... Determining If a Preference Node Contains a Specific Value... to determine whether the Preference code contains the specified value
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
Java arraylist, linkedlist
Java arraylist, linkedlist  What is the major difference between LinkedList and ArrayList
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
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
how to check the radio button automatically depending up on the value of database when editing the form...
how to check the radio button automatically depending up on the value...;/form> </body> </html> 2)edit.jsp: <%@page language="java"%>...()){ %> <tr> <td><input type="text" name="name" value="<
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
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
java code for conversion of arraylist to string - Development process
java code for conversion of arraylist to string  Hi, i have an arraylist contains [a,b,value,c,d,value]when ever value comes it will split... by "value" as a delimiter  Hi friend, Code to solve the problem
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
updating rows which contains same id, different value for each row
updating rows which contains same id, different value for each row  ... rows with sid 5. Like this table may contains multiple records with same sid. Here my requirement is "I have to update those records which contains same sid
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"         
Fixed Value check using struts validator framework
Fixed Value check using struts validator framework  Hi All, can anyone tell me how to use struts validator framework for fixed value check. eg. country='India'; Thanks in advance
Java ArrayList sublist
It returns the part of the ArrayList as List reference. part is the given range between two indexes. Java Arraylist Sublist Example import...) { String  ar[]={"india","pakistan","United Kingdom","Japan","Korea"}; ArrayList
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
Java ArrayList removeall
the elements from list1 which is present in the list2 Java Arraylist Removeall...","Korea"}; ArrayList list=new ArrayList(); ArrayList list1=new ArrayList
ARRAYLIST CODE - Java Beginners
ARRAYLIST CODE  HELLO SIR,I AM VERY NEW TO JAVA FIELD,HOW TO READ ELEMENTS FROM TWO ARRAYLIST AND PRINT THE ELEMENTS WHICH ARE COMMON TO TWO ARRAYLIST IN ONE LINE AND OTHER IN NEXT LINE,PLZ HELP ME.IN ADVANCE THANK U VERY
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
Check duplicate values and the count in an arraylist and store it in another list
Check duplicate values and the count in an arraylist and store it in another list  Hi All, I have a list which has four fields: year name, month name, file info and count of search string. I need to count month name
Java new arraylist
Java ArrayList object is created with the reference of List interface Object is also created directly by the ArrayList class Java New Arraylist..."}; //ArrayList list=new ArrayList(); List list=new ArrayList(); list.add(ar[0
Java arraylist merge
is joined in to the list1. Example Java Arraylist Merge import... In Java Two arrays can be joined by using the Collection list...};         List list1=new ArrayList();         List list2=new ArrayList
maximum size of arraylist - java
maximum size of arraylist - java  1.what is the maximum size of arraylist? 2.what is the drawback of arralist? 2.what is the drawback of JDBC? 4.how many access specifier and how many access modifier in java? 5.what

Ads