[Java class set] _ notes for sorting and repeating elements (test by instance)

Source: Internet
Author: User
Tags comparable

[Java class set] _ notes for sorting and repeating Elements

Objectives of this chapter:

Master the sorting principle of treeset
Understanding the definition of repeated elements in the Set Interface

3. Details:

The contents of the treeset class can be sorted, so now I will give a class at will to see if it can be sorted.

Import Java. util. set; import Java. util. treeset; class person {private string name; private int age; Public Person (string name, int age) {This. name = Name; this. age = age;} Public String tostring () {return "name:" + this. name + "; age:" + this. age ;}} public class treesetdemo02 {public static void main (string ARGs []) {set <person> allset = new treeset <person> (); allset. add (new person ("Zhang San", 30); allset. add (new person ("Li Si", 31); allset. add (new person ("Wang Wu", 31); allset. add (new person ("Wang Wu", 32); allset. add (new person ("Zhao ", 33); allset. add (new person ("Sun Qi", 34); allset. add (new person ("Zhang San", 35); system. out. println (allset );}}

The following error occurs during execution:

Exception in thread "Main" Java. Lang. classcastexception: person cannot be cast t
O java. Lang. Comparable
At java. util. treemap. Put (unknown source)
At java. util. treeset. Add (unknown source)
At treesetdemo02.main (treesetdemo02.java: 22)

The modification code is as follows:

Import Java. util. set; import Java. util. treeset; class person implements comparable <person> {private string name; private int age; Public Person (string name, int age) {This. name = Name; this. age = age;} Public String tostring () {return "name:" + this. name + "; age:" + this. age + "\ n";} public int compareto (person per) {// This method must be separated from equals, although it is easy to confuse if (this. age> Per. age) {return 1;} else if (this. age <per. age) {return 0;} else {return this. name. compareto (Per. name); // call compareto () method in string }}public class treesetdemo02 {public static void main (string ARGs []) {set <person> allset = new treeset <person> (); allset. add (new person ("Zhang San", 30); allset. add (new person ("Li Si", 31); allset. add (new person ("Wang Wu", 31); allset. add (new person ("Wang Wu", 32); allset. add (new person ("Zhao ", 33); allset. add (new person ("Sun Qi", 34); allset. add (new person ("Zhang San", 35); system. out. println (allset );}}

In this case, the removed duplicate elements are not actually removed.

Import Java. util. set; import Java. util. hashset; class person {private string name; private int age; Public Person (string name, int age) {This. name = Name; this. age = age;} Public String tostring () {return "name:" + this. name + "; age:" + this. age + "\ n" ;}} public class treesetdemo03 {public static void main (string ARGs []) {set <person> allset = new hashset <person> (); allset. add (new person ("Zhang San", 30); allset. add (new person ("Li Si", 31); allset. add (new person ("Wang Wu", 32); allset. add (new person ("Wang Wu", 32); allset. add (new person ("Zhao ", 33); allset. add (new person ("Sun Qi", 34); allset. add (new person ("Zhang San", 35); system. out. println (allset );}}

At this point, duplicate elements are not removed. How can we remove duplicate elements?

If you want to cancel duplicate elements, you need two methods in the object class for help:

Hashcode (); indicates a unique encoding, which is generally expressed by computation.
Equals (); compares objects.

Import Java. util. set; import Java. util. treeset; class person {private string name; private int age; Public Person (string name, int age) {This. name = Name; this. age = age;} Public String tostring () {return "name:" + this. name + "; age:" + this. age;} public Boolean equals (Object OBJ) {// overwrite equals to complete object comparison if (this = OBJ) {return true;} If (! (OBJ instanceof person) {return false;} person P = (person) OBJ; // perform a downward transformation if (this. name. equals (P. name) & this. age = P. age) {return true;} else {return false;} public int hashcode () {return this. name. hashcode () * This. age;} Public String tostring () {return "name:" + this. name + "; age:" + this. age ;}} public class repeatdemo02 {public static void main (string ARGs []) {set <person> allset = new hashset <person> (); allset. add (new person ("Zhang San", 30); allset. add (new person ("Li Si", 31); allset. add (new person ("Wang Wu", 32); allset. add (new person ("Wang Wu", 32); allset. add (new person ("Zhao ", 33); allset. add (new person ("Sun Qi", 34); allset. add (new person ("Zhang San", 35); system. out. println (allset );}}

If you want to use set, you must pay attention to the above two problems.

4. Conclusion:

1. I have previously stressed that a good class should overwrite the equals (), hashcode (), and tostring () methods in the object class. In fact, all the methods in string have been overwritten.
2. The set interface relies on hashcode () and equals () to judge duplicate elements, which will also be reflected in future map interfaces.
3. treeset uses the comparable interface to perform sorting (the compareto () method must be overwritten ).

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.