In Java, the term “contains list java” is used to check whether a particular object or element is present in a list or collection.
The ArrayList method contains() is used to check specified elements in List.
Contains Syntax
public boolean contains(Object o)
- Parameters: The method takes a single parameter, which is the object or element that you want to check for in the list.
- Return Value: The method returns a boolean value, which is “true” if the list contains the specified element, and “false” otherwise.
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);
numbers.add(40);
numbers.add(50);
boolean containsElement = numbers.contains(30);
System.out.println("Contains 30: " + containsElement);
containsElement = numbers.contains(100);
System.out.println("Contains 100: " + containsElement);
}
}
The contains method is then used to check if the list contains a specific element. In this case, the code checks if the numbers list contains the element 30 by passing it as an argument to the contains method. The result is stored in the containsElement boolean variable.
Similar way we again check for value 100.
Finding a name in String Array List
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
fruits.add("Mango");
boolean containsApple = fruits.contains("Apple");
System.out.println("Contains Apple: " + containsApple);
boolean containsGrapes = fruits.contains("Grapes");
System.out.println("Contains Grapes: " + containsGrapes);
}
}
Here an ArrayList is created and we find the value “Apple” using contains method.
Checking Student name in ArrayList
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList studentList = new ArrayList<>();
studentList.add("Ram");
studentList.add("Mohan");
studentList.add("Sohan");
studentList.add("Namita");
String studentName = "Nita";
boolean isPresent = studentList.contains(studentName);
if (isPresent) {
System.out.println(studentName + " is present in the student list.");
} else {
System.out.println(studentName + " is not present in the student list.");
}
}
}
The code creates an ArrayList called studentList and adds four student names to it. It then checks if the name “Nita” is present in the list using the contains method. Based on the result, it prints whether “Nita” is present or not.
Checking an object in List of Objects
import java.util.ArrayList;
public class Student {
private int id;
private int rollNo;
private String name;
private String mailId;
private String mobileNo;
public Student(int id, int rollNo, String name, String mailId, String mobileNo) {
this.id = id;
this.rollNo = rollNo;
this.name = name;
this.mailId = mailId;
this.mobileNo = mobileNo;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Student [id=" + id + ", rollNo=" + rollNo + ", name=" + name + ", mailId=" + mailId + ", mobileNo=" + mobileNo + "]";
}
public static void main(String[] args) {
ArrayList studentList = new ArrayList<>();
Student first=new Student(1, 101, "Ram Kumar", "[email protected]", "1234567890");
Student second=new Student(2, 102, "Mohan Kumar", "[email protected]", "9876543210");
Student third=new Student(3, 103, "Sohan Kumar", "[email protected]", "5678901234");
Student fourth=new Student(4, 104, "Naman Kumar", "[email protected]", "9870123456");
studentList.add(first);
studentList.add(second);
studentList.add(third);
studentList.add(fourth);
Student searchStudent = first;
boolean isStudentPresent = studentList.contains(searchStudent);
if (isStudentPresent) {
System.out.println("The student list contains the student: " + searchStudent);
} else {
System.out.println("The student list does not contain the student: " + searchStudent);
}
}
}
In the Student class, there are private instance variables id, rollNo, name, mailId, and mobileNo representing the attributes of a student. The class also provides a constructor to initialize these attributes, as well as a getter method for the name attribute.
The toString method is overridden to provide a string representation of a Student object, displaying all the attributes.
In the main method, an ArrayList named studentList is created to store Student objects. Four Student objects are created using the constructor and added to the studentList.
A search student object is created by referencing the first student object. The contains method is then used to check if the searchStudent is present in the studentList. The result is stored in the isStudentPresent variable.
Read More
how to return array, arraylist, object from a method in java