當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Comparable Interface用法及代碼示例


Comparable 接口用於將同一類的對象與該類的實例進行比較,它為用戶定義類的對象提供數據排序。該類必須實現 java.lang.Comparable 接口來比較其實例,它提供了compareTo 方法,該方法采用該類的對象的參數。在本文中,我們將了解如何根據不同的比較參數對不同數據類型對的數組進行排序。

使用類似的接口

  • 在這個方法中,我們將實現 Comparable 接口java.lang封裝在Pair類中。
  • Comparable 接口包含方法相比於來決定元素的順序。
  • 覆蓋相比於Pair 類中的方法。
  • 創建一個 Pair 數組並填充該數組。
  • 使用Arrays.sort() 函數對數組進行排序。

示例 1

給定一個由字符串和整數類型的兩個字段組成的 Pair 數組。您必須按字典順序升序對數組進行排序,如果兩個字符串相同,則根據它們的整數值對其進行排序。

輸入/輸出示例:

Input:  { {"abc", 3}, {"a", 4}, {"bc", 5}, {"a", 2} }
Output:  { {"a", 2}, {"a", 4}, {"abc", 3}, {"bc", 5} }

Input:  { {"efg", 1}, {"gfg", 1}, {"cba", 1}, {"zaa", 1} }
Output:  { {"cba", 1}, {"efg", 1}, {"gfg", 1}, {"zaa", 1} }

Java


import java.io.*;
import java.util.*;
class Pair implements Comparable<Pair> {
    String x;
    int y;
    public Pair(String x, int y)
    {
        this.x = x;
        this.y = y;
    }
    public String toString()
    {
        return "(" + x + "," + y + ")";
    }
    @Override public int compareTo(Pair a)
    {
        // if the string are not equal
        if (this.x.compareTo(a.x) != 0) {
            return this.x.compareTo(a.x);
        }
        else {
            // we compare int values
            // if the strings are equal
            return this.y - a.y;
        }
    }
}
public class GFG {
    public static void main(String[] args)
    {
        int n = 4;
        Pair arr[] = new Pair[n];
        arr[0] = new Pair("abc", 3);
        arr[1] = new Pair("a", 4);
        arr[2] = new Pair("bc", 5);
        arr[3] = new Pair("a", 2);
        // Sorting the array
        Arrays.sort(arr);
        // printing the
        // Pair array
        print(arr);
    }
    public static void print(Pair[] arr)
    {
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }
}

輸出:

Before Sorting:
(abc, 3);
(a, 4);
(bc, 5);
(a, 2);

After Sorting:
(a,2)
(a,4)
(abc,3)
(bc,5)

Note: if two strings are the same then the comparison is done based on the value.

示例 2

給定一個 Pairs 數組,由兩個帶有名字和姓氏的字符串組成。您必須按名字的字典順序升序對數組進行排序,如果兩個字符串相同,則根據姓氏進行排序。

輸入/輸出示例:

Input:  { {"raj", "kashup"}, {"rahul", "singh"}, {"reshmi", "dubey"}, {"rahul", "jetli"} }
Output:  { {"rahul", "jetli"}, {"rahul", "singh"}, {"raj", "kashup"}, {"reshmi", "dubey"} }

Input:  { {"abc", "last"}, {"pklz", "yelp"}, {"rpng", "note"}, {"ppza", "xyz"} }
Output:  { {"abc", "last"}, {"pklz", "yelp"}, {"ppza", "xyz"}, {"rpng", "note"} }

Java


import java.io.*;
import java.util.*;
class Pair implements Comparable<Pair> {
    String firstName;
    String lastName;
    public Pair(String x, String y)
    {
        this.firstName = x;
        this.lastName = y;
    }
    public String toString()
    {
        return "( " + firstName + " , " + lastName + " )";
    }
    @Override public int compareTo(Pair a)
    {
        // if the string are not equal
        if (this.firstName.compareTo(a.firstName) != 0) {
            return this.firstName.compareTo(a.firstName);
        }
        else {
            // we compare lastName if firstNames are equal
            return this.lastName.compareTo(a.lastName);
        }
    }
}
public class GFG {
    public static void main(String[] args)
    {
        int n = 4;
        Pair arr[] = new Pair[n];
        arr[0] = new Pair("raj", "kashup");
        arr[1] = new Pair("rahul", "singh");
        arr[2] = new Pair("reshmi", "dubey");
        arr[3] = new Pair("rahul", "jetli");
        // Sorting the array
        Arrays.sort(arr);
        // printing the
        // Pair array
        print(arr);
    }
    public static void print(Pair[] arr)
    {
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }
}

輸出:

Before Sorting:
( raj , kashup )
( rahul , singh )
( reshmi , dubey )
( rahul , jetli )


After Sorting:
( rahul , jetli )
( rahul , singh )
( raj , kashup )
( reshmi , dubey )

在本文中,我們使用 java Compare 對具有不同數據類型的用戶定義對進行排序。



相關用法


注:本文由純淨天空篩選整理自ashutosh44大神的英文原創作品 Comparable Interface in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。