SortedSet custom sorting and sortedset custom sorting

Source: Internet
Author: User

SortedSet custom sorting and sortedset custom sorting

We know that a Set can store a series of objects, such as int and class, which are unordered and repeatable. Today we will discuss:Can the Set be sorted? How can I customize the sorting rules?

First, steal a graph to describe the inheritance relationship of the Set:

We will discuss the usage of SortedSet today.
(Note: it is an excuse to use their implementation classes)

Next we will sort SortedSet:

public class test {    public static void main(String[] args) {        TreeSet<String> set=new TreeSet<String>();        set.add("B");        set.add("D");        set.add("A");        set.add("E");        set.add("C");        System.out.println(set);    }}

What is the result? [A, B, C, D, E]...
It is arranged in alphabetical order, so if I want to arrange it in reverse order, I need to customize a sorting rule here.

// A construction method of the TreeSet // comparator: This is the custom sorting Rule public TreeSet (Comparator <? Super E> comparator) {this (new TreeMap <> (comparator ));}

Let's test it;

Public class test {public static void main (String [] args) {// a new custom Comparator TreeSet <String> set = new TreeSet <String> (new MyComparator ()); set. add ("B"); set. add ("D"); set. add ("A"); set. add ("E"); set. add ("C"); System. out. println (set) ;}} class MyComparator implements Comparator {@ Override public int compare (Object o1, Object o2) {String s1 = (String) o1; String s2 = (String) o2; // reverse return s2.compareTo (s1 );}}

Result and clear: [E, D, C, B, A]

This is very simple. If we use a custom class, such as a Person class, we need to sort by age. Let's take a look at how to implement it:

Public class test {public static void main (String [] args) {// custom sorting rule TreeSet <Person> set = new TreeSet <Person> (new MyComparator ()); set. add (new Person ("A", 20); set. add (new Person ("D", 10); set. add (new Person ("E", 40); set. add (new Person ("C", 50); set. add (new Person ("B", 30); System. out. println (set) ;}} class Person {String name; int age; public Person (String name, int age) {this. name = name; this. age = age ;}@ Override // rewrite the toString method to specify the output format: public String toString () {return "name:" + name + ", age = "+ age ;}} class MyComparator implements Comparator {@ Override public int compare (Object o1, Object o2) {Person p1 = (Person) o1; Person p2 = (Person) o2; // custom comparison rule return (int) (p1.age-p2.age );}}

Result:
[Name: D, age = 10, name: A, age = 20, name: B, age = 30, name: E, age = 40, name: C, age = 50]

Achieve the desired result.

In this way, we can sort 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.