Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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() 
like image 973
Gregoire Gaspar Avatar asked Mar 08 '26 13:03

Gregoire Gaspar


1 Answers

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
like image 164
Dylan Meeus Avatar answered Mar 11 '26 01:03

Dylan Meeus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!