Java 學習記錄70 — Sets & HashSet — 2/5

String test = "Test";String test2 = "Test";test == test2; // returns true unexpectedlytest.equals(test2); // returns true as expected
String obj1 = new String("xyz");String obj2 = new String("xyz");if (obj1 == obj2)
System.out.println("obj1==obj2 is TRUE");
else
System.out.println("obj1==obj2 is FALSE");
obj2 = obj1;
if(obj1 == obj2)
System.out.println("obj1==obj2 is TRUE");
else
System.out.println("obj1==obj2 is FALSE");
import java.util.HashSet;
import java.util.Set;
public class HeavenlyBody {
private final String name;
private final double orbitalPeriod;
private final Set<HeavenlyBody> satellites;
public HeavenlyBody(String name, double orbitalPeriod) {
this.name = name;
this.orbitalPeriod = orbitalPeriod;
this.satellites = new HashSet<>();
} public String getName() {
return name;
}
public double getOrbitalPeriod() {
return orbitalPeriod;
}
public boolean addMoon(HeavenlyBody moon) {
return this.satellites.add(moon);
}
public Set<HeavenlyBody> getSatellites() {
return new HashSet<>(this.satellites);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
System.out.println("obj.getClass() is " + obj.getClass());
System.out.println("this.getClass() is " + this.getClass());
if ((obj == null) || obj.getClass() != this.getClass()) {
System.out.println("obj is null or not equal");
return false;
}
String objName = ((HeavenlyBody) obj).getName();
return this.name.equals(objName);
}
}

Overriding equals and hashCode in Java — 039

Are two Java objects with same hashcodes not necessarily equal?

import java.util.HashSet;
import java.util.Set;
public final class HeavenlyBody {
private final String name;
private final double orbitalPeriod;
private final Set<HeavenlyBody> satellites;
public HeavenlyBody(String name, double orbitalPeriod) {
this.name = name;
this.orbitalPeriod = orbitalPeriod;
this.satellites = new HashSet<>();
}
public String getName() {
return name;
}
public double getOrbitalPeriod() {
return orbitalPeriod;
}
public boolean addMoon(HeavenlyBody moon) {
return this.satellites.add(moon);
}
public Set<HeavenlyBody> getSatellites() {
return new HashSet<>(this.satellites);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
System.out.println("equals called");
System.out.println("obj.getClass() is " + obj.getClass());
System.out.println("this.getClass() is " + this.getClass());
if ((obj == null) || (obj.getClass() != this.getClass())) {
System.out.println("obj is null or not equal!");
return false;
}
String objName = ((HeavenlyBody) obj).getName();
return this.name.equals(objName);
}
@Override
public int hashCode() {
System.out.println("hashcode called");
return this.name.hashCode() + 57;
}
}

Working With hashcode() and equals()

Douglas

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
張小雄

張小雄

記錄成為軟體工程師的過程