Comparator.comparing(....)を使用して、フィールドでのみ比較するためにJava 8でコンパレータを実装します。カスタムコンパレータはJavaのフィールドのみで作成します。
私が実現したい機能は以下の通りです:
List<DocumentLink> documentList = documentLinkService.getDocumentList(baseInstance);
//call of custom comparator for DigitalFileCategory due to compare only by Name
documentList = documentList.stream()
.filter(doc -> category.comp(doc.getDigitalFileCategory()))
.collect(Collectors.toList());
だから私は、ブール値を返さなければなりません。 DigitalFileCategoryで コンプ方法:
public boolean comp(Object obj) {
return super.equals(obj) ||
(obj != null &&
getName() != null &&
getName().equals(((DigitalFileCategory) obj).getName()));
}
すべてのアイデア、私はそれをどのように行うことができますか? 私はComparator.comparingを実装しようとするとgetNameを静的にするように求めました。
DigitalFileCategory.class
public class DigitalFileCategory extends _Base {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "iddigitalfilecategory", nullable = false)
private Integer iddigitalfilecategory;
@Column(name = "Name", length = 64)
private String name;
@Column(name = "Priority")
private Integer priority;
// getter && setter
}
'DocumentLink'と' DigitalFileFactory'クラスの基本情報を提供してください。 –
なぜコンパレータが必要ですか? – Oleg
'Comparator'は注文のためのものであり、等価ではありません。 – shmosel