0
我有这样的测试:订单未如预期
@Test
public void testPrioQueue() {
PriorityQueue<Map.Entry<String, Integer>> pq = new PriorityQueue<>((a, b) -> b.getValue() - a.getValue());
pq.add(new SimpleEntry<>("one", 1));
pq.add(new SimpleEntry<>("three", 3));
pq.add(new SimpleEntry<>("two", 2));
List<String> keys = pq.stream().map(e -> e.getKey()).collect(Collectors.toList());
assertEquals(Arrays.asList("three", "two", "one"), keys);
}
我想到时Queue能根据我的比较顺序为:先排序最高值。相反,我得到这样的结果:
java.lang.AssertionError: expected:<[three, two, one]> but was:<[three, one, two]>
我的期望错了吗?
好奇downvote。 – EJP
你应该提到,这*消耗*队列。也就是说,完成后,'queue'将是空的。 –
@JimMischel当然,好主意:) –