gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:42:16 25 4
gpt4 key购买 nike

我有一个包含浮点属性的 Person ArrayList。我想做的是获取我的 Arraylist 的最小值和最大值以显示它们。

public class Person implements Serializable {

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

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

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

希望有人能帮助我!

最佳答案

假设您有一个人员列表...

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

这就是你如何获得最大和最小值(value)的人

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

}

关于java - 获取对象的 ArrayList 的最小/最大浮点值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38709120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com