1

I'm trying to get the right output for the methods "contain" and "lastIndexOf" using String, but it won't give me the right output for those, in that case, false or true for "contain" and the position of the element for "lastIndexOf". How can I do that please ? Thanks very much.

public class Employee {
    public static final int size = 0;
    String firstName;
    String surname;
    int yearOfBirth;
    String PPSNumber;
    String email;
    String phoneNumber;

    public Employee(String firstName, String surname, int yearOfBirth, String PPSNumber, String email, String phoneNumber) {
        this.firstName = firstName;
        this.surname = surname;
        this.yearOfBirth = yearOfBirth;
        this.PPSNumber = PPSNumber;
        this.email = email;
        this.phoneNumber = phoneNumber;
    }
}

//____________________________________________________//

import java.util.ArrayList;

public class EmployeeManagement {
    public static void main(String[] args) {
        ArrayList<Employee> employeeList = new ArrayList<>();

        employeeList.add(new Employee("Charlie", "Charles", 1991, "234567b", "charlie@b.ie", "7654321"));
        employeeList.add(new Employee("David", "Davies", 1992, "5213452d", "david@d.ie", "352135613"));
        employeeList.add(new Employee("Levi", "Silva", 1990, "1234", "Levi@b.ie", "333333"));
        employeeList.add(new Employee("Gus", "Silva", 1993, "4321", "Gus@b.ie", "444444"));

        for (Employee getName : employeeList) {

            System.out.print(getName.firstName + ",         ");

        }

        System.out.println(" ");

        // contains(Employee)
        Employee Emp = new Employee("Gus", "Silva", 1993, "4321", "Gus@b.ie", "444444");
        System.out.println("Contains String: " + employeeList.contains(Emp));    // can't make give me the right contain answer


        // lastIndexOf()    
        Employee Charlie1 = new Employee("Charlie", "Charles", 1991, "234567b", "charlie@b.ie", "7654321");
        System.out.println("lastIndexOf:  " + employeeList.lastIndexOf(Charlie1));    // can't find the lastIndexOf


    }
}
Josh
  • 13
  • 4

3 Answers3

2

You need to implement equals and hashCode in Employee (or any other class in order to contains, sort or other methods works.

For example, this idea wizard default using all fields:

class Employee {
    public static final int size = 0;
    String firstName;
    String surname;
    int yearOfBirth;
    String PPSNumber;
    String email;
    String phoneNumber;

    public Employee(String firstName, String surname, int yearOfBirth, String PPSNumber, String email, String phoneNumber) {
        this.firstName = firstName;
        this.surname = surname;
        this.yearOfBirth = yearOfBirth;
        this.PPSNumber = PPSNumber;
        this.email = email;
        this.phoneNumber = phoneNumber;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Employee employee = (Employee) o;

        if (yearOfBirth != employee.yearOfBirth) return false;
        if (firstName != null ? !firstName.equals(employee.firstName) : employee.firstName != null) return false;
        if (surname != null ? !surname.equals(employee.surname) : employee.surname != null) return false;
        if (PPSNumber != null ? !PPSNumber.equals(employee.PPSNumber) : employee.PPSNumber != null) return false;
        if (email != null ? !email.equals(employee.email) : employee.email != null) return false;
        return phoneNumber != null ? phoneNumber.equals(employee.phoneNumber) : employee.phoneNumber == null;
    }

    @Override
    public int hashCode() {
        int result = firstName != null ? firstName.hashCode() : 0;
        result = 31 * result + (surname != null ? surname.hashCode() : 0);
        result = 31 * result + yearOfBirth;
        result = 31 * result + (PPSNumber != null ? PPSNumber.hashCode() : 0);
        result = 31 * result + (email != null ? email.hashCode() : 0);
        result = 31 * result + (phoneNumber != null ? phoneNumber.hashCode() : 0);
        return result;
    }
}

Now, just add the desired or mandatory fields only and you will see how contains works.

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
1

As the javadoc of lastIndexOf states, you get -1 if the object is not found. But the reason it is not found, is, that you are dealing with a list of Employee and not a list of String. You could of course implement equals and hashCode on your Employee-class, but that way you are not flexible enough. Instead you should probably just filter your list for the elements you require, e.g.

// Get (or show) all employees, whose name is "some string" using Stream API
employeeList.stream()
            .filter(employee -> employee.firstName.contains("some string"))
            .collect(Collectors.toList()); // or: .forEach(System.out::println);

About getting the last index of your entry.... do you really need such a functionality? If so and you don't have the actual object to query (the Employee object of "Gus") and you do not want to implement hashCode and equals, I suggest, that you just iterate over the list and break on the first occurrence:

int lastIndex = -1;
for (int i = employeeList.size() - 1; i >= 0 ; i--) {
  Employee anEmployee = employeeList.get(i);
  if (anEmployee.firstName.equals("Gus")) {
    lastIndex = i;
    break;
 }
}
System.out.printf("lastIndexOf: %s%n", lastIndex);
Roland
  • 22,259
  • 4
  • 57
  • 84
  • i suppose not use hashCode and equals, just changed the code fixing the type of the object but still doesn't work. – Josh Feb 23 '17 at 12:07
  • If you implemented equals and hashCode correctly, then your code should work. However what you do seems to be filtering/searching. I wouldn't rely on equals/hashCode then, but instead would filter the list depending on your filter criteria. – Roland Feb 23 '17 at 20:52
0

Your employeeList is declared as ArrayList<Employee>, so your employeeList.lastIndexOf("Gus") won't work because the list contains Employee objects while "Gus" is a String so it is nowhere in the list.

About "contain", I can't find your call to ArrayList.contains(*) in your code (provided that's the method you are talking about). Can you point to it ?

SantiBailors
  • 1,596
  • 3
  • 21
  • 44
  • I have changed the code just to what i really need and fixed the type of the object to "Employee", it was String before, The codes I got here work but supposed not the way I did work as I fixed the type of the object? Thanks. – Josh Feb 23 '17 at 12:05
  • @Gab `contains` and `lastIndexOf` need to be able to tell if two `Employee` instances are equal. They do that by calling `equals` on `Employee`. So, as the accepted answer says, you need to override `equals` (and `hashCode`) in your `Employee` class, and put there the logic you want to be used to tell if two `Employee`s are equal. Then both `contains` and `lastIndexOf` will work as you expect. Just wonder what you actually mean by "contains": that it contains an employee with the same surname ? Or same email ? Or with the same values in all those fields ? Put that logic into `Employee.equals`. – SantiBailors Feb 23 '17 at 13:02
  • @Gab About why one should always override `hashCode` when overriding `equals`, you can see [this answer](http://stackoverflow.com/a/2265637/2814308). Overriding `hashCode` is not necessary as long as only the code in your example is concerned, but that code is certainly part of a bigger application, so this doesn't matter and not overriding `hashCode` would be a bad idea if you override `equals`. – SantiBailors Feb 23 '17 at 13:11