java 8 比较 list中对象的相关参数的最大最小值

今天做总结的时候发现java 8 比较list中对象的相关参数最大值时方法有五种方法之多,做个总结记录一下:
话不多说上代码。

例子:
实体类:
public class Transaction {
private Trader trader;
private int year;
private int value;

public Transaction(Trader trader, int year, int value) {
	this.trader = trader;
	this.year =year;
	this.value =value;
}
public Trader getTrader() {
	return trader;
}
public void setTrader(Trader trader) {
	this.trader = trader;
}
public int getYear() {
	return year;
}
public void setYear(int year) {
	this.year = year;
}
public int getValue() {
	return value;
}
public void setValue(int value) {
	this.value = value;
}

}

最大值查询

方法1:返回最大值

OptionalInt highestValue = transactions.stream()
					.mapToInt(Transaction::getValue)
					.max();

方法2:返回拥有最大值的对象

Optional<Transaction> highestValue = transactions.stream()
					.collect(Collectors.maxBy(transactionComparator));

方法3:返回最大值

Optional<Integer> lowestValue = transactions.stream()
					.map(Transaction :: getValue)
					.reduce(Integer::min);

方法4:返回拥有最大值的对象

Optional<Transaction> highestValue1 = transactions.stream()
					.collect(Collectors.reducing(
							(x,y) -> x.getValue() > y.getValue() ? x:y));

方法5:返回拥有最大值的对象

Comparator<Transaction> transactionComparator = Comparator.comparingInt(Transaction::getValue);			
Optional<Transaction> highest =transactions.stream()
					.collect(Collectors.maxBy(transactionComparator));
			

最小值方法类推。

方便以后使用

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Java 的 `Collections.max()` 和 `Collections.min()` 方法结合自定义的比较器 `Comparator` 来实现获取列表时间字段最大的值和最小的值。以下是示例代码: ```java import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Collections; import java.util.Comparator; import java.util.List; public class TimeUtil { public static LocalDateTime getMaxTime(List<String> timeList) { Comparator<LocalDateTime> comparator = Comparator.naturalOrder(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); List<LocalDateTime> localDateTimeList = timeList.stream() .map(timeStr -> LocalDateTime.parse(timeStr, formatter)) .toList(); return Collections.max(localDateTimeList, comparator); } public static LocalDateTime getMinTime(List<String> timeList) { Comparator<LocalDateTime> comparator = Comparator.naturalOrder(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); List<LocalDateTime> localDateTimeList = timeList.stream() .map(timeStr -> LocalDateTime.parse(timeStr, formatter)) .toList(); return Collections.min(localDateTimeList, comparator); } } ``` 其,`getMaxTime()` 方法和 `getMinTime()` 方法都接收一个 `List` 类型的时间字符串列表作为参数返回该列表时间最大值和最小值对应的 `LocalDateTime` 对象。该方法使用了 Java 8 的 `Stream` API 将时间字符串转化为 `LocalDateTime` 对象,并使用 `Collections.max()` 和 `Collections.min()` 方法结合 `Comparator` 来获取时间最大值和最小值。在比较,使用了 `Comparator.naturalOrder()` 方法来获取自然顺序的比较器,也就是按照时间从小到大或从大到小的顺序进行比较。时间格式的转换使用了 Java 8 的日期时间类 `LocalDateTime` 和 `DateTimeFormatter` 进行处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值