Set TreeSet (natural sort and custom sort) __treeset

Source: Internet
Author: User

a natural sort of treeset:

Steps:

1. Let the elements themselves have a comparative nature,

2. Implement Compareable interface, cover its CompareTo method

Cases:

class Student implementscomparable//first: Implement Compareable interface

{

Private String name;

private int age;

Replication constructor initialization name and age

Student (String name,int age)

{

this. Name = name;

this. Age = age;

}

public int compareTo (Object obj)//Second: Replication CompareTo method

{

return 0;

if (!   (objinstanceof Student)) Third: Determine if an object is an instance of a particular class

throw New RuntimeException ("Not the Student object");

Student s = (Student) obj;

System.out.println (this. name+ "... compareto ..." +s.name);

Four: The age of the current object is compared to the age of the inserted object, and the current age is greater than the age at which the object was inserted, returning 1,

The right side of the binary tree is inserted at this time, and when equal, returns 0, compares the minor conditions, calls again, and returns-1 when it is less than;

if (this. age>s.age)//Determine whether the current object age is greater than the incoming object age

return 1;

if (this. age==s.age)///If the current age is equal to the age of the incoming object, compare the names to the same

{

return this. Name.compareto (s.name);

}

return-1;

/**/

}

Public String getName ()//Get Name

{

return name;

}

public int getage ()//getting Age

{

return age;

}

}

class Treesetdemo

{

Public static void main (string[] args)

{

TreeSet ts = newTreeSet (); Create a collection of TreeSet

Ts.add (new Student ("lisi02", 22)); Adding elements to the collection

Ts.add (new Student ("lisi007", 20));

Ts.add (new Student ("lisi09", 19));

Ts.add (new Student ("lisi08", 19));

Iterator it = Ts.iterator (); Initializes an iterator that iterates through all the elements in the collection

while (It.hasnext ())

{

Student stu = (Student) it.next ();

System.out.println (Stu.getname () + "..." +stu.getage ());

}

}

}

Second, the custom sort of TreeSet

1, Origin: When the element itself does not have the comparative sex, or have the comparison is not needed.

At this point to let the collection itself have a comparative, in the initialization, there is a comparison of the way.

2. Steps:

1) Implement Comparator interface

2) Replication Compare method

3 provides a comparator object when creating a TreeSet collection object,

Cases:

import java.util.Comparator;

import Java.util.Set;

import Java.util.TreeSet;

class student1{

Private Integer age;

Public Student1 (Integer age) {

Super ();

this. Age = age;

}

Public Integer Getage () {

return age;

}

Public void setage (Integer age) {

this. Age = age;

}

@Override

Public String toString () {

return age + "";

}

}

class Mycomparator implements comparator{//First step: Implement Comparator interface

@Override

public int Compare (object O1, Object O2) {//Step two: Implement a Campare method

Determine if an object is an instance of a particular class

if (O1 instanceof Student1 & O2instanceof Student1) {

Student1 S1 = (Student1) O1;

Student1 s2 = (Student1) O2;

if (S1.getage () > S2.getage ()) {

return-1;

else If(S1.getage () < S2.getage ()) {

return 1;

}

}

return 0;

}

}

Public class DEMO15 {

Public static void main (string[] args) {

set<student1> s= New TreeSet (new mycomparator ());//Step three: When you create a TreeSet collection object, you provide a comparator object,

/**

* To implement a custom sort, you need to provide a comparator object when you create the TreeSet collection object,

* This object is responsible for the sorting logic of the collection elements;

*/

S.add (new Student1 (140));

S.add (new Student1 (15));

S.add (new Student1 (11));

S.add (new Student1 (63));

S.add (new Student1 (96));

System.out.println (s);

}

}

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.