【发布时间】:2022-01-19 05:44:13
【问题描述】:
我想按 ElementWithDistance 类中的距离对对象列表 potentialHS 进行排序,但我无法以这种方式获取 distnace 字段。调用 getElementWithDistance 后,我无法调用 getDistance。如何按包含类中的字段对列表进行排序?
List<PotentialHS> potentialHS = potentialHS.stream().sorted(Comparator.comparingDouble(PotentialHS :: getElementWithDistance)).collect(Collectors.toList());
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Builder
@ToString
public class PotentialHS {
private Integer density;
private Double startDistance;
private Double lastDistance;
private List<PMValue> pmv;
private ElementWithDistance elementWithDistance;
}
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Builder
@ToString
public class ElementWithDistance {
private Long elementId;
private Double distance;
private Type type;
}
【问题讨论】:
标签: java list class sorting comparator