📜  Java中的可比接口与示例

📅  最后修改于: 2022-05-13 01:54:19.746000             🧑  作者: Mango

Java中的可比接口与示例

Comparable 接口用于将同一类的对象与该类的实例进行比较,它为用户定义类的对象提供数据排序。该类必须实现Java.lang.Comparable接口来比较它的实例,它提供了 compareTo 方法,该方法接受该类对象的参数。在本文中,我们将看到如何根据不同的比较参数对不同数据类型对的数组进行排序。

使用可比接口

  • 在这个方法中,我们将从 Pair 类中的Java.lang包中实现 Comparable 接口。
  • Comparable 接口包含方法compareTo来决定元素的顺序。
  • 覆盖 Pair 类中的compareTo方法。
  • 创建一个 Pairs 数组并填充该数组。
  • 使用Arrays.sort ()函数对数组进行排序。

示例 1

给定一个由字符串和整数类型的两个字段组成的对数组。您必须按字典顺序升序对数组进行排序,如果两个字符串相同,则根据它们的整数值对其进行排序。

示例 I/O:

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}, {"efg", 1}, {"zaa", 1} }
Java
import java.io.*;
import java.util.*;
  
class Pair implements Comparable {
    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]);
        }
    }
}


Java
import java.io.*;
import java.util.*;
  
class Pair implements Comparable {
    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:
(abc, 3);
(a, 4);
(bc, 5);
(a, 2);

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

示例 2

给定一个 Pairs 数组,该数组由两个具有名字和姓氏的字符串组成。您必须按名字的升序字典顺序对数组进行排序,如果两个字符串相同,则根据它们的姓氏进行排序。

示例 I/O:

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 {
    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 compatible对具有不同数据类型的用户定义对进行了排序。