중복 항목 로직에 대한 비교는 equals() 메소드를 사용하여 Java에서 수행됩니다.
myString.equals ("compare this");
고객 유형의 개체를 비교해야 할 때 equals 메서드를 재정의해야합니다.
Student student1=new Student();
Student student2=new Student();
student1.equals(student2);
그러나 equals() 메소드를 재정의 할 때는주의해야합니다. 일부 고유 ID를 기반으로 비교 근거를 제공해야합니다. 예를 들어, 다음 구현에서는 롤 번호를 비교를위한 고유 ID로 사용합니다.
public class Student {
private int rollNo;
private String name;
private String address;
// Getters and Setters
@Override
public int hashCode() {
// Overide this only when you use Hashing implementations
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
System.out.println("Not Equal due to NULL");
return false;
}
if (this == obj) {
System.out.println("Equals due to same reference");
return true;
}
if (getClass() != obj.getClass()) {
System.out.println("Different Type");
return false;
}
Student other = (Student) obj;
if (rollNo == other.rollNo) {
System.out.println("RollNo " + rollNo + " EQUALS " + other.rollNo);
return true;
}
if (rollNo != other.rollNo) {
System.out.println("RollNo " + rollNo + " NOT EQUALS " + other.rollNo);
return false;
}
System.out.println("Default is FALSE");
return false;
}
}
진지하게, 사람, 가까운/아래 투표는 무엇입니까?어쩌면 완벽한 질문은 아니지만, 먼저 남자에게 강타하는 대신에/증언하라고 부탁하십시오. – MaDa