获取Object的ArrayList的最小/最大浮点值

时间:2016-08-01 22:34:10

标签: java arraylist

我有一个PersonList,它包含一个float属性。我想做的是获取我的Arraylist的最小值和最大值来显示它们。

public class Person implements Serializable {

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

我尝试使用Collections.min(mylist)Collections.max(mylist),但似乎我必须覆盖比较器。

然后我的第二个问题是我想找到mylist的每个元素,这些元素在date属性中具有相同月份(同年),但我真的不知道如何做到这一点。

希望有人可以帮助我!

3 个答案:

答案 0 :(得分:3)

假设你有一个人的数组列表......

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

这是你获得最大值和最小值的人

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

然后,您可以使用Map<People>Calendar实例按月对人员进行分组,如下所示:

    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); 
    }

总而言之,这是一个可以测试的完整工作示例:

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;
        }
    }

}

答案 1 :(得分:0)

提示(因为这是一个家庭作业问题):

首先要有你的类实现:

implements Comparator<Person>

然后执行以下操作:

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);    
  }

答案 2 :(得分:0)

实施Comparable接口

implements Serializable, Comparable<Person>

并覆盖compareTo方法

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