如何在哈希映射中管理队列?

huangapple 未分类评论47阅读模式
英文:

How would I go about managing queues within a hashmap?

问题

   static void admitPatient(Map<Integer, Pet> readPets, HashMap<String, PetQueue> vetMap) {
        if (vetMap.isEmpty()) {
            System.out.println("No vets are currently checked in.");
        } else {
            System.out.print("Enter the pet's id number: ");
            int petID = Integer.parseInt(keyb.nextLine());
            Pet p1 = readPets.get(petID);
            if (p1 != null) {
                PetQueue shortestQueue = null;
                Map.Entry<String, PetQueue> entry = vetMap.entrySet().iterator().next();
                int size = entry.getValue().size() + 1;
                String drName = "";
                System.out.println("size" + size);
                for (Map.Entry<String, PetQueue> vetsAndQueue : vetMap.entrySet()) {
                    if (vetsAndQueue.getValue().size() <= size) {
                        shortestQueue = vetsAndQueue.getValue();
                        shortestQueue.enqueue(p1);
                        drName = vetsAndQueue.getKey();
                    }
                }
                vetMap.put(drName, shortestQueue);
                System.out.printf("%s will see Dr. %s\n", p1, drName);
            } else {
                System.out.printf("No pet found with ID %d\n", petID);
            }
        }
    }
英文:

I want to get access of the PetQueue and using the ID number of the pet adds the Pet object (not just the ID number) to the shortest queue available. I am unsure of to go about managing the queues, finding the shortest. Whenever I run this it always adds the pet to the same queue no matter the size.

   static void admitPatient(Map&lt;Integer, Pet&gt; readPets, HashMap&lt;String, PetQueue&gt; vetMap) {
        if (vetMap.isEmpty()) {
            System.out.println(&quot;No vets are currently checked in.&quot;);
        } else {
            System.out.print(&quot;Enter the pet&#39;s id number: &quot;);
            int petID = Integer.parseInt(keyb.nextLine());
            Pet p1 = readPets.get(petID);
            if (p1 != null) {
                PetQueue shortestQueue = null;
                Map.Entry&lt;String, PetQueue&gt; entry = vetMap.entrySet().iterator().next();
                //System.out.println(entry);
                int size = entry.getValue().size() +1;
                String drName = &quot;&quot;;
                System.out.println(&quot;size&quot; + size);
                for (Map.Entry&lt;String, PetQueue&gt; vetsAndQueue : vetMap.entrySet()) {
                   // System.out.println(vetsAndQueue.getValue());
//                    PetQueue  vetsAndQSize = vetsAndQueue.getValue();
                    if (vetsAndQueue.getValue().size() &lt;= size){
                        shortestQueue = vetsAndQueue.getValue();
                        shortestQueue.enqueue(p1);
                        drName = vetsAndQueue.getKey();
                    }
                }
                vetMap.put(drName,shortestQueue);
                
                System.out.printf(&quot;%s will see Dr. %s\n&quot;, p1, drName);
            }else{
                    System.out.printf(&quot;No pet found with ID %d\n&quot;, petID);
                }
            }
        }
}

答案1

得分: 0

以下是您提供的代码部分的翻译:

你用来寻找最短队列的逻辑不太清楚。

首先,你将搜索限制设置为 int size = entry.getValue().size() + 1;,这是迭代器中第一个元素的大小加一。

然后,你使用 if (vetsAndQueue.getValue().size() <= size) 来寻找最短队列。大多数情况下,它只会运行到迭代器中的同一个第一个元素,然后直接运行到 if 代码块,宠物将被直接入列。这就是为什么你的宠物始终在同一个队列中的原因。

size 变量的定义没有意义,因为你无法保证 vetMap 中的第一个队列是最小大小的。

正如其他人所回答的,你只需要等待找到真正最短的队列,以确保你的宠物元素被入队。代码可能如下所示:

static void admitPatient(Map<Integer, Pet> readPets, HashMap<String, PetQueue> vetMap) {
    if (vetMap.isEmpty()) {
        System.out.println("当前没有兽医处于签到状态。");
    } else {
        System.out.print("请输入宠物的ID编号:");
        int petID = Integer.parseInt(keyb.nextLine());
        Pet p1 = readPets.get(petID);
        if (p1 != null) {
            Map.Entry<String, PetQueue> shortestQueueVet = null;
            Map.Entry<String, PetQueue> entry = vetMap.entrySet().iterator().next();
            //System.out.println(entry);
            int size = entry.getValue().size();
            String drName = "";
            System.out.println("大小" + size);
            for (Map.Entry<String, PetQueue> vetsAndQueue : vetMap.entrySet()) {
                if (vetsAndQueue.getValue().size() < size){// 在这里继续寻找最短队列
                    size = vetsAndQueue.getValue().size();
                    shortestQueueVet = vetsAndQueue;
                }
            }
            shortestQueueVet.getValue().enqueue(p1);
            drName = shortestQueueVet.getKey();
            //vetMap.put(drName, shortestQueue); ## 你不需要重新放置元素,因为键保持不变

            System.out.printf("%s 将会看到 %s 医生\n", p1, drName);
        } else {
            System.out.printf("未找到ID为 %d 的宠物\n", petID);
        }
    }
}

注意:代码中的变量名、注释和格式可能会在实际环境中稍有不同,但翻译内容会保持一致。

英文:

The logic you use to find the shorted queue is not clear.

First, you set the search limit to be int size = entry.getValue().size() +1; which is the size of the first element in the iterator plus one.

Then you use the if (vetsAndQueue.getValue().size() &lt;= size) to find the shortest queue. Most of the time it will just run into the same first element in the iterator and just run into the if block and the pet will be enqueued directly. That's why you end up having the pet always in the same queue.

The definition of size variable doesn't make sense since you have no guarantee that the first queue in the vetMap is of the smallest size.

Just as other has answered you just need to wait till you find the real shortest queue to ensue you pet element. It could look like this:

static void admitPatient(Map&lt;Integer, Pet&gt; readPets, HashMap&lt;String, PetQueue&gt; vetMap) {
        if (vetMap.isEmpty()) {
            System.out.println(&quot;No vets are currently checked in.&quot;);
        } else {
            System.out.print(&quot;Enter the pet&#39;s id number: &quot;);
            int petID = Integer.parseInt(keyb.nextLine());
            Pet p1 = readPets.get(petID);
            if (p1 != null) {
                Map.Entry&lt;String, PetQueue&gt; shortestQueueVet = null;
                Map.Entry&lt;String, PetQueue&gt; entry = vetMap.entrySet().iterator().next();
                //System.out.println(entry);
                int size = entry.getValue().size();
                String drName = &quot;&quot;;
                System.out.println(&quot;size&quot; + size);
                for (Map.Entry&lt;String, PetQueue&gt; vetsAndQueue : vetMap.entrySet()) {
                    if (vetsAndQueue.getValue().size() &lt; size){// here keep finding shortest queue
                        size = vetsAndQueue.getValue().size();
                        shortestQueueVet = vetsAndQueue;
                    }
                }
                shortestQueueVet.getValue().enqueue(p1);
                drName = shortestQueueVet.getKey();
                //vetMap.put(drName, shortestQueue); ##you don&#39;t need to re-put the element since the key remains unchanged

                System.out.printf(&quot;%s will see Dr. %s\n&quot;, p1, drName);
            }else{
                    System.out.printf(&quot;No pet found with ID %d\n&quot;, petID);
                }
            }
        }
}

huangapple
  • 本文由 发表于 2020年5月5日 12:40:05
  • 转载请务必保留本文链接:https://java.coder-hub.com/61605913.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定