Java: // usage of Comparator and Comparable (output the key-value pairs of the map set in sequence as required ),

Source: Internet
Author: User

Java: // usage of Comparator and Comparable (output the key-value pairs of the map set in sequence as required ),

 

Import java. util .*;

Public class Person implements Comparable <Person> // compare the attributes of Person.

{

Private String name;

Private int age;

Public Person (String name, int age) // initialize the constructor

{

This. name = name;

This. age = age;

}

Public void set (String name, int age) // reset the name and age

{

This. name = name;

This. age = age;

}

Public String getName () // get the name

{

Return name;

}

 

Public int getAge () // get age

{

Return age;

}

Public String toString () // returns the name and age in the form of a String

{

Return name + "," + age;

}

// When duplicate names occur, find another hash address to store data (automatically called at the underlying layer)

Public int hashCode ()

{

Return name. hashCode () + age * 34;

}

// Determine whether the accepted class is a Person class. If not, an exception is thrown (automatically called at the underlying layer)

Public boolean equals (Object obj)

{

If (! (Obj instanceof Person ))

Throw new ClassCastException ("not a Person class ");

Person p = (Person) obj;

Return this. name. equals (p. name) & this. age = p. age;

}

Public int compareTo (Person p) // (compare according to your own requirements)

{

Int num = new Integer (this. age). compareTo (p. age );

Return num = 0? This. name. compareTo (p. name): num;

}

}

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.

TreeMap <Person, String> map = new TreeMap <Person, String> (/* new Mycompare ()*/);

// Add data to the set

Map. put (new Person ("czhangsan1", 11), "beijing ");

Map. put (new Person ("zhangsan5", 15), "nanjing ");

Map. put (new Person ("azhangsan5", 10), "shanghai ");

Map. put (new Person ("zhangsan2", 20), "haierbing ");

Map. put (new Person ("bzhangsan2", 20), "beijing ");

Map. put (new Person ("zhangsan3", 12), "shanghai ");

Map. put (new Person ("zhangsan4", 19), "changchun ");

Map. put (new Person ("zhangsan4", 10), "changchun ");

Map. put (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

Set <Map. Entry <Person, String> entryset = map. entrySet ();

// Obtain the iterator

Iterator <Map. Entry <Person, String> it = entryset. iterator ();

// Use the iterator to retrieve data

While (it. hasNext ())

{

Map. Entry <Person, String> m = it. next ();

Person key = m. getKey (); // retrieve key

String value = m. getValue (); // retrieves the value.

Sop (key + "..." + "adress:" + value); // print the key and value

}

}

}

Class Mycompare implements comparator <Perosn> // customize a comparator

{

Public int compare (Person p1, Person p2)

{

Int num = p1.getName (). compareTo (p2.getName ());

Return num = 0? New Integer (p1.getAge (). compareTo (new Integer (p2.getAge (): num;

}

}

// 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.

Related Article

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.