我用的是jdk1.8
package com.test;import java.util.Arrays;import java.util.List;import java.util.Objects;/** * @author ygr * @description 覆写equals方法 * @date 2021/10/26 */public class EqualsTest { public static void main(String[] args) { List<Persone> list = Arrays.asList(new Person("Xiao", "Ming", 10), new Person("Xiao", "Hong", 41), new Person("Bob", "Smith", 27)); int index = list.indexOf(new Persone("Xiao", "Hong", 41)); System.out.println(index); //1 }}class Person { String firstName; String lastName; int age; public Person(String firstName, String lastName, int age) { this.firstName = firstName; this.lastName = lastName; this.age = age; } public boolean equals(Object obj) { if (obj instanceof Person) { Person person = (Person) obj; return Objects.equals(this.firstName, person.firstName) && Objects.equals(this.lastName, person.lastName) && this.age == person.age; } return false; }}
Sign in to make a reply
云外方天
我用的是jdk1.8