The TreeSet of the first season of Java store custom objects and guarantee sorting and uniqueness

Source: Internet
Author: User
Tags java treeset

In a simple example, the internal process of TreeSet collection storage is described, and it is only guaranteed that the custom objects stored here are perfect.

Requirement: A:
* Natural Sorting, sorted by age from small to large
B
* member variable values are the same as the same element


Custom student classes are given out:

public class Student implements comparable<student> {private String name;private int age;public Student () {super (); }public Student (String name, int age) {super (); this.name = Name;this.age = age;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;} @Overridepublic int CompareTo (Student s) {//return 0;//returns 0 that is a repeating element except for the first root node. Equal not to respond, because for 0, think all do not respond//return 1;//How to get in how to come out. More than 0 right row, 1 is greater than 0 all right row//return-1;//How to get in, how to pour out. Less than 0 left row, 1 less than 0 all left row//seen from above. S is the first, for the root node//What is returned here, in fact, should be based on our sorting rules//According to the order of age, the main condition int num = this.age-s.age;//Secondary condition//age same time, also have to see whether the name is also the same//if the age and name are the same, is the same element int num2 = (num = = 0? this.name.compareTo (s.name): num);//Two-level comparison compare age return num2;}}
The test class was given out:
public class TreeSetDemo2 {public static void main (string[] args) {//Create collection Object treeset<student> ts = new Treeset<stu Dent> ();//create element Student S1 = new Student ("Linqingxia", 27); Student s2 = new Student ("Zhangguorong", 29); Student s3 = new Student ("Wanglihong", 23); Student S4 = new Student ("Linqingxia", 27); Student S5 = new Student ("Liushishi", 22); Student s6 = new Student ("Wuqilong", 40); Student s7 = new Student ("Fengqingy", 22);//add Element Ts.add (S1); Ts.add (S2); Ts.add (S3); Ts.add (S4); Ts.add (S5); Ts.add (S6); Ts.add (S7);//Traverse for (Student s:ts) {System.out.println (S.getname () + "---" + s.getage ());}}

Printing results:

Fengqingy---22
Liushishi---22
Wanglihong---23
Linqingxia---27
Zhangguorong---29
Wuqilong---40


Storing custom objects is actually more of a comparison rule, essentially unchanged.

Beginning of the first season of Java TreeSet Store custom objects and guarantee sorting and uniqueness

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.