Reputation: 500
Yesterday, I was given following task to implement in technical round. I do not want you to implement all the tasks, I tried myself but I stuck at question 3. My question is how do I implement to search by registration number? Because as per question 5, it should be more efficient. I tried with HashMap but could not solve it.
DogSort.java
public class DogSort {
public static void main(String[] args) {
ArrayList<Dog> listDog = new ArrayList<Dog>();
Scanner sc = new Scanner(System.in);
listDog.add(new Dog("Max", "German Shepherd", "33"));
listDog.add(new Dog("Gracie","Rottweiler","11"));
listDog.add(new Dog("Sam", "Beagle", "22"));
System.out.println(listDog);
System.out.println("Select one of the following commands: ");
System.out.println(
"Press 1: Sort by name\n"+
"Press 2: Sort by breed\n" +
"Press 3: Add new dog\n" +
"Press 4: Search by registration number\n" +
"Press 5: Serach by Name\n ");
int i = sc.nextInt();
switch (i){
case 1: Collections.sort(listDog, Dog.COMPARE_BY_NAME);
System.out.println(listDog);
break;
case 2:
Collections.sort(listDog, Dog.COMPARE_BY_BREED);
System.out.println(listDog);
break;
default:
System.out.println("Invalid input");
break;
}
}
}
Dog.java
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 String getName() {
return this.name;
}
public String getBreed() {
return this.breed;
}
public String getRegistrationNumber() {
return this.registrationNumber;
}
public void setName(String name) {
this.name = name;
}
public void setBreed(String breed) {
this.breed = breed;
}
public void setRegistrationNumber(String registrationNumber) {
this.registrationNumber = registrationNumber;
}
@Override
public String toString() {
return this.name;
}
public static Comparator<Dog> COMPARE_BY_NAME = new Comparator<Dog>() {
public int compare(Dog one, Dog other) {
return one.name.compareTo(other.name);
}
};
public static Comparator<Dog> COMPARE_BY_BREED = new Comparator<Dog>() {
public int compare(Dog one, Dog other) {
return one.breed.compareTo(other.breed);
}
};
}
Upvotes: 2
Views: 228
Reputation: 2217
There are mutliple ways to solve the problem.
First solution would be to use Java 8 Stream API. You would be able to search, filter for results and return the filtered result. This is a nice approuch if you don't have a too complex logic and not too much entries. If you would have more entries i would go for another solution.
Second solution would be to use multiple Maps with the specific keys you like to search for. The implementation could get a bit more complex when searching for names (More then one dog could have the same name). Depending on what you're looking for, you could use the given map for this case.
Third solution (and maybe a bit oversized) ... If you're going to extend it over a while you could take a look for a real search engine. Elasticsearch also exists as an embedded search engine. but as i've said, this could be a bit oversized and only makes sense if you have a lot of data and different fields to search and combine.
I'm interested in other solutions as well...
Upvotes: 2