equals() Comparator Comparator , Java- , compareTo() equals() .
Java == / , .
:
String apple1 = new String("apple");
String apple2 = new String("apple");
System.out.println(apple1.equals(apple2));
StringBuilder app1 = new StringBuilder("apple");
StringBuilder app2 = new StringBuilder("apple");
System.out.println(app1.equals(app2));
, . ? , String equals(), , . , StringBuilder equals(), equals(), Object. , () Object, , .
, , Java, equals(), , , equals(), Object equals()
, , : Apple
public class Apple {
private int weight;
private int cost;
private String color;
, , ? , , - ? equals, .
Apple , , "Apple", . , , , , , , , .
@Override
public boolean equals(Object obj) {
if ( !(obj instanceof Apple)) return false;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Apple other = (Apple) obj;
if (cost != other.cost and weight != other.weight )
return false;
return true;
}
, equals(). Java equals() , . , , .
equals() - , equals(), hashCode(). , hashcode Java, .
Hashcode - , . , (, , ) , . , , , ( ), , , , , hashCode(). hashCode() , equals(). :
-, hashCode() . , hascode() , . , Apple , .. , hashcode(), .
, , equals() true, hashCode() . equals() false , hashCode() . ? ? hashCode() , .
Comparator - , , .
, , Apple : weight price TreeSet.
public class Apple {
private int weight;
private int cost;
, , - weight price? ?
Comparator Comparable .
.
public class Apple {
private int weight;
private int cost;
public static void main(String[] args) {
Apple redApple = new Apple();
redApple.setCost(10);
redApple.setWeight(2);
Apple greenApple = new Apple();
greenApple.setCost(12);
greenApple.setWeight(3);
Set<Apple> apples = new TreeSet<>();
apples.add(redApple);
apples.add(greenApple);
System.out.println(apples);
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public int getCost() {
return cost;
}
public void setCost(int cost) {
this.cost = cost;
}
@Override
public String toString() {
return "Apple [weight=" + weight + ", cost=" + cost + "]";
}
}
, RuntimeError: Apple cannot be cast to java.lang.Comparable, , .
: Comparable
public class Apple implements Comparable<Apple> {
compareTo
@Override
public int compareTo(Object obj) {
int cost = ((Apple) obj).getCost();
return this.getCost() - cost;
}
:
[Apple [weight=2, cost=10], Apple [weight=3, cost=12]]
.
, Apple, Comparable.
Comparator .
implements Comparable, ,
public class Apple {
@Override
public String toString() {
return "Apple [weight=" + weight + ", cost=" + cost + "]";
}
:
public class AppleComparator implements Comparator<Apple> {
@Override
public int compare(Apple app1, Apple app2) {
return app1.getCost() - app2.getCost();
}
}
Comparator ,
Set<Apple> apples = new TreeSet<>(new AppleComparator());
, .
Comparator, Comparable, , TreeSet.
: Comparator equals(). Comparable (compareTo()) equals().
- Apple , Comparable,
. compareTo() 0, , equals() true, .
, compareTo(), iff x.equals(y) , x.compareTo(y) 0. , Comparable , Java- , compareTo() equals() .
compareTo(), :
public class Apple implements Comparable<Apple> {
private int weight;
private int cost;
private String color;
public boolean equals(Object obj) {
if(!(obj instanceof Apple)) {
return false;
}
Apple other = (Apple) obj;
return this.weight == other.weight;
}
public int compareTo(Apple obj) {
return this.cost.compareTo(obj.cost); }
}
Apple , . . compareTo() 0 Apple , , compareTo() equals().