为优先级队列提供更多元素?

时间:2016-12-24 05:10:46

标签: java priority-queue

我想知道为什么以下的输出是[12,15,12]?我知道优先级队列通过堆来对其元素进行排序,但是如何在15之前放置12?非常感谢! :)

public void JSON_DATA_WEB_CALL() {

    Map<String, Object> jsonParams = new HashMap<>();
    jsonParams.put("name", "value");

    JsonObjectRequest postRequest = new JsonObjectRequest(Request.Method.POST, GET_JSON_DATA_HTTP_URL,

            new JSONObject(jsonParams),
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {

                    Log.e("Res", response.toString());

                    // Do your stuff here

                    JSON_PARSE_DATA_AFTER_WEBCALL(response);


                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    //   Handle Error
                }
            }) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<>();
            headers.put("Content-Type", "application/json; charset=utf-8");
            headers.put("User-agent", System.getProperty("http.agent"));
            return headers;
        }
    };
    queue.add(postRequest);
}



public void JSON_PARSE_DATA_AFTER_WEBCALL(String response) {

    try {

        JSONArray jsonArray = new JSONArray(response);

        for (int i = 0; i < jsonArray.length(); i++) {

            GetDataAdapterRiwayat GetDataAdapter2 = new GetDataAdapterRiwayat();

            JSONObject json = null;

            try {

                json = jsonArray.getJSONObject(i);

                GetDataAdapter2.setIdJalanRiwayat(json.getString(JSON_ID_JALAN));
                GetDataAdapter2.setNamaJalanRiwayat(json.getString(JSON_NAMA_JALAN));
                GetDataAdapter2.setLatitudeRiwayat(json.getString(JSON_LATITUDE));
                GetDataAdapter2.setLongitudeRiwayat(json.getString(JSON_LONGITUDE));
                GetDataAdapter2.setFotoSebelumRiwayat(json.getString(JSON_FOTO_SEBELUM));
                GetDataAdapter2.setFotoSetelahRiwayat(json.getString(JSON_FOTO_SETELAH));
                GetDataAdapter2.setIdRiwayat(json.getString(JSON_ID_RIWAYAT));

            } catch (JSONException e) {

                e.printStackTrace();
            }
            GetDataAdapter1.add(GetDataAdapter2);
        }

        recyclerViewadapterRiwayat = new RecyclerViewAdapterRiwayat(GetDataAdapter1, this);

        recyclerView.setAdapter(recyclerViewadapterRiwayat);

    } catch (Exception ex) {
        ex.printStackTrace();
    }


}

1 个答案:

答案 0 :(得分:2)

你应该养成为JDK课程咨询Javadoc的习惯。

System.out.println(q)根据its documentation调用String.valueOf(q),根据its documentation调用q.toString(),根据its documentation使用该订单来自q.iterator()的元素,根据its documentation没有按任何特定顺序排列。

如果你考虑如何实现优先级队列(通常作为某种堆结构),这是有道理的:堆不跟踪元素的顺序,而不是确保每个节点都有一个较小的值比其所有后代节点。要以有意义的顺序返回元素,需要最坏情况的O( n log n )时间。