-1
I’m starting in Java 8 and would like to turn the SQL command below into a lambda function:
SELECT * FROM Correspondencia c, (SELECT entidadeOrigem, MAX(m1)
similaridadeMaxima FROM Correspondencia WHERE m1 <> 0 GROUP BY
entidadeOrigem) r WHERE c.entidadeOrigem = r.entidadeOrigem AND c.m1 =
r.similaridadeMaxima
I created the Class Correspondência
with getters
and setters
.
public class Correspondencia {
int codigo;
String nome;
String entidadeOrigem;
String EntidadeDestino;
double m1;
double m2;
double m3;
double medMax;
double dem;
public int getCodigo() {
return codigo;
}
public void setCodigo(int codigo) {
this.codigo = codigo;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getEntidadeOrigem() {
return entidadeOrigem;
}
public void setEntidadeOrigem(String entidadeOrigem) {
this.entidadeOrigem = entidadeOrigem;
}
public String getEntidadeDestino() {
return EntidadeDestino;
}
public void setEntidadeDestino(String entidadeDestino) {
EntidadeDestino = entidadeDestino;
}
public double getM1() {
return m1;
}
public void setM1(double m1) {
this.m1 = m1;
}
public double getM2() {
return m2;
}
public void setM2(double m2) {
this.m2 = m2;
}
public double getM3() {
return m3;
}
public void setM3(double m3) {
this.m3 = m3;
}
public double getMedMax() {
return medMax;
}
public void setMedMax(double medMax) {
this.medMax = medMax;
}
public double getDem() {
return dem;
}
public void setDem(double dem) {
this.dem = dem;
}
}
I want to turn the SQL query into a Java 8 stream or mbda expression.
I created a list of Correspondencia
:
List<Correspondencia> cor = new ArrayList<>();
cor.add(new Correspondencia(41,"Paper_Organization","Paper", "Organization",0,0,0.14,0.04,0.23,0.08"));
cor.add(new Correspondencia(11,"email_hasanemail","email", "hasanemail",0,0.19,0.21,0.19,0,0.12));
cor.add(new Correspondencia(31,"Review_Reviewer","Review","Reviewer",0,0.36,0.5,0.41,0,0.25));
cor.add(new Correspondencia(01,"PaperAbstract_Abstract","PaperAbstract","Abstract", 0,0.33,0.45,0.32,0,0.22));
Now I want to filter the course with Lambda or Stream according to the query:
SELECT * FROM Correspondencia c, (SELECT entidadeOrigem, MAX(m1) similaridadeMaxima FROM Correspondencia WHERE m1 <> 0 GROUP BY entidadeOrigem) r WHERE c.entidadeOrigem = r.entidadeOrigem AND c.m1 = r.similaridadeMaxima
However, it would be clear the return of the sql query to
List<Correspondencia> cor = new ArrayList<Correspondencia>();
cor.add(new Correspondencia(41,"Paper_Organization","Paper", "Organization",0,0.14,0.04,0.23,0.08));
cor.add(new Correspondencia(22,"Paper_Organization","Paper", "Organization",0,0.15,0.04,0.23,0.08));
cor.add(new Correspondencia(22,"Paper_Organization","Paper", "Organization",0,0.36,0.04,0.23,0.08));
cor.add(new Correspondencia(11,"email_hasanemail","email", "hasanemail",0.19,0.21,0.19,0.,0.12));
cor.add(new Correspondencia(11,"email_hasanemail","email", "hasanemail",0.25,0.21,0.19,0.,0.12));
cor.add(new Correspondencia(11,"email_hasanemail","email", "hasanemail",0.37,0.21,0.19,0.,0.12));
cor.add(new Correspondencia(31,"Review_Reviewer","Review","Reviewer",0.36,0.5,0.41,0.,0.25));
cor.add(new Correspondencia(31,"Review_Reviewer","Review","Reviewer",0.38,0.5,0.41,0.,0.25));
cor.add(new Correspondencia(31,"Review_Reviewer","Review","Reviewer",0.37,0.5,0.41,0.,0.25));
cor.add(new Correspondencia(32,"email_hasanemail","email", "hasanemail",0.36,0.5,0.41,0.,0.25));
cor.add(new Correspondencia(01,"PaperAbstract_Abstract","PaperAbstract","Abstract", 0.33,0.45,0.32,0.,0.22));
cor.add(new Correspondencia(01,"PaperAbstract_Abstract","PaperAbstract","Abstract", 0.37,0.45,0.32,0.,0.22));
that would return me as a result the higher value of M1 grouped by the entity:
22,"Paper_Organization","Paper", "Organization",0,0.36,0.04,0.23,0.08
11,"email_hasanemail","email", "hasanemail",0.37,0.21,0.19,0.,0.12
31,"Review_Reviewer","Review","Reviewer",0.38,0.5,0.41,0.,0.25
01,"PaperAbstract_Abstract","PaperAbstract","Abstract", 0.37,0.45,0.32,0.,0.22
The problem is that it is only returning the higher value of M1. And what I need is that return of the highest value of each repeated item of M1 by the entityOrigem.
– Roberto Monteiro