2016-08-01 45 views
1

我有一個包含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

回答

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

} 
+0

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

+0

是的,即使只有一個元素,它也可以工作。 'NoSuchElementException'僅在人員'ArrayList'爲空時出現。 –

+0

好的,謝謝我正在嘗試這種方法,看看它是否工作! – Helvin

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

我會試試這個謝謝你!我的第二個問題有什麼想法? – Helvin

+0

@Helvin在列表中運行for循環,並將每個對象與所有其他對象進行比較並找到它。使用'getMonth()'和'getYear'方法尋找月份和年份 –

+0

我嘗試了最小/最大值的解決方案,但出現錯誤,因爲getValue返回float並且compareTo期望爲int – Helvin

0

實現Comparable接口

implements Serializable, Comparable<Person> 

並重寫compareTo方法

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