0

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;
        }
    }
} 
14
  • 6
    Would this happen to be homework? Commented Sep 15, 2010 at 19:07
  • 1
    What's special about lines 1,4 and 6? Commented Sep 15, 2010 at 19:09
  • what object types are you dealing with? ... so many questions means your question is a bad one! Commented Sep 15, 2010 at 19:12
  • Special, this are objects that i have to merge in my list to one single line/object -->(1 - 12 - 11 - 22). Commented Sep 15, 2010 at 19:14
  • 3
    Comparator classes don't find things, they compare things. Commented Sep 15, 2010 at 19:28

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

Thank you Aperkins. I am not really confused where to use comparator (in case of List). But, how to describe matching criteria to comparator in Comparator-Language.
Really confused I become, if thinking about how to reduce list size by merging objects. By Folowing this way, I have to create 2nd list, iterating through 1st list coping values of matched object and calling by every match Collection.sort() method. For larger list this seems not to be a perfomant solution. If somebody is intersted below is a link for initial description of entire problem. Link:stackoverflow.com/questions/3712669/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.