云计算和java有可比性吗_Java可比接口

云计算和java有可比性吗

Java Comparable interface is a member of collection framework which is used to compare objects and sort them according to the natural order.

Java Comparable接口是集合框架的成员,该集合框架用于比较对象并根据自然顺序对其进行排序。

The natural ordering refers to the behavior of compareTo() method which is defined into Comparable interface. Its sorting technique depends on the type of object used by the interface. If object type is string then it sorts it Lexicographically.

自然排序是指在Comparable接口中定义的compareTo()方法的行为。 它的排序技术取决于接口使用的对象类型。 如果对象类型是字符串,则按字典顺序对其进行排序。

If object type is wrapper class object like: integer or list then it sorts according to their values.

如果对象类型是包装类对象,例如:整数或列表,则它将根据其值排序。

If object type is custom object like: user defined object then sorts according to the defined compareTo() method.

如果对象类型是自定义对象,例如:用户定义的对象,则根据定义的compareTo()方法进行排序。

Classes that implements this interface can be sorted automatically by calling Collections.sort() method. Objects that implement this interface can be used as keys in a sorted map or as elements in a sorted set, without the need to specify a comaprator. Declaration of this interface is given below.

可以通过调用Collections.sort()方法自动对实现此接口的类进行排序。 实现此接口的对象可以用作排序映射中的键,也可以用作排序集中的元素,而无需指定协同映射器。 该接口的声明在下面给出。

宣言 (Declaration )

public interface Comparable<T>

可比方法 (Comparable Method)

It contains single method compareTo() that is given below.

它包含下面给出的单个方法compareTo()

int compareTo(T o) : It compares object with the specified object for order.

int compareTo(T o):比较对象与指定对象的顺序。

The compareTo() function compares the current object with the provided object. This function is already implemented for default wrapper classes and primitive data types but, this function also needs to be implemented for user-defined classes.

compareTo()函数将当前对象与提供的对象进行比较。 对于默认包装类和原始数据类型已经实现了此功能,但是对于用户定义的类也需要实现此功能。

It returns positive integer, if the current object is greater than the provided object.

如果当前对象大于提供的对象,则返回正整数。

If the current object is less than the provided object then it returns negative integer.

如果当前对象小于提供的对象,则返回负整数。

If the current object is equal to the provided object then it returns zero.

如果当前对象等于提供的对象,则它返回零。

例外情况 (Exceptions)

This method returns NullPointerException, if the specified object is null and ClassCastException if the specified object's type prevents it from being compared to this object.

如果指定对象为null,则此方法返回NullPointerException;如果指定对象的类型阻止将其与该对象进行比较,则此方法返回ClassCastException

示例:排序列表 (Example : Sorting list)

Lets take an example to sort an ArrayList that stores integer values. We are using sort() method of Collections class that sort those object which implements Compares interface. Since integer wrapper class implements Comparable so we are able to get sorted objects. See the below example.

让我们以一个示例来对存储整数值的ArrayList进行排序。 我们使用Collections类的sort()方法对实现Compares接口的那些对象进行排序。 由于整数包装器类实现Comparable,因此我们能够获得排序的对象。 请参见以下示例。

import java.util.*;  
public class Demo {  
    public static void main(String a[]){
      // Creating List
        ArrayList<Integer> list = new ArrayList<>();
        // Adding elements
        list.add(100);  
        list.add(2);  
        list.add(66); 
        list.add(22);
        list.add(10);
        // Displaying list
        System.out.println(list);
        // Sorting list
        Collections.sort(list);
        // Displaying sorted list
        System.out.println("Sorted List : "+list);
    }  
}

[100, 2, 66, 22, 10] Sorted List : [2, 10, 22, 66, 100]

[100,2,66,22,10]排序列表:[2,10,22,66,100]

示例:对字符串对象进行排序 (Example: Sorting String objects)

While sorting string objects, the comparable sorts it based on lexicographically. It means a dictionary like sorting order. See the below example.

在对字符串对象进行排序时,可比对象根据词典顺序对其进行排序。 这意味着像排序顺序的字典。 请参见以下示例。

import java.util.*;  
public class Demo {  
    public static void main(String a[]){
      // Creating List
        ArrayList<String> list = new ArrayList<>();
        // Adding elements
        list.add("D");  
        list.add("L");  
        list.add("A"); 
        list.add("Z");
        list.add("C");
        // Displaying list
        System.out.println(list);
        // Sorting list
        Collections.sort(list);
        // Displaying sorted list
        System.out.println("Sorted List : "+list);
    }

[D, L, A, Z, C] Sorted List : [A, C, D, L, Z]

[D,L,A,Z,C]排序列表:[A,C,D,L,Z]

示例:对用户定义的对象进行排序 (Example: Sorting User Defined Object)

If we have custom objects then we have to implement the Comparable interface and override its compareTo() method. Now it will compare based on the logic we defined in our compareTo() method. See the below example.

如果我们有自定义对象,则必须实现Comparable接口并覆盖其compareTo()方法。 现在它将根据我们在compareTo()方法中定义的逻辑进行比较。 请参见以下示例。

import java.util.*;  

class Employee implements Comparable <Employee>
{
    int empId;
    String name;
    public Employee (int empId, String name)
    {
        this.empId=empId;
        this.name=name;
    }
    public String toString()
    {
        return this.empId + " " + this.name;
    }

     // Sorting by empId
    public int compareTo(Employee std){    
        
        return this.empId - std.empId; 
    } 
}

public class Demo {  
    public static void main(String a[]){
      ArrayList <Employee> list = new ArrayList <Employee> ( ); 
        list.add(new Employee(2, "Boman")); 
        list.add(new Employee(1, "Abram")); 
        list.add(new Employee(3, "Dinesh")); 

        // Displaying
        for (int i=0; i<list.size(); i++) 
            System.out.println(list.get(i)); 
  
        // Sorting 
        Collections.sort(list); 

        // Displaying after sorting
        System.out.println("\nAfter Sorting :\n");
        for (int i=0; i<list.size(); i++) 
            System.out.println(list.get(i)); 
    }  
}

2 Boman 1 Abram 3 Dinesh After Sorting : 1 Abram 2 Boman 3 Dinesh

2 Boman 1 Abram 3 Dinesh排序后:1 Abram 2 Boman 3 Dinesh

翻译自: https://www.studytonight.com/java/comparable-in-collection-framework.php

云计算和java有可比性吗

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值