对文件使用优先级队列 Reader

Using priority queue with File Reader

我想使用文件 Reader 订购 1-100 范围内的数字。这是非常奇怪的输出。我的代码:

import java.io.*;
import java.util.*;
public class Main {
public static String strLine;
public static int order = 1;
public static void main(String[] args) {
    Queue<String> queue = new PriorityQueue<String>();
    try{
        FileReader fr = new FileReader("File.txt");
        BufferedReader br = new BufferedReader(fr);
        while ((strLine = br.readLine()) != null)   {
            queue.offer(strLine);
        }
        br.close();
    } catch (IOException e){
        System.out.println("File not found");
    }
     while (!queue.isEmpty()){
        System.out.println(order + ".Number: " + queue.poll());
        order++;
    }
} }

File.txt:

81
56
42
3
49
100
61
76
74
79
6
90
34
93
22
71
72
63
67
18
14
5
91
39
53
86
40
45
60
55
48
62
8
4
80
11
87
47
15
46
97
66
43
58
41
54
33
96
82
44
98
27
85
10
32
95
35
24
7
30
83
9
31
99
25
29
57
36
69
50
28
59
12
37
78
73
70
89
65
51
77
88
20
17
21
94
64
19
26
23
92
52
2
16
68
75
38
1
84
13

输出:

1.Number: 1
2.Number: 10
3.Number: 100
4.Number: 11
5.Number: 12
6.Number: 13
7.Number: 14
8.Number: 15
9.Number: 16
10.Number: 17
11.Number: 18
12.Number: 19
13.Number: 2
14.Number: 20
15.Number: 21
16.Number: 22
17.Number: 23
18.Number: 24
19.Number: 25
20.Number: 26
21.Number: 27
22.Number: 28
23.Number: 29
24.Number: 3
25.Number: 30
26.Number: 31
27.Number: 32
28.Number: 33
29.Number: 34
30.Number: 35
31.Number: 36
32.Number: 37
33.Number: 38
34.Number: 39
35.Number: 4
36.Number: 40
37.Number: 41
38.Number: 42
39.Number: 43
40.Number: 44
41.Number: 45
42.Number: 46
43.Number: 47
44.Number: 48
45.Number: 49
46.Number: 5
47.Number: 50
48.Number: 51
49.Number: 52
50.Number: 53
51.Number: 54
52.Number: 55
53.Number: 56
54.Number: 57
55.Number: 58
56.Number: 59
57.Number: 6
58.Number: 60
59.Number: 61
60.Number: 62
61.Number: 63
62.Number: 64
63.Number: 65
64.Number: 66
65.Number: 67
66.Number: 68
67.Number: 69
68.Number: 7
69.Number: 70
70.Number: 71
71.Number: 72
72.Number: 73
73.Number: 74
74.Number: 75
75.Number: 76
76.Number: 77
77.Number: 78
78.Number: 79
79.Number: 8
80.Number: 80
81.Number: 81
82.Number: 82
83.Number: 83
84.Number: 84
85.Number: 85
86.Number: 86
87.Number: 87
88.Number: 88
89.Number: 89
90.Number: 9
91.Number: 90
92.Number: 91
93.Number: 92
94.Number: 93
95.Number: 94
96.Number: 95
97.Number: 96
98.Number: 97
99.Number: 98
100.Number: 99

Process finished with exit code 0

因为它是 String 的队列,所以它按字母顺序排序。如果您想将其保留为 Strings,您可以使用自定义比较器将 Strings 转换为 ints 并比较它们:

// This constructor requires an initial capacity before the comparator
Queue<String> queue = new PriorityQueue<String>(100, new Comparator<String>() {
    public int compare(String s1, String s2) {
        return Integer.parseInt(s1) - Integer.parseInt(s2);
    }
});

另一种选择是将队列的类型更改为Integer并转换输入:

Queue<Integer> queue = new PriorityQueue<Integer>();
...
while ((strLine = br.readLine()) != null)   {
    queue.offer(Integer.parseInt(strLine));
}