获取对象ArrayList的最小/最大浮点值

问题描述:

我有一个包含float属性的Person的ArrayList。我想要做的是获得我的Arraylist的最小值和最大值来显示它们。获取对象ArrayList的最小/最大浮点值

public class Person implements Serializable { 

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

我试图用Collections.min(mylist)Collections.max(mylist)但似乎我不得不重写比较。

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

希望有人能帮助我!

+2

你的对象可能需要实现'Comparable' ...只是一个想法... –

+2

你为什么不去一个简单的“for循环”?我认为这是最直接的开始,特别是当你是初学者时。 – Matt

+0

我不好当我复制/粘贴代码,但它不是一个很长,但很好浮动myvalue – Helvin

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

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

} 
+0

如果列表中只有一个元素,您的最小/最大查找程序是否工作?看来我得到一个NoSuchElementException – Helvin

+0

是的,即使只有一个元素,它也可以工作。 'NoSuchElementException'仅在人员'ArrayList'为空时出现。 –

+0

好的,谢谢我正在尝试这种方法,看看它是否工作! – Helvin

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

先听听你的类实现:

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

我会试试这个谢谢你!我的第二个问题有什么想法? – Helvin

+0

@Helvin在列表中运行for循环,并将每个对象与所有其他对象进行比较并找到它。使用'getMonth()'和'getYear'方法寻找月份和年份 –

+0

我尝试了最小/最大值的解决方案,但出现错误,因为getValue返回float并且compareTo期望为int – Helvin

实现Comparable接口

implements Serializable, Comparable<Person> 

并重写compareTo方法

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