Java stream distinct by property
Java 8 stream has a distinct() method which could be used to filter out a list of distinct objects, but the distinctness of that method is based on Object.equals(Object). What if you want to filter a list of objects based on any property (field) of the object? The StreamEx library comes as an elegant solution.
import lombok.Data;
import lombok.EqualsAndHashCode;
import javax.util.streamex.StreamEx;
@Data
@EqualsAndHashCode(of={"id"})
public class SomeObject {
private Long id; // for hashCode and equals
private Long externalId; // some id from external system, could be duplicated
//...
private String otherFields;
}
//...
public List<SomeObject> getDistinctSomeObjects(List<SomeObject> someObjects) {
return StreamEx.of(someObjects).distinct(SomeObject::getExternalId).toList();
}
References