2017-08-29 37 views
1

昨天,我在技術回合中得到了以下任務。我不希望你執行所有的任務,我嘗試過自己,但我堅持問題3.我的問題是我如何實現通過註冊號搜索?因爲按照問題5,它應該更有效率。我嘗試了HashMap,但無法解決它。在具有多個參數的ArrayList中搜索

  1. 按字母順序維護一個名單,首先是名字,然後是品種。
  2. 提供添加新狗的方法。
  3. 提供按註冊號碼進行搜索的方法。
  4. 提供按名稱搜索的方法。
  5. 採用最有效的搜索技術。
  6. 構造函數接受狗的初始列表。
  7. 可以做什麼簡單的結構來提高狗類

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); 
     } 
    }; 
} 
+5

'HashMap'是正確的方向。你嘗試了什麼,什麼不起作用? – Thomas

+0

@Thomas我有代碼,但不知道如何顯示代碼?我無法進入答案部分。 – jParmar

+0

請將其編輯爲您的問題。 – Thomas

回答

1

有解決辦法複式問題。

第一種解決方案是使用Java 8 Stream API。您將能夠搜索,過濾結果並返回過濾結果。如果你沒有太複雜的邏輯和不太多的條目,這是一個很好的approuch。如果你有更多的條目,我會去尋求另一種解決方案。

第二種解決方案是使用多個地圖,並使用您想要搜索的特定鍵。在搜索名字時,實現可能會變得更復雜一些(更多的一隻狗可能具有相同的名稱)。根據你在找什麼,你可以使用給定的地圖爲這種情況。

第三個解決方案(也許有點過大)...如果你打算延長它一段時間,你可以看看一個真正的搜索引擎。 Elasticsearch也作爲嵌入式搜索引擎存在。但正如我所說,這可能有點過大,只有在您有大量數據和不同字段進行搜索和組合時纔有意義。

我對其他解決方案以及...