For the first question ie to search details by registration number here is the code
import java.util.Comparator;
import java.util.TreeMap;
public class DogSort {
public static void main(String[] args) {
TreeMap<Integer, Dog> listDogs = new TreeMap<>();
listDogs.put(33, new Dog("Max", "German Shepherd", "33"));
listDogs.put(11, new Dog("Gracie", "Rottweiler", "11"));
System.out.println(listDogs);
System.out.println(listDogs.containsKey(11));
System.out.println(listDogs.get(11));
}
}
class Dog {
private String name;
private String breed;
private String registrationNumber;
public Dog(String name, String breed, String registrationNumber) {
this.name = name;
this.breed = breed;
this.registrationNumber = registrationNumber;
}
public static Comparator<Dog> COMPARE_BY_NAME = new Comparator<Dog>() {
public int compare(Dog one, Dog other) {
return one.name.compareTo(other.name);
}
};
@Override
public String toString() {
return "Dog [name=" + name + ", breed=" + breed + ", registrationNumber=" + registrationNumber + "]";
}
}
It is very difficult to get the details of dog by registration number using arraylist, but with map it is quite easy.
And you can override the hashcode and equals method like this but the arraylist compare method works differently.
What you can do is you can write a method which can search details by registration number. The method will have to iterate the list and find the Dog object,and if the list is sorted then you need to implement your own binary search to get the Dog object according to the registration number.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class DogSort {
public static void main(String[] args) {
ArrayList<Dog> listDog = new ArrayList<Dog>();
listDog.add(new Dog("Max", "German Shepherd", "33"));
listDog.add(new Dog("Gracie", "Rottweiler", "11"));
Collections.sort(listDog, Dog.COMPARE_BY_NAME);
System.out.println(listDog);
System.out.println(listDog.contains(new Dog("Max", "Rottweiler", "33")));
}
}
class Dog {
private String name;
private String breed;
private String registrationNumber;
public Dog(String name, String breed, String registrationNumber) {
this.name = name;
this.breed = breed;
this.registrationNumber = registrationNumber;
}
public static Comparator<Dog> COMPARE_BY_NAME = new Comparator<Dog>() {
public int compare(Dog one, Dog other) {
return one.name.compareTo(other.name);
}
};
@Override
public String toString() {
return "Dog [name=" + name + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((registrationNumber == null) ? 0 : registrationNumber.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Dog other = (Dog) obj;
if (registrationNumber == null) {
if (other.registrationNumber != null)
return false;
} else if (!registrationNumber.equals(other.registrationNumber))
return false;
return true;
}
}