Java:向量排序与集合排序

Java: Vector sort vs Collection sort

我从来没有愉快地使用过下面的代码,现在我偶然发现了一个作业,我想在其中争论哪些最常用,以及为什么。

我们有两个例子,

public Vector<Integer> sort(Vector<Integer> integers) { ... }

public Collection<Integer> sort(Collection<Integer> integers) {}

本质上,我们要争论的是这两个示例中的哪一个是排序事物的最佳解决方案。这两个哪个用得最多,为什么?

Essentially, we're to argument which of these two examples is the best solution to sorting things. Which of these two are used the most, and why?

很多评论,但我真的没有看到答案。所以:现在更多使用第二个。第一个是旧方法,从早期 Java 天回来。您仍然可以使用它,但现在人们主要使用第二种,因为它显然对使用它的人的要求较低(特别是您正在排序的集合不需要是 Vector)。

各有优缺点。我认为你被要求在两个糟糕的选择之间做出选择(也许是故意的)。

正如许多人所说,Vector 已过时。它不应该被使用。

但是……Collections.sort 方法采用 List 而不是 Collection 是有原因的:如何对无序 Set 进行排序?如何对已经具有所需顺序的集合(如 TreeSet)进行排序?

由于对 Collection 进行排序的概念定义不明确,采用 Vector 的方法可能是更好用的方法,即使不应该使用 Vector 本身。