1

I want to find max value in java list using java8 lambda expression, so, I have a Class called TicketMaster and item table is TicketMasterLog ,in TicketMasterLog class I have StatusMaster Class statusId reference column, so here i want to find max statusId in TicketMasterLog List, below i'll give my code please refer

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

and TicketMasterItem Table is

@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

}

and StatusMaster Table is

@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...

Now I have a TicketMasterlog List

List<TicketMasterLog> tl = //some objects;

so in that list I want find max status id value

thanking you.

Stefan Zobel
  • 3,182
  • 7
  • 28
  • 38
Guruprasad N
  • 85
  • 3
  • 10
  • Why not use order by and take first or max directly from DB? – Viktor Mellgren Mar 24 '17 at 08:07
  • hi Viktor, thanks for your reply.Its posible for getting max value from database but I'll get this max value in a loop , so in that loop may be more than 100 some times, so that,every time we need go for database query 100 times. so , avoid interaction to database every time , I'll try for lambda expression. – Guruprasad N Mar 24 '17 at 09:02

1 Answers1

2

You can use:

Stream<TicketMasterLog> ticketMasterLogStream = tl.stream();
TicketMasterLog max = ticketMasterLogStream.reduce((a,b)-> 
    a.getStatusId.compareTo(b.getStatusId) > 0 ? a:b;
).get()
frhack
  • 4,862
  • 2
  • 28
  • 25
  • HI, its worikin fine thanks for your answer – Guruprasad N Mar 24 '17 at 09:02
  • 5
    Any reason not to use `Stream.max` for getting the maximum? `tl.stream().max(Comparator.comparing(TicketMasterLog::getStatusId)).get()`. Of course, you can do the same even without streams: `Collections.max(tl, Comparator.comparing(TicketMasterLog::getStatusId))` – Holger Mar 24 '17 at 12:56
  • No, but using reduce is a more general functional programming solution. So if you learn how it works you can reuse more than "max". Do more with less. Reduce is very important in functional programming. – frhack Mar 24 '17 at 13:08
  • @Holger moreover the question ask to use a lambda expression. – frhack Mar 24 '17 at 13:41
  • 1
    Whether you write `TicketMasterLog::getStatusId` or `x -> x.getStatusId()`, is irrelevant. If you insist on using a lambda expression, use the latter. I still don’t see how you do “more with less”. You are *writing* more with `(a,b) -> a.getStatusId().compareTo(b.getStatusId()) > 0 ? a:b`, but achieving nothing more. – Holger Mar 24 '17 at 13:44
  • I mean that if you learn how to use "reduce" you'll be able to do more things than if you know stream.max. – frhack Mar 24 '17 at 13:46
  • 3
    Sure. And if you know how to create a `Comparator` without verbosity nor code duplication, you can also do more things with it than querying the max value. And using a straight-forward solution designed for the actual task, is something worth learning too. – Holger Mar 24 '17 at 13:51