Java-preliminary Understanding-Common Object API (Collection framework-map collection-Store custom objects)

Source: Internet
Author: User

One.

Next you want to do a sort of student, should make a comparison method. By observing the other part of the program, students have a comparative approach and are ranked by age.

Now the key to sort, (I think who called the key, who called the value is irrelevant, but the position of the decision), the front of the HashMap to change to TreeMap,

Just traverse the keyset, and now you are ready to use EntrySet.

The results were sorted, ordered by age.

If you want to sort by name, you need to get a comparator. There is,

The student is the one that compares the person. This comparator, the set of person, can be used, and the collection of Student,worker can also be used.

It is certain that you can receive both this type and the parent type. The student is saved, and can be received by the student in the parent class.

Do not look at the API of the TreeMap collection, but also know that there is a comparator inside it.

Just a little change, sort by alphabetical order. After learning TreeSet, it is very simple to learn treemap.

Java-preliminary Understanding-Common Object API (Collection framework-map collection-Store custom objects)

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.