Java比较器问题

时间:2013-02-14 13:29:55

标签: java priority-queue

我使用结果集从MySQL数据库表中获取所有信息并将所有值添加到数组中

public void populateQueueFromDB() {
        // create priority queue
        try {
            String url = "jdbc:mysql://localhost:3306/project";
            Connection conn = DriverManager.getConnection(url, "root", "nbuser");

            PreparedStatement stmt = conn.prepareStatement("SELECT user_id,s_date,e_date,d_date,department,projectname,projectapplication,priority,cores,disk_space,analysis FROM booking");
            ResultSet rs;
            rs = stmt.executeQuery();


            //List<JobRequest> jobList = new ArrayList<JobRequest>();

            while (rs.next()) {
                JobRequest job = new JobRequest();
                User user = new User();
                user.setUserID(rs.getString("user_id"));
                job.setUserID(user.getUserID()); // changes the /user id to the job.setuser id so can call for my queue print.
                job.setStartDate(rs.getString("s_date"));
                job.setEndDate(rs.getString("e_date"));
                job.setDeadDate(rs.getString("d_date"));
                job.setDepartment(rs.getString("department"));
                job.setProjectName(rs.getString("projectname"));
                job.setProjectApplication(rs.getString("projectapplication"));
                job.setPriority(rs.getInt("priority"));
                job.setCores(rs.getInt("cores"));
                job.setDiskSpace(rs.getInt("disk_space"));
                job.setAnalysis(rs.getString("analysis"));

                schedulerPriorityQueue.addJob( job );

            }
            schedulerPriorityQueue.printQueue();

            conn.close();

        } catch (Exception e) {
            System.err.println("Got an exception! ");
            System.err.println(e.getMessage());
        }

    }

从这里开始我调用我的比较器来命令数据,取决于1,2,3的优先级然后对队列进行排序。其他一些代码命名等,但基本上它将我发送到比较器

public class JobQueueComparator implements Comparator<JobRequest> {

    @Override
    public int compare(JobRequest object1, JobRequest object2) {
        if(object1.getPriority() < object2.getPriority()){
            return 1;
        } else {
            return -1;
        }
    }

}

但是我从比较器得到的输出是按优先级3,1然后2进行排序。我已经从网上的例子中对它进行了调整,但是我不明白比较例子的回报。

我如何能够改变那个比较器来排序我的优先级,1是最重要的,3是最不重要的。我确保在将所有结果集添加到数组后打印输出,所以我知道它正在工作,因为它改变了我的订单,只是不知道如何订购我想要的。

由于

编辑: schedulerPriorityQueue

public class Queue {

    private Comparator<JobRequest> comparator = new JobQueueComparator(); //calls my comparator
    private PriorityQueue< JobRequest> scheduledJobs = new PriorityQueue<JobRequest>(100, comparator);

    public void addJob(JobRequest job) {
        // now add job to priority queue
        scheduledJobs.add(job); // add jobs from the resultset into queue
    }

4 个答案:

答案 0 :(得分:2)

让自己轻松自如,并使用Integer及其compareTo方法。

你的比较器方法看起来像这样

    @Override
public int compare(JobRequest object1, JobRequest object2) {
    Integer iO1 = Integer.valueOf(object1.getPriority());
    Integer iO2 = Integer.valueOf(object2.getPriority());
    return -(i01.compareTo(iO2));
}

假设getPriority返回intString

答案 1 :(得分:0)

只需在比较方法上交换标志即可。这将颠倒顺序。

public int compare(JobRequest object1, JobRequest object2) {
    if(object1.getPriority() < object2.getPriority()){
        return -1;
    } else {
        return +1;
    }
}

答案 2 :(得分:0)

如果两个对象相等,则应该从比较器返回0,如果对象1 <1,则应该小于0的整数。对象2,如果对象1> 1,则为大于0的整数。对象2.当前JobQueueComparator永远不会返回0.请尝试以下操作:

public class JobQueueComparator implements Comparator<JobRequest> {
    @Override
    public int compare(JobRequest object1, JobRequest object2) {
        return object1.getPriority() - object2.getPriority();
    }
}

有关详细信息,请参阅Comparator documentation

答案 3 :(得分:0)

您错过了优先级相同的情况。我会用这样的东西:

public class JobQueueComparator implements Comparator<JobRequest> {

@Override
public int compare(JobRequest object1, JobRequest object2) {
        return -(object1.getPriority() - object2.getPriority());
 }
}

如果您考虑这样的优先顺序,这将起作用:1,2,3。