简体   繁体   English

简单比较的Java比较器

[英]java comparator for simple custom logic

How I can find line 4,1 and 6 in example below? 在下面的示例中,如何找到第4,1和6行?
And is the use of Collection.sort() with Comparator reasonable in this case? 在这种情况下,在Comparator中使用Collection.sort()是否合理?

       a -  b - c - d

1.)    6    8   16  18   
2.)    38  40   55  57  
3.)    6    8   25  27  
4.)    1    5   11  15  
5.)    6    8    3   5  
6.)    9   12   19  22   
7.)    18  20    1   3  
8.)    23  25   15  17 

Example on the top is a List with object meets following criteria: 顶部的示例是一个对象满足以下条件的列表:
- every object contains 4 integer(a,b,c,d), -每个对象都包含4个整数(a,b,c,d),
- every object in the list is unique, -列表中的每个对象都是唯一的,
- a < b and c < d. -a <b和c <d。


Below is not working example, but my way of thinking, how I can expect comparator to work for finding expected object. 下面不是工作示例,而是我的思维方式,如何期望比较器工作以找到期望的对象。

public class Row_Filter implements Comparable<Row_Filter>{
    int a,b,c,d;
    public Row_Filter(int a, int b, int c, int d) {
        this.a = a; this.b = b; this.c = c; this.d = d;
    }
   static class FilterAccordingAB implements Comparator<Row_Filter> {
        public int compare(Row_Filter o1, Row_Filter o2) {
            return o2.a - o1.b+1;
        }
    }
   static class FilterAccordingCD implements Comparator<Row_Filter> {
        public int compare(Row_Filter o1, Row_Filter o2) {
            return o2.c - o1.d+1;
        }
    }
   static class FilterAccordingABCD implements Comparator<Row_Filter> {
        public int compare(Row_Filter o1, Row_Filter o2) {
            FilterAccordingAB abF=null;    FilterAccordingCD cdF=null;
            if((abF.compare(o1, o2)==0) && (cdF.compare(o1, o2)==0)){
                return 1;
            }
            return -1;
        }
    }
} 

What you need to do is implement a Comparator interface . 您需要做的是实现一个Comparator 接口 Look up the JavaDocs for that interface. 查找该接口的JavaDocs。 You will need to write a class that implements that interface. 您将需要编写一个实现该接口的类。 This involves writing one method (you don't need to reimplement equals()). 这涉及编写一个方法(您无需重新实现equals())。

The method gets passed two objects. 该方法将传递两个对象。 Look at what value you need to return from the method to show the two objects are 'equal' according to your requirements. 查看需要从该方法返回的值,以根据您的要求显示两个对象“相等”。 Then write the code to return that value when they are 'equal', according to your requirements. 然后根据您的要求编写代码以在“相等”时返回该值。

If any of that is unclear you will need to look up a basic Java textbook on writing methods, writing classes or using interfaces. 如果尚不清楚,则需要查找有关编写方法,编写类或使用接口的基本Java教科书。

It seems you are confused where you would use a comparator. 似乎在使用比较器的地方感到困惑。 DJClayworth describes exactly HOW to create one. DJClayworth准确描述了如何创建一个。 You would use one in, for instance, a sort mechanism: 例如,您可以使用一种排序机制:

Collections.sort(myList, myComparator);

You use this because you can define the comparison algorithm to sort through the collection. 之所以使用它,是因为您可以定义比较算法以对集合进行排序。 Hope this helps clarify somewhat. 希望这有助于澄清。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM