Reuse of java Comparator

Source: Internet
Author: User

Reuse of java Comparator

 

Imagine A scenario where A has N fields and A comparison function dedicated to. Each comparison function is reused in N multiple business lines.

Then, the problem arises. One day A adds an in field, and the priority of this field in the original scenario is higher than that of all other fields. It has no effect in other scenarios.

How can this problem be solved?

 

1. rewrite all existing Comparator classes and their compare methods. This method is too costly, because if... else is required in N many places.

2. Only the comparator method before the combination is added to a specific scenario.

If there is a data structure such as Collections. sort (aList, Lists. newArraryList (c1, c2, c3), the problem will be solved (in fact, it is very easy to write one by yourself ).

However, I did not find a class that can completely replace apache. commons. collections. ComparatorChain stores all sorters using a linked list.

 

C1 and C2 can all be used as sorters in the past.

 

Static class A {Integer n1; Integer n2; A (Integer n1, Integer n2) {this. n1 = n1; this. n2 = n2 ;}@ Override public String toString () {return A {+ n1 = + n1 +, n2 = + n2 + '}';}} public static void main (String [] args) {List
 
  
Data = Lists. newArrayList (3, 1, 3, 1, 3, 2, 4, 2, 2, 2, 4, 4, 2, 3, 1, 3, 4, 1, 2); List aList = Lists. newArrayList (); for (Integer I = 0; I <data. size (); I + = 2) {aList. add (new A (data. get (I), data. get (I + 1);} ComparatorChain multiSort = new ComparatorChain (); multiSort. addComparator (new Comparator () {@ Override public int compare (A o1, A o2) {return Integer. compare (o1.n1, o2.n1) ;}@ Override public boolean equals (Object obj) {return false ;}}); multiSort. addComparator (new Comparator () {@ Override public int compare (A o1, A o2) {return Integer. compare (o1.n2, o2.n2) ;}@ Override public boolean equals (Object obj) {return false ;}}); System. out. println (Before sorting: + Joiner. on (,). join (aList); Collections. sort (aList, multiSort); System. out. println (after sorting: + Joiner. on (,). join (aList ));}
 


 

Test results:

Before sorting: A {n1 = 3, n2 = 1}, A {n1 = 3, n2 = 1}, A {n1 = 3, n2 = 2 }, A {n1 = 4, n2 = 2}, A {n1 = 2, n2 = 2}, A {n1 = 2, n2 = 4}, A {n1 = 4, n2 = 2}, A {n1 = 3, n2 = 1}, A {n1 = 3, n2 = 4}, A {n1 = 1, n2 = 2}
After sorting: A {n1 = 1, n2 = 2}, A {n1 = 2, n2 = 2}, A {n1 = 2, n2 = 4 }, A {n1 = 3, n2 = 1}, A {n1 = 3, n2 = 1}, A {n1 = 3, n2 = 1}, A {n1 = 3, n2 = 2}, A {n1 = 3, n2 = 4}, A {n1 = 4, n2 = 2}, A {n1 = 4, n2 = 2}

 

 

 

 

 

 

 

 

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.