这是具有以下结构的 map :
Map<String, Oddcurve> curves;
class Oddcurve entends Curve {
private String curvename;
}
class Curve {
private int curveId;
protected Set<CurvePoint> points = new TreeSet();
}
class CurvePoint {
private double time;
private double value;
}
我想按 ASC 时间对曲线进行排序,最后返回相同的 map :
proxyCurves.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getValue));
我是这样写的,当我调试它时,我可以看到排序工作正常,但最终仍然返回原始曲线,我不知道为什么?
proxyCurves.values().stream().forEach(curve -> curve.setPoints(curve.getPoints().stream().sorted(new ComparableComparator<>()).collect(Collectors.toSet())));
最佳答案
- 首先,我建议您重写
hashCode()
和equals()
方法(您可能会因此遇到问题)。 - 其次,我建议您实现
Comparable
CurvePoint
类中的接口(interface),因为您使用的是TreeSet
。然后你就可以覆盖compareTo()
方法,并定义排序方式。
关于java - 如何在多层中按值对 Map 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44903370/