当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。