当前位置: 首页 > 知识库问答 >
问题:

使用流[重复]获取java8列表中的最大值

司寇旺
2023-03-14

我想用Java8lambda表达式在java列表中找到max值,所以,我有一个类叫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

}
@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...
List<TicketMasterLog> tl = //some objects;

因此,在列表中,我希望找到最大状态id值

谢谢你。

共有1个答案

许兴文
2023-03-14

您可以使用:

Stream<TicketMasterLog> ticketMasterLogStream = tl.stream();
TicketMasterLog max = ticketMasterLogStream.reduce((a,b)-> 
    a.getStatusId.compareTo(b.getStatusId) > 0 ? a:b;
).get()
 类似资料: