Is there a more terse way to accomplish this?
private boolean fieldLockExists(String tableName, String fieldName, Integer id)
{
List<MpFieldLocks> fieldLocks = getFieldLocks(tableName, id);
boolean exists = false;
for (MpFieldLocks fieldLock: fieldLocks)
{
if (Objects.equals(fieldLock.getFieldName(), fieldName))
{
exists = true;
break;
}
}
return exists;
}
I know I could return from within the for loop to make it shorter. I'm looking for a more fundamental change, if possible.
In C# I might use LINQ. In Java, I'm not sure.
If you're using Java 8, you can use streams:
private boolean fieldLockExists(String tableName, String fieldName, Integer id)
{
return getFieldLocks(tableName, id)
.stream()
.anyMatch(fl -> Objects.equals(fl.getFieldName(), fieldName));
}
See more on this question at Stackoverflow