用于简单自定义逻辑的java比较器

发布于 2024-09-19 11:09:25 字数 1447 浏览 2 评论 0原文

如何在下面的示例中找到第 4,1 和 6 行?
在这种情况下,将 Collection.sort() 与 Comparator 一起使用是否合理?

       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 

顶部的示例是一个列表,其中的对象满足以下条件:
- 每个对象包含 4 个整数(a,b,c,d),
- 列表中的每个对象都是唯一的,
-a < b和c< d.


下面不是工作示例,而是我的思维方式,我如何期望比较器能够找到预期的对象。

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;
        }
    }
} 

How I can find line 4,1 and 6 in example below?
And is the use of Collection.sort() with Comparator reasonable in this case?

       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),
- every object in the list is unique,
- a < b and 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;
        }
    }
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半城柳色半声笛 2024-09-26 11:09:25

您需要做的是实现一个比较器接口。查找该接口的 JavaDocs。您需要编写一个实现该接口的类。这涉及编写一种方法(您不需要重新实现 equals())。

该方法传递两个对象。根据您的要求,查看您需要从该方法返回什么值来显示两个对象“相等”。然后根据您的要求编写代码以在它们“相等”时返回该值。

如果其中任何一个不清楚,您将需要查找有关编写方法、编写类或使用接口的基本 Java 教科书。

What you need to do is implement a Comparator interface. Look up the JavaDocs for that interface. You will need to write a class that implements that interface. This involves writing one method (you don't need to reimplement 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.

謌踐踏愛綪 2024-09-26 11:09:25

您似乎对在哪里使用比较器感到困惑。 DJClayworth 准确地描述了如何创建一个。例如,您可以在排序机制中使用它:

Collections.sort(myList, myComparator);

您使用它是因为您可以定义比较算法来对集合进行排序。希望这有助于澄清一些。

It seems you are confused where you would use a comparator. DJClayworth describes exactly HOW to create one. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文