我想使用java8 lambda表达式在java列表中找到最大值, 所以,我有一个名为TicketMaster的类,项目表是TicketMasterLog,在TicketMasterLog类中我有StatusMaster类statusId参考列,所以这里我想在TicketMasterLog列表中找到max statusId,下面我将给出我的代码请参考
@Entity
@Table(name="ticket_master")
@NamedQuery(name="TicketMaster.findAll", query="SELECT t FROM TicketMastert")
public class TicketMaster implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="TICKET_ID", unique=true, nullable=false, length=10)
private String ticketId;
//bi-directional many-to-one association to TicketMasterLog
@OneToMany(mappedBy="ticketMaster",cascade=CascadeType.ALL)
private List<TicketMasterLog> ticketMasterLogs;
//getters and stters
}
和TicketMasterItem表是
@Entity
@Table(name="ticket_master_log")
@NamedQuery(name="TicketMasterLog.findAll", query="SELECT t FROM TicketMasterLog t")
public class TicketMasterLog implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private TicketMasterLogPK id;
//bi-directional many-to-one association to StatusMaster
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="STATUS_ID")
private StatusMaster statusMaster;
//bi-directional many-to-one association to TicketMaster
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="TICKET_ID", nullable=false, insertable=false, updatable=false)
private TicketMaster ticketMaster;
//getters and setters
}
和StatusMaster表是
@Entity
@Table(name="status_master")
@NamedQuery(name="StatusMaster.findAll", query="SELECT s FROM StatusMaster s")
public class StatusMaster implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="STATUS_ID", unique=true, nullable=false, length=10)
private String statusId;
@Column(name="STATUS_NAME", length=45)
private String statusName;
//bi-directional many-to-one association to TicketMasterLog
@OneToMany(mappedBy="statusMaster")
private List<TicketMasterLog> ticketMasterLogs;
//getters and setters...
现在我有一个TicketMasterlog列表
List<TicketMasterLog> tl = //some objects;
所以在该列表中我想找到最大状态id值
感谢你。
答案 0 :(得分:2)
您可以使用:
Stream<TicketMasterLog> ticketMasterLogStream = tl.stream();
TicketMasterLog max = ticketMasterLogStream.reduce((a,b)->
a.getStatusId.compareTo(b.getStatusId) > 0 ? a:b;
).get()