Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用多个参数在ArrayList中搜索_Java_Arraylist_Collections - Fatal编程技术网

Java 使用多个参数在ArrayList中搜索

Java 使用多个参数在ArrayList中搜索,java,arraylist,collections,Java,Arraylist,Collections,昨天,我被赋予以下任务,以在技术回合中实施。我不想让你执行所有的任务,我自己也试过了,但我还是停留在问题3。我的问题是如何实现按注册号搜索?因为根据问题5,它应该更有效。我尝试使用HashMap,但无法解决它 按字母顺序维护狗的列表,首先按名称,然后按品种 提供添加新狗的方法 提供按注册号搜索的方法 提供按名称搜索的方法 采用最有效的搜索技术 接受狗的初始列表的构造函数 可以做什么简单的构造来改进Dog类 DogSort.java public class DogSort { publ

昨天,我被赋予以下任务,以在技术回合中实施。我不想让你执行所有的任务,我自己也试过了,但我还是停留在问题3。我的问题是如何实现按注册号搜索?因为根据问题5,它应该更有效。我尝试使用HashMap,但无法解决它

  • 按字母顺序维护狗的列表,首先按名称,然后按品种
  • 提供添加新狗的方法
  • 提供按注册号搜索的方法
  • 提供按名称搜索的方法
  • 采用最有效的搜索技术
  • 接受狗的初始列表的构造函数
  • 可以做什么简单的构造来改进Dog类
  • 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;       
            }
    
        } 
    }
    
    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);
            }
        };
    }
    
    公共类DogSort{
    公共静态void main(字符串[]args){
    ArrayList listDog=新的ArrayList();
    扫描仪sc=新的扫描仪(System.in);
    添加(新狗(“Max”、“German Shepherd”、“33”));
    添加(新狗(“格雷西”、“罗特维尔”、“11”);
    添加(新狗(“山姆”、“小猎犬”、“22”));
    System.out.println(listDog);
    System.out.println(“选择以下命令之一:”);
    System.out.println(
    “按1:按名称排序\n”+
    “按2:按品种排序\n”+
    “按3:添加新狗\n”+
    “按4:按注册号搜索\n”+
    “按5:Serach按名称\n”);
    int i=sc.nextInt();
    开关(一){
    案例1:Collections.sort(listDog、Dog.COMPARE\u BY\u NAME);
    System.out.println(listDog);
    打破
    案例2:
    收集。分类(listDog,Dog,按品种比较);
    System.out.println(listDog);
    打破
    违约:
    System.out.println(“无效输入”);
    打破
    }
    } 
    }
    
    Dog.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;       
            }
    
        } 
    }
    
    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);
            }
        };
    }
    
    类狗{
    私有字符串名称;
    私家犬;
    私有字符串注册号;
    公犬(犬串名称、犬串品种、犬串注册号){
    this.name=名称;
    这个品种;
    this.registrationNumber=注册号;
    }
    公共字符串getName(){
    返回此.name;
    }
    公共字符串getbride(){
    归还这个。繁殖;
    }
    公共字符串getRegistrationNumber(){
    返回此.registrationNumber;
    }
    公共void集合名(字符串名){
    this.name=名称;
    }
    公共芦苇(弦品种){
    这个品种;
    }
    public void setRegistrationNumber(字符串registrationNumber){
    this.registrationNumber=注册号;
    }
    @凌驾
    公共字符串toString(){
    返回此.name;
    }
    公共静态比较器COMPARE_BY_NAME=新比较器(){
    公共整数比较(狗一,狗另一){
    返回一个.name.compareTo(其他.name);
    }
    };
    公共静态比较器按品种比较=新比较器(){
    公共整数比较(狗一,狗另一){
    返回一个.品种.比较(其他.品种);
    }
    };
    }
    
    有多种方法可以解决这个问题

    第一种解决方案是使用Java8流API。您将能够搜索、筛选结果并返回筛选结果。如果您没有太复杂的逻辑和太多的条目,那么这是一个很好的方法。如果你有更多的条目,我会选择另一个解决方案

    第二种解决方案是使用多个地图和您想要搜索的特定键。搜索名字时,实现可能会变得更复杂一些(超过一只狗可能有相同的名字)。根据您要查找的内容,您可以在本例中使用给定的地图

    第三种解决方案(可能有点过大)。。。如果你想把它扩展一段时间,你可以看看真正的搜索引擎。Elasticsearch也作为嵌入式搜索引擎存在。但正如我所说的,这可能有点过大,只有当您有大量数据和不同的字段要搜索和组合时才有意义


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

    HashMap
    是正确的方向。你尝试了什么,什么不起作用?@Thomas我有代码,但不知道如何显示代码?我不能进入答案区。请改为将其编辑到您的问题中。@Thomas实际上,使用HashMap,我可以解决第三个问题,但同时,我不知道如何使用comparator和HashMap。因此,问题1仍未解决。