13.Java comparable interface, readable interface and iterable interface

Source: Internet
Author: User
Tags comparable iterable

1.Comparable Interface Description: Can be compared (sortable)

Example: sequencing by the Y attribute of MyClass

Class MyClass implements comparable<myclass>{    private int x;    private int y;    Public MyClass (int x,int y) {        this.x=x;        this.y=y;    }    @Override public    int compareTo (MyClass o) {        //in ascending order by Y        return y<o.y?-1: (y==o.y?0:1);    }    @Override public    String toString () {        return x+ "@" +Y;    }} The main function list<myclass> list=new arraylist<> (arrays.aslist (New MyClass, New MyClass (3,1), New MyClass ( 2, 3))); Collections.sort (list); SYSTEM.OUT.PRINTLN (list); results: [email protected], [email protected], [email protected]

2.Comparator Interface Description: Iterator, the parameter of the sort function

Example: Sorting by the X attribute of MyClass

Class myclass{public    int x;    public int y;    Public MyClass (int x,int y) {        this.x=x;        this.y=y;    }    @Override public    String toString () {        return x+ "@" +Y;    }} Class Mycomparator implements comparator<myclass>{    @Override public    int compare (MyClass o1,myclass O2 ){
Return o1.x<o2.x?-1: (o1.x==o2.x? (o1.y<o2.y?-1: (o1.y==o2.y?0:1)) : 1);


Main function
List<myclass> list=new arraylist<> (arrays.aslist (New MyClass), New MyClass (3,1), New MyClass (3,3), new MyClass (3,2), new MyClass (2,3));
Collections.sort (List,new mycomparator ());
SYSTEM.OUT.PRINTLN (list);
Results: [Email protected], [email protected], [email protected], [email protected], [email protected]]

3.Iterable interface

Description: Can iterate, after implementing the Iterable<t> interface, you can use the foreach traversal

Example: a bit redundant, the list can be traversed with foreach directly

Class MyClass implements iterable<integer>{public    list<integer> list=new arraylist<> ( Arrays.aslist (New Integer (1), new Integer (2), new Integer (3)));    @Override public    iterator<integer> Iterator () {        return list.iterator ();    }    @Override public    String toString () {        return super.tostring ();    }}

Example: Combining iterator interface

Class MyClass implements iterable<integer>{    int[] array=new int[]{1,2,3,4,5,6,7};    int index=0;    @Override public    iterator<integer> Iterator () {        return new iterator<integer> () {            @Override Public            Boolean Hasnext () {                return index<array.length;            }            @Override public            Integer Next () {                return array[index++];            }            @Override public            Void Remove () {                throw new unsupportedoperationexception ();            }        };    }    @Override public    String toString () {        return super.tostring ();    }}

4.Readable interface

Description: An input stream that can be used as an scanner object after implementing the readable interface

Example:

public class Tasktimeoutdemo {public    static void Main (string[] args) {        Scanner scanner=new Scanner (New MyClass (2 ));        while (Scanner.hasnext ()) {            System.out.println (Scanner.nextline ());}}}    Class MyClass implements readable{    private int count;    public MyClass (int count) {        this.count=count;    }    @Override public    int Read (Charbuffer cb) throws IOException {        if (count--==0) return-1;        Cb.append ("Aa");        Cb.append ("Bb");        Cb.append ("Cc");        Cb.append ("Dd");        Cb.append ("Ee");        Cb.append ("Ff");        Cb.append ("Gg");        Cb.append ("Hh");        Cb.append ("ii\n");        return 1;}    }

It's important to note when the read function is called.

1.scanner.hasnext () After discovering that there is no data in the buffer, go back to read the data in the input stream, the data of the input stream is stored in the Charbuffer, the above example, once read adds a line of string to the buffer, the entire program calls two read functions.

13.Java comparable interface, readable interface and iterable interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.