0
package com.example; 
import java.util.ArrayList; 
import java.util.List;  

public class example {     
   public static void main(String[] args) {          
        Employee e1 = new Employee("Jeny", 22, "Chennai");         
        Employee e2 = new Employee("Justin", 22, "Mumbai");         
        Employee e3 = new Employee("Esha", 22, "Pune");         
        Employee e4 = new Employee("Saumya", 22, "Trichur");          

       List<Employee> al = new ArrayList<>();         
       al.add(e1);         
       al.add(e2);         
       al.add(e3);         
       al.add(e4);

       System.out.println(al);
   }
}

Hi everyone i am new to coding in java and to stack overflow. This is just something that I was trying. I'm trying to understand how will I use the contains method to know if the element "Jeny" or any other element is contained. I don't know if contains method is the only way to do it. But its what Ive been trying at the moment. Help please.

Arsaceus
  • 293
  • 2
  • 19
jenygeorge
  • 11
  • 4
  • you need to override `hascode()` and `equals()` method in `Employee` class and need to provide the implementation to them as per your need so that if you just pass "Jeny" `hashcode()` and `equals()` returns true, in this way you can use contains method. – Vivek Singh Feb 04 '16 at 05:31
  • 3
    I would say `contains` isn't really what you're looking for, unless, you can create an exact copy of the `Employee` and `Employee` overrides the `equals` and `hashcode` method. If you're looking to find an object which matches a particular attribute, you will need to use a `for-loop` to achieve it – MadProgrammer Feb 04 '16 at 05:32
  • just look at the details of Arraylist - [http://beginnersbook.com/2013/12/java-arraylist/ ] with example and explain in detail – Rahul Bhawar Feb 04 '16 at 05:42
  • Are you learning Java 8 or an earlier version? If it's Java 8, then Noor Nawaz's answer does the trick (if you replace `allMatch` with `anyMatch`). If not, you'll need some kind of loop with a condition inside. – Dawood ibn Kareem Feb 04 '16 at 06:10

3 Answers3

1

If I got your question correct, you are looking for a method which takes any one of the parameter (name/age/city) and tell you back that if an Employee with matching property exists or not. For this purpose you can't use ArratList.contains() method, Because contains internally use equals() method (which you implements as per need) which must take an object of type same as Elements in your collection( here ArrayList). More on this java doc about contains()

The possible solution to your scenario is, define your own method which iterates over ArrayList and returns true if property exists for any one the Employee in the List.

Mayank Raghav
  • 640
  • 1
  • 7
  • 17
0

You can also use Java 8 stream and its different methods like

List<Employee> employees =new ArrayList<>();
String name="John";         
if(employees.stream().anyMatch(e->e.getName().equals(name)))
{
  System.out.println("Exists");
}

You can use

employees.stream().allMatch(yourPredicat);
Noor Nawaz
  • 2,175
  • 27
  • 36
0

The contains method of List uses the equals method in the object to evaluate.

So if you want your Employee method to be evaluated according to the name field. You will have to implement equals and hashcode methods in your Employee class as below.

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }

    Employee other= (Employee)obj;
    if (!this.name.equals(other.name)) {
        return false;
    }

    return true;

}

@Override
public int hashCode() {
    //your hashcode logic goes here
}   

Now you can use contains to check for matching objects. For example see below code.

 Employee e5 = new Employee("Jeny", 21, "abc"); 

   if(al.contains(e5)){
       System.out.println("List contains Employee with name:"+e5.getName());
   }

Now this will print

List contains Employee with name:Jeny

You can modify the equals method according to your needs.

vineeth sivan
  • 510
  • 3
  • 18