Collection sort: Use difference of comparator and comparable

Source: Internet
Author: User

  • Comparator interface
  • Comparable interface
  • Difference

It is common to use collections in Java to store data, and the set sort feature is also one of the most common features. Here's how to sort the collections, the common methods are: Comparator andComparable

Comparator interface

Steps to use:

    • Create a new comparison class,
    • Implement the Comparator interface,
    • Override the Compare method,
package sort;import java.util.Comparator;publicclassimplements Comparator<LuckBoy>{    @Override    publicintcompare(LuckBoy o1, LuckBoy o2) {        return o1.getAge()-o2.getAge();    }}
    • Call Collections.sort() methods to sort,
    • Form: Collections.sort(集合, 比较器实例) .
@Test Public void test1() {list<luckboy> boylist =NewArraylist<luckboy> (); Luckboy boy1 =New Luckboy("Zhang San", -,"Shanghai"); Luckboy Boy2 =New Luckboy("John Doe", A,"Beijing"); Luckboy Boy3 =New Luckboy("Harry", -,"Shenzhen"); Luckboy Boy4 =New Luckboy("Caifan", -,"Nanjing"); Boylist.Add(boy1); Boylist.Add(BOY2); Boylist.Add(BOY3); Boylist.Add(BOY4); System. out.println("before sorting:"); for(Luckboy luckboy:boylist) {System. out.println(Luckboy); } System. out.println("After sorting:"); Collections.Sort(Boylist,New Luckboycompare()); for(Luckboy luckboy:boylist) {System. out.println(Luckboy); }}

Luckboy.java

Package sort; Public classluckboy{PrivateString name;PrivateInteger age;PrivateString City; Public Luckboy() {Super(); } Public Luckboy(String name, Integer age, String city) {Super(); This.name= name; This. Age= age; This. City= City; } PublicStringGetName() {returnName } Public void SetName(String name) { This.name= name; } PublicIntegerGetage() {returnAge } Public void Setage(Integer Age) { This. Age= age; } PublicStringgetcity() {returnCity } Public void setcity(String city) { This. City= City; }@Override     PublicStringtoString() {return "Luckboy [name="+ name +", age="+ Age +", city="+ City +"]"; }}

Printing results:

排序前:LuckBoy [name=张三, age=13, city=上海]LuckBoy [name=李四, age=12, city=北京]LuckBoy [name=王五, age=18, city=深圳]LuckBoy [name=马六, age=17, city=南京]排序后:LuckBoy [name=李四, age=12, city=北京]LuckBoy [name=张三, age=13, city=上海]LuckBoy [name=马六, age=17, city=南京]LuckBoy [name=王五, age=18, city=深圳]
Comparable interface

Steps to use:

    • The data model implements the comparable interface,
    • Override the CompareTo method,
package sort;publicclassimplements Comparable<LuckBoy>{//TODO 中间代码省略    @Override    publicintcompareTo(LuckBoy o) {        returnthis.age-o.age;    }}
    • Call Collections.sort() methods to sort,
    • Form:Collections.sort(集合)
@Test Public void test2() {list<luckboy> boylist =NewArraylist<luckboy> (); Luckboy boy1 =New Luckboy("Zhang San", -,"Shanghai"); Luckboy Boy2 =New Luckboy("John Doe", A,"Beijing"); Luckboy Boy3 =New Luckboy("Harry", -,"Shenzhen"); Luckboy Boy4 =New Luckboy("Caifan", -,"Nanjing"); Boylist.Add(boy1); Boylist.Add(BOY2); Boylist.Add(BOY3); Boylist.Add(BOY4); System. out.println("============================"); System. out.println("before sorting:"); for(Luckboy luckboy:boylist) {System. out.println(Luckboy); } System. out.println("After sorting:"); Collections.Sort(boylist); for(Luckboy luckboy:boylist) {System. out.println(Luckboy); }}

Printing results:

排序前:LuckBoy [name=张三, age=13, city=上海]LuckBoy [name=李四, age=12, city=北京]LuckBoy [name=王五, age=18, city=深圳]LuckBoy [name=马六, age=17, city=南京]排序后:LuckBoy [name=李四, age=12, city=北京]LuckBoy [name=张三, age=13, city=上海]LuckBoy [name=马六, age=17, city=南京]LuckBoy [name=王五, age=18, city=深圳]
Difference

Comparator Use flexibility, do not need to modify the source code. However, the comparator object needs to be passed in when used;
comparable simple to use, but need to modify the source code.

Collection sort: Use difference of comparator and comparable

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.