在ArrayList中搜索多个参数

时间:2017-08-29 15:00:04

标签: java arraylist collections

昨天,我在技术回合中获得了以下任务。我不希望你执行所有的任务,我试过自己,但我坚持第3个问题。我的问题是我如何通过注册号实现搜索?因为根据问题5,它应该更有效率。我尝试使用HashMap但无法解决它。

  1. 按字母顺序维护一个狗列表,首先是名称,然后是品种。
  2. 提供添加新犬的方法。
  3. 提供按注册号搜索的方法。
  4. 提供按名称搜索的方法。
  5. 采用最有效的搜索技术。
  6. 接受初始狗列表的构造函数。
  7. 可以采用哪种简单的构造来改善Dog类
  8. 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);
            }
        };
    }
    

1 个答案:

答案 0 :(得分:1)

有多种方法可以解决问题。

首先解决方案是使用Java 8 Stream API。您将能够搜索,过滤结果并返回过滤结果。如果您没有过于复杂的逻辑且没有太多条目,这是一个很好的approuch。如果你有更多的参赛作品,我会选择其他解决方案。

第二种解决方案是使用您想要搜索的特定键的多个地图。搜索名称时,实现可能会变得更复杂(多于一只狗可能具有相同的名称)。根据您要查找的内容,您可以使用给定的地图来处理此案例。

第三种解决方案(可能有点过大)......如果你想延长它一段时间,你可以去寻找一个真正的搜索引擎。 Elasticsearch也作为嵌入式搜索引擎存在。但正如我所说,这可能有点过大,只有你有大量的数据和不同的字段才能搜索和组合。

我也对其他解决方案感兴趣...