I get a List of Objects List<APIObjects> apiObjectList as an Input to my API (through HTTP-Post), I need to compare this Input-List with my List of Entity Object which i get by executing repository.findAll() with Spring-boot-data-JPA framework
Currently i loop the List<DatabaseObject> and then find if there is a match. Below is my code
public Boolean findIfAllAPIobjectsExist (List<APIObject> apiObjects) {
List<DatabaseObject> databaseObjectsList = databaseRepository.findAll()
return apiObjects.stream().allMatch {
apiObject -> {
for (DatabaseObject dbObject : databaseObjectsList) {
if ((dbObject.getGroupId().trim().equals(dbObject.getGroupId().trim())) &&
(dbObject.getArtifactId().trim() .equals(dbObject.getArtifactId().trim())) &&
(dbObject.getVersion().trim().equals(dbObject.getVersion().trim()))) {
System.out.println("Matching ..");
return true;
}
}
return false;
}
}
}
}
But this looping seems to consume lot of time and Memory and How can it be tackled with Lambda functions ? I am pretty sure that my current methodology shown above (looping of DatabaseObject) isn't the right or professional way to tackle it
APIObject.java
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class APIObject{
private String groupId;
private String artifactId;
private String version;
}
DatabaseObject.java
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "my_table")
public class DatabaseObject {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
@Column(name = "name")
String name;
@Column(name = "group_id")
String groupId;
@Column(name = "artifact_id")
String artifactId;
@Column(name = "version")
String version;
}
hibernateorspring-data-jpaway of tackling this ? Probably with aninquery