Helvin
Helvin

Reputation: 53

Getting min/max float value of an ArrayList of Object

I have a ArrayList of Person which contains a float attribute. What I would like to do is to get the min value and the max value of my Arraylist to display them.

public class Person implements Serializable {

    private float myvalue;
    private Date date;
    //getters setters...

I tried to use Collections.min(mylist) and Collections.max(mylist) but it seems I have to override the comparator.

Then my second problem is that I would like to find each element of mylist which have the same month (of the same year) in the date attribute but I don't really see how i can do this.

Hope someone could help me !

Upvotes: 1

Views: 2447

Answers (3)

Austin
Austin

Reputation: 8585

Assuming you have an array list of people...

    Collection<Person> people = new ArrayList<>();

This is how you get the max and min value people

    Person maxValuePerson = people.parallelStream()
            .max(Comparator.comparing(p -> ((Person) p).getMyValue()))
            .get();
    Person minValuePerson = people.parallelStream()
            .min(Comparator.comparing(p -> ((Person) p).getMyValue()))
            .get();

You can then group the people by Month by using a Map<People> and a Calendar instance as follows:

    HashMap<Integer,ArrayList<Person>> monthMap = new HashMap<>();

    Calendar cal = Calendar.getInstance(); //expensive operation... use sparingly

    for (Person p : people){
        cal.setTime(p.getDate()); //Sets this Calendar's time with the person's Date.
        int month = cal.get(Calendar.MONTH); //gets int representing the month
        ArrayList<Person> monthList = monthMap.get(month); 

        //initialize list if it's null (not already initialized)
        if(monthList == null) {
            monthList = new ArrayList<>(); 
        }

        monthList.add(p); //add the person to the list

        // put this month's people list into the map only if it wasn't there to begin with
        monthMap.putIfAbsent(month, monthList); 
    }

Putting it all together, here's a full working example that you can test:

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Random;

public class MinMaxTest {

    public static void main(String[] args) {

        Random rand = new Random();


        //Assuming an array list of people...
        Collection<Person> people = new ArrayList<>();


        for (int i = 0; i < 50; i++){
            Person p = new Person();
            p.setMyvalue(rand.nextFloat());
            p.setDate(new Date(rand.nextLong()));
            people.add(p);
        }

        //This is how you get the max and min value people
        Person maxValuePerson = people.parallelStream()
                .max(Comparator.comparing(p -> ((Person) p).getMyValue()))
                .get();
        Person minValuePerson = people.parallelStream()
                .min(Comparator.comparing(p -> ((Person) p).getMyValue()))
                .get();

        //to group the people by month do the following:
        HashMap<Integer,ArrayList<Person>> monthMap = new HashMap<>();

        Calendar cal = Calendar.getInstance();

        for (Person p : people){
            cal.setTime(p.getDate());
            int month = cal.get(Calendar.MONTH);
            ArrayList<Person> monthList = monthMap.get(month);
            if(monthList == null)
                monthList = new ArrayList<>();
            monthList.add(p);
            monthMap.putIfAbsent(month, monthList);
        }

        for(Integer i : monthMap.keySet()){
            System.out.println("Month: "+ i);
            for(Person p : monthMap.get(i)){
                System.out.println(p);
            }
        }

    }

    static class Person implements Serializable {
        private float myvalue;
        private Date date;

        public Date getDate() {
            return date;
        }
        public void setDate(Date date) {
            this.date = date;
        }
        public float getMyValue() {
            return myvalue;
        }
        public void setMyvalue(float myvalue) {
            this.myvalue = myvalue;
        }
    }

}

Upvotes: 3

atr
atr

Reputation: 714

Implement the Comparable interface

implements Serializable, Comparable<Person>

and override the compareTo method

@Override
public int compareTo(Person o) {
    int value = (int) o.getAge();
    return (int) (this.age - value);
}

Upvotes: 0

Pritam Banerjee
Pritam Banerjee

Reputation: 18958

Hint (as this is a Homework problem I guess):

First have your class implement:

implements Comparator<Person>

Then do the following:

public int compareTo(Object anotherPerson) throws ClassCastException {
    if (!(anotherPerson instanceof Person))
      throw new ClassCastException("A Person object expected.");
    int yourValue = ((Person) anotherPerson).getYourValue();  
    return (this.yourValue - yourValue);    
  }

Upvotes: 0

Related Questions