Gregoire Gaspar Gregoire Gaspar - 3 months ago 14
Java Question

Downgrading function of Java 8 to Java 7

I have started to learn Java with current version 1.8. This means I use function that appears in this version and I don't know how things were going on before...

I would like someone tell me how to do the equivalent of this in java 7 please :

I have a Person object which contains a float value attribute. I want to find the min/max value of this value out of a list of "Person". This is how I did in Java 8 with parallelStream :

Person max = people.parallelStream().max(Comparator.comparing(p-> ((Person) p).getFloatValue())).get()

Answer

To get the person where that value is maximized, you could write a loop over the list, and then check each value.

 Person maxPerson = people.get(0); // get's the first person
 for(int i = 1; i < people.size(); i ++) // loop over list, finding max
 {
    if(people.get(i).getFloatValue() > maxPerson.getFloatValue())
       maxPerson = people.get(i); 
 }
 // now maxPerson stores the person where this value is the highest
Comments