0

I have an object.

public class Test{

int age;
LocalDateTime currentTime;

//class also contains getters and setters. 

}

Lets assume I have a List and i need to calculate the mean age. I iterate though all, add values then divide by the size of the list. Now i need to calculate the difference between the age and the mean. If the difference between the age and mean repeats (is same between different objects in the list) then i need to remove the object with the earliest timestamp from the array.

I am stuck on how to do this. Initially i was thinking that i can just expand the Test class to store the difference then do a sort, but want to avoid this. The other options left were to store it in a Map or create a separate object for them but seems very messy.

Any other ideas on how to do this nicely?

Rory Lester
  • 2,858
  • 11
  • 49
  • 66

2 Answers2

0

Two objects have the same (average-age) if and only if they have the same age. So, you could for example sort the list by (age, [timestamp from latest to earliest]) (this can be done with a comparator) and iterate over the array starting from the second element, deleting each element if it has the same age as the previous one.

[edit : unless you consider the absolute value of the difference between mean and age ? If so, please clarify].

Actually, if you mean the absolute value of the difference, it doesn't make a lot of difference. Create a comparator that sorts your array by (abs(mean-age), [timestamp from latest to earliest]) and the algorithm still applies. If needed, see : How to use Comparator in Java to sort.

Community
  • 1
  • 1
Zoyd
  • 3,449
  • 1
  • 18
  • 27
  • 1
    Although the OP did not explicitely tell this, I assume he meant the absolute difference. If the mean is 20 years, then 18 years and 22 years have the same difference. In this case the 18 year object must be deleted. This is what I understand. So your "iff" seems to be wrong. – Seelenvirtuose Apr 25 '14 at 07:41
0

Here you don't need mean in remove algorithm

then just try use two for loops

for(int i=0; i<list.size();++i){

    Test currentTest = list.get(i);

    List<Test> toRemove = new LinkedList<>();
    for(int j=i+1;j<list.size();++j){
        Test test = list.get(j);
        if(currentTest.age == test.age){
              if(currentTest.currentTime.after(test.currentTime){
                    toRemove.add(currentTest);
                    currentTest = test;
              } else {
                    toRemove.add(test);
              }
        }
    }
    list.removeAll(toRemove);
}

Or you can at first sort list by time (from earlier to older, iterate over list and remove elements if have same age and are after current element). Or use treemap with age key, iterate over list, check if element is in tree, if not add else compare currentTime and replace if needed. Then get all values as set.

Remember that if you need mean you must count it before removing any element from list.

kaczy
  • 21
  • 3
  • This works of course, but in this case, using an O(n²) algorithm would not qualify as doing things "nicely" in my opinion. – Zoyd Apr 25 '14 at 07:49