How to check in java if Set contains object with some string value?

后端 未结 6 1523
北荒
北荒 2020-12-16 10:19

I have Set of objects. Each object has String value.

I need to select all objects that have this value equal to \"direction\".

Is it possibl

6条回答
  •  眼角桃花
    2020-12-16 10:53

    Yes this is possible by overwriting the equals() method.

    @Override 
    public boolean  equals (Object object) {
    
    }
    

    You just want to check everything works in the equals method.

    Code:

    package com.webapp.test;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class EmployeeModel {    
    
        public EmployeeModel(String name, String designation, long age) {
            this.name = name;
            this.designation = designation;
            this.age = age;
        }
    
        private String name;
        private String designation;
        private long age;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getDesignation() {
            return designation;
        }
    
        public void setDesignation(String designation) {
            this.designation = designation;
        }
    
        public long getAge() {
            return age;
        }
    
        public void setAge(long age) {
            this.age = age;
        }
    
        @Override
        public boolean equals (Object object) {
            boolean result = false;
            if (object == null || object.getClass() != getClass()) {
                result = false;
            } else {
                EmployeeModel employee = (EmployeeModel) object;
                if (this.name == employee.getName() && this.designation == employee.getDesignation() && this.age.equals(employee.getAge())) {
                    result = true;
                }
            }
            return result;
        }
    }
    
    public static void main(String args[]) {
        EmployeeModel first = new EmployeeModel("Sameer", "Developer", 25);
        EmployeeModel second = new EmployeeModel("Jon", "Manager", 30);
        EmployeeModel third = new EmployeeModel("Priyanka", "Tester", 24);
    
        List employeeList = new ArrayList();
        employeeList.add(first);
        employeeList.add(second);
        employeeList.add(third);
    
        EmployeeModel checkUserOne = new EmployeeModel("Sameer", "Developer", 25);
        System.out.println("Check checkUserOne is in list or not ");
        System.out.println("Is checkUserOne Present = ? " + employeeList.contains(checkUserOne));
    
        EmployeeModel checkUserTwo = new EmployeeModel("Tim", "Tester", 24);
        System.out.println("Check checkUserTwo is in list or not");
        System.out.println("Is checkUserTwo Present = ? " + employeeList.contains(checkUserTwo));
    
    }
    

    Output:

    Check checkUserOne is in list or not 
    Is checkUserOne Present = ? true
    Check checkUserTwo is in list or not 
    Is checkUserTwo Present = ? false
    

提交回复
热议问题