在优先级队列中分配随机优先级?

Posted

技术标签:

【中文标题】在优先级队列中分配随机优先级?【英文标题】:Allocate random priority in priority queue? 【发布时间】:2021-04-18 01:32:04 【问题描述】:

我正在为 ServiceDesk 分配列表分配随机优先级(即高、中、低)。

在此之前,我想知道如何在所述优先级队列中存储(和打印)一个数组。这是我目前拥有的。

*更新代码

import java.util.*;
import java.util.Random;

public class Demo 

      public static void main (String[] args) 
          
      String [] names = 
         "Tanya Turner","Juana Downs","Mac Bautista","Leanne Donaldson",
         "Jere Romero","Autumn Hayden","Vincenzo Mckee","Penelope Stanley",
         "Rose Solis","Randal Savage","Delia Hardy","Alisha Hebert","Johnson Jefferson",
         "Gregorio Richard","Jana Simmons","Marian Shepherd","Lynn Pugh","Christine Newman",
         "Essie Frederick","Jewel Oneill","Raul Coleman","Lou Glover","Cora Rush",
         "Damien Norris","Omer Parsons","Adolph Petersen","Dane Proctor","Norbert Ritter",
         "Gloria Dickerson","Ella Morton","Madeline Mccullough","Patsy Berger","Tory Hardin",
         "Sonny Guzman","Kathrine Bond","Teodoro Bolton","Aimee Moran","Jerry Rhodes",
         "Palmer Golden","Zelma Hobbs","Marcella Patel","Freddy Lucas","Ladonna Hutchinson",
         "Devon Boone","Sue Graves","Chadwick Mcpherson","Antonia Rocha","Roseann Peters",
         "Leif Riggs","Judith Mcbride","Frances Simon","Nora Cervantes","Alba Hickman",
         "Concetta Wu","Chelsea Eaton","Dana Rocha","Hubert Kaiser","Phillip Stephenson",
         "Estela Kent","Rene Hughes","Clement Gilmore","Arlie Fernandez","Teodoro Buckley",
         "Daniel Chavez","Jeffry Shepherd","Devin Case","Eric Cooley","Dina Duncan","Teddy Price",
         "Matthew Cooke","Andres Dalton","Clayton Fields","Vito Lara","Lynette Mccann","Greta Choi",
         "Santo Noble","Thurman Douglas","Therese Norton","Juliette Graves","Fran Vang",
         "Forrest Gibbs","Cameron Bernard","Tracy Zhang","Hugh Huerta","Jaime Huynh","Tami Cordova",
         "Jami Mcpherson","Melissa Stein","Rayford Brewer","Tammie Lucero","Marcia Velez","Jasper Watkins",
         "Cora Chapman","Vickie Mccarthy","Gino Pena","Chadwick Hutchinson","Antonio Bryan",
         "Zachery Barnett","Randy Crawford","Laura Barton","Nolan Leach","Deborah Perry",
         "Georgina Sanford","Heidi Anthony","Leah Hester","Dong Swanson","Genevieve Wagner",
         "Russell Todd","Sherman Wolfe","Bo Schultz","Rosalyn Stevens","***e Moses","Jasmine Brock",
         "Guadalupe Andersen","Emilio Horne","Clara Spencer","Raul Levine","Colton Adams","Eve Avila",
         "Donny Murray","Laverne Valentine","Wilbert Gilbert","Justine Terrell","Waldo Nielsen",
         "Erma Mason","Brandie Sullivan","Murray Torres","Angelique Whitney","Shanna Humphrey",
         "Graig Farley","Lindsay Hines","Susanne Compton","Frankie Frank","Saundra Marks","Lorna Skinner",
         "Josephine Boyle","Maynard Wagner","Ronda Potts","Elias French","Gilberto Nguyen"
         ;
      
         String[] priority = "High", "Medium", "Low";
         Random r = new Random();
         
         for (String i : names) 
            System.out.println("Names are: " + i);
            int randomPriority = r.nextInt(priority.length);
            System.out.println("Priority: " + priority[randomPriority]);
         
            

       
               
   

【问题讨论】:

告诉我们你做了什么研究,为什么它看起来不合适。 您尝试了哪些搜索词? 我搜索了各种术语/短语,即“在优先级队列中分配随机优先级”或“在队列中随机优先级”,以为我可以通过 google 或 youtube 找到一些东西。我认为我发现的最接近的事情是在队列中分配随机整数,我试图在那里对代码进行修改,但我一直出错。 【参考方案1】:

听起来您正在寻求有关如何开始的帮助。您正在寻求学习学习方面的帮助。以下是我将如何解决您的问题:

显然你应该使用优先队列。

    编写一个小程序,创建一个优先级队列并将字符串存储到其中,然后将它们打印出来。 定义一个类并将该类的实例而不是字符串存储到优先级队列中。 修改优先队列上的排序标准,注意打印顺序会根据排序标准发生变化。 编写一个函数,创建一个具有随机值的类实例。 编写一个创建所有 100 个类实例的函数。 宣告胜利。

【讨论】:

优先队列可以存储任何type of object。不幸的是 cmets 几乎没有格式化能力。将更新附加到您的问题。 好的开始!现在read about the Java for-each loop 并在代码的最后一段中使用一个来填充优先级队列。 For-each 循环非常感谢您的帮助! 为什么不使用最新的代码更新您的问题?其他人可能会从您的学习经历中学习。【参考方案2】:

优先级队列本身不存储优先级。相反,它提供了一个钩子,询问“这个项目的优先级是什么?”并且您的代码以优先级响应。因此,您需要根据优先级来增加您存储的项目。

换句话说,您需要使用比纯字符串更丰富的数据格式。您可能应该并排存储名称及其优先级。它需要一个自定义类,其中包含 authorpriority 等字段。

enum Priority 
    LOW,
    MEDIUM,
    HIGH


class Ticket 
    String author;
    String description;
    Priority priority;

您可以在创建每个Ticket 时分配随机优先级。

将优先级队列和Ticket 类绑定的粘合剂是一个自定义比较器。使用-> lambda 语法编写一个非常简单:

PriorityQueue<Ticket> queue = new PriorityQueue<>(
    Comparator.comparing(ticket -> ticket.priority)
);

【讨论】:

以上是关于在优先级队列中分配随机优先级?的主要内容,如果未能解决你的问题,请参考以下文章

如何生成随机数并将它们插入优先级队列?

如何选择线程的优先级?

C++ 优先级队列 priority_queue

为啥 STL 优先级队列错误地对我的类对象进行排序

Verilog的分层事件队列

PriorityQueue 源码分析