Java: // usage of Comparator and Comparable (sort and output the data of the set as required ):,

Source: Internet
Author: User

Java: // usage of Comparator and Comparable (sort and output the data of the set as required ):,

Import java. util. ArrayList;

Import java. util. Collections;

Import java. util. Comparator;

// Usage of comparator and comparable (output the key-value pairs of the map set in sequence as required)

Import java. util. List;

 

Public class Test

{

Public static <T> void sop (T t)

{

System. out. println (t );

}

Public static void main (String args []) throws Exception

{

// Create a map set to store data <> indicates that the received data is generic, that is, the specified type.

List <Person> list = new ArrayList <Person> ();

// Add data to the set

List. add (new Person ("czhangsan1", "11", "beijing "));

List. add (new Person ("zhangsan5", "15", "nanjing "));

List. add (new Person ("azhangsan5", "10", "shanghai "));

List. add (new Person ("zhangsan2", "20", "haierbing "));

List. add (new Person ("bzhangsan2", "20", "beijing "));

List. add (new Person ("zhangsan3", "12", "shanghai "));

List. add (new Person ("zhangsan4", "19", "changchun "));

List. add (new Person ("zhangsan4", "10", "changchun "));

List. add (new Person ("zhangsan4", "10", "zhengzhou "));

// Converts a map set to a set, because there is no iterator for the map set, and the set has

Collections. sort (list );

System. out. println ("sort by age :");

For (Person person: list)

{

System. out. println (person. toString ());

}

Collections. sort (list, new MyCompare ());

System. out. println ("sort by name :");

For (Person person: list)

{

System. out. println (person. toString ());

}

}

}

 

Class Person implements Comparable <Person> // compare the attributes of Person.

{

Private String name;

Private String age;

Private String address;

Public Person (String name, String age, String address) // Initialization

{

This. name = name;

This. age = age;

This. address = address;

}

Public String getName ()

{

Return name;

}

Public void setName (String name)

{

This. name = name;

}

Public String getAge ()

{

Return age;

}

Public void setAge (String age)

{

This. age = age;

}

Public String getAddress ()

{

Return address;

}

Public void setAddress (String address)

{

This. address = address;

}

@ Override

Public String toString ()

{

Return "name:" + name + ", age:" + age + ", address:" + address;

}

// Sort by age

@ Override

Public int compareTo (Person o)

{

Return this. age. compareTo (o. getAge ());

}

}

 

// Sort by name dictionary

Class MyCompare implements Comparator <Person> // customize a Comparator

{

@ Override

Public int compare (Person o1, Person o2)

{

Return o1.getName (). compareTo (o2.getName ());

}

}

// Note: Both of the above two forms achieve comparison. You can select either of them. Either implement the comparable interface or pass in a custom comparator in the set.

 

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.