使用 Comparable 对 LinkedList 进行排序的Java程序
在Java中,LinkedList 是Java.util 包中提供的集合框架的一部分。 LinkedList 是一种线性数据结构,其中所有元素都在连续的内存位置中未排序。 LinkedList 的优点是它是动态的,易于插入和删除任何元素。我们不能直接访问LinkedList的任何节点,我们需要从Head节点开始,遍历一个LinkedList。
Comparable 提供单一排序序列。如果我们使用 Comparable 那么它会影响原始类。 Comparable 接口提供 compareTo() 方法来对元素进行排序。在Java中,可比较由Java.lang 包提供。我们可以通过调用 Collections.sort(List) 对 LinkedList 进行排序 方法。
LinkedList 可以使用 Comparable 按以下顺序排序:
- 升序
- 降序排列
- 使用 Collections.reverseOrder()
- 覆盖 CompareTo() 方法
类型 1:升序
要使用 Comparable 按升序对 LinkedList 进行排序,我们需要为我们的类实现 Comparable 接口并覆盖 CompareTo() 方法以根据特定项目对 LinkedList 进行排序。之后,我们需要使用 Collection.sort() 方法对 LinkedList 进行排序。
Syntax: Collections.sort(Listobject)
例子:
Java
// Java Program to Sort LinkedList using Comparable
// in ascending order
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
// User defined class implements Comparable
class Student implements Comparable {
String Name;
int Id;
int Rank;
Student(String name, int id, int rank)
{
this.Name = name;
this.Id = id;
this.Rank = rank;
}
// Override the compareTo() method
@Override public int compareTo(Student s)
{
if (Rank > s.Rank) {
return 1;
}
else if (Rank == s.Rank) {
return 0;
}
else {
return -1;
}
}
}
public class Main {
// Main driver method
public static void main(String[] args)
{
// Create ine LinkedList for Student object
LinkedList List
= new LinkedList();
List.add(new Student("Meet", 32, 2));
List.add(new Student("Jhon", 11, 5));
List.add(new Student("Sham", 92, 1));
List.add(new Student("William", 86, 3));
List.add(new Student("Harry", 35, 4));
// Print the Unsorted LinkedList
System.out.println("UnSorted List");
for (Student s : List) {
System.out.println(s.Rank + " " + s.Name + " "
+ s.Id);
}
System.out.println();
// sort in ascending order
Collections.sort(List);
// Print the sorted LinkedList
System.out.println("Sorted List");
for (Student s : List) {
// Print the sorted LinkedList
System.out.println(s.Rank + " " + s.Name + " "
+ s.Id);
}
}
}
Java
// Sort LinkedList using Comparable in Java
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
// User defined class implements Comparable
class Student implements Comparable {
String Name;
int Id;
int Rank;
Student(String name, int id, int rank)
{
this.Name = name;
this.Id = id;
this.Rank = rank;
}
// Override the compareTo() method
@Override public int compareTo(Student s)
{
if (Rank > s.Rank) {
return 1;
}
else if (Rank == s.Rank) {
return 0;
}
else {
return -1;
}
}
}
public class Main {
// Main driver method
public static void main(String[] args)
{
// Create ine LinkedList for Student object
LinkedList List
= new LinkedList();
List.add(new Student("Meet", 32, 2));
List.add(new Student("Jhon", 11, 5));
List.add(new Student("Sham", 92, 1));
List.add(new Student("William", 86, 3));
List.add(new Student("Harry", 35, 4));
// Print the Unsorted LinkedList
System.out.println("UnSorted List");
for (Student s : List) {
System.out.println(s.Rank + " " + s.Name + " "
+ s.Id);
}
System.out.println();
// sort in descending order
Collections.sort(List, Collections.reverseOrder());
// Print the sorted LinkedList
System.out.println("Sorted List");
for (Student s : List) {
// Print the sorted LinkedList
System.out.println(s.Rank + " " + s.Name + " "
+ s.Id);
}
}
}
Java
// Sort LinkedList using Comparable in Java
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
// User defined class implements Comparable
class Student implements Comparable {
String Name;
int Id;
int Rank;
Student(String name, int id, int rank)
{
this.Name = name;
this.Id = id;
this.Rank = rank;
}
// Override the compareTo() method
@Override public int compareTo(Student s)
{
// Changed the Comparision logic
if (Rank < s.Rank) {
return 1;
}
else if (Rank == s.Rank) {
return 0;
}
else {
return -1;
}
}
}
public class Main {
// Main driver method
public static void main(String[] args)
{
// Create ine LinkedList for Student object
LinkedList List
= new LinkedList();
List.add(new Student("Meet", 32, 2));
List.add(new Student("Jhon", 11, 5));
List.add(new Student("Sham", 92, 1));
List.add(new Student("William", 86, 3));
List.add(new Student("Harry", 35, 4));
// Print the Unsorted LinkedList
System.out.println("UnSorted List");
for (Student s : List) {
System.out.println(s.Rank + " " + s.Name + " "
+ s.Id);
}
System.out.println();
// sort in ascending order
Collections.sort(List);
// Print the sorted LinkedList
System.out.println("Sorted List");
for (Student s : List) {
// Print the sorted LinkedList
System.out.println(s.Rank + " " + s.Name + " "
+ s.Id);
}
}
}
UnSorted List
2 Meet 32
5 Jhon 11
1 Sham 92
3 William 86
4 Harry 35
Sorted List
1 Sham 92
2 Meet 32
3 William 86
4 Harry 35
5 Jhon 11
时间复杂度: O(n log n)
类型 2:降序
要按降序对 LinkedList 进行排序,我们有两种方法。
A. 使用 Collections.reverseOrder()
在这种方法中,我们必须首先按升序对 LinkedList 进行排序,然后使用 Collections.reverseOrder() 我们可以反转已排序 LinkedList 的顺序。
Syntax: Collections.sort(Listobject,Collections.reverseOrder())
例子:
Java
// Sort LinkedList using Comparable in Java
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
// User defined class implements Comparable
class Student implements Comparable {
String Name;
int Id;
int Rank;
Student(String name, int id, int rank)
{
this.Name = name;
this.Id = id;
this.Rank = rank;
}
// Override the compareTo() method
@Override public int compareTo(Student s)
{
if (Rank > s.Rank) {
return 1;
}
else if (Rank == s.Rank) {
return 0;
}
else {
return -1;
}
}
}
public class Main {
// Main driver method
public static void main(String[] args)
{
// Create ine LinkedList for Student object
LinkedList List
= new LinkedList();
List.add(new Student("Meet", 32, 2));
List.add(new Student("Jhon", 11, 5));
List.add(new Student("Sham", 92, 1));
List.add(new Student("William", 86, 3));
List.add(new Student("Harry", 35, 4));
// Print the Unsorted LinkedList
System.out.println("UnSorted List");
for (Student s : List) {
System.out.println(s.Rank + " " + s.Name + " "
+ s.Id);
}
System.out.println();
// sort in descending order
Collections.sort(List, Collections.reverseOrder());
// Print the sorted LinkedList
System.out.println("Sorted List");
for (Student s : List) {
// Print the sorted LinkedList
System.out.println(s.Rank + " " + s.Name + " "
+ s.Id);
}
}
}
UnSorted List
2 Meet 32
5 Jhon 11
1 Sham 92
3 William 86
4 Harry 35
Sorted List
5 Jhon 11
4 Harry 35
3 William 86
2 Meet 32
1 Sham 92
时间复杂度: O(n log n)
B. 覆盖 CompareTo() 方法
在这种方法中,我们必须重写 compareTo() 方法,使其按降序返回 LinkedList。
例子:
Java
// Sort LinkedList using Comparable in Java
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
// User defined class implements Comparable
class Student implements Comparable {
String Name;
int Id;
int Rank;
Student(String name, int id, int rank)
{
this.Name = name;
this.Id = id;
this.Rank = rank;
}
// Override the compareTo() method
@Override public int compareTo(Student s)
{
// Changed the Comparision logic
if (Rank < s.Rank) {
return 1;
}
else if (Rank == s.Rank) {
return 0;
}
else {
return -1;
}
}
}
public class Main {
// Main driver method
public static void main(String[] args)
{
// Create ine LinkedList for Student object
LinkedList List
= new LinkedList();
List.add(new Student("Meet", 32, 2));
List.add(new Student("Jhon", 11, 5));
List.add(new Student("Sham", 92, 1));
List.add(new Student("William", 86, 3));
List.add(new Student("Harry", 35, 4));
// Print the Unsorted LinkedList
System.out.println("UnSorted List");
for (Student s : List) {
System.out.println(s.Rank + " " + s.Name + " "
+ s.Id);
}
System.out.println();
// sort in ascending order
Collections.sort(List);
// Print the sorted LinkedList
System.out.println("Sorted List");
for (Student s : List) {
// Print the sorted LinkedList
System.out.println(s.Rank + " " + s.Name + " "
+ s.Id);
}
}
}
UnSorted List
2 Meet 32
5 Jhon 11
1 Sham 92
3 William 86
4 Harry 35
Sorted List
5 Jhon 11
4 Harry 35
3 William 86
2 Meet 32
1 Sham 92
时间复杂度: O(n log n)