实体类是这样的
public class BtHistory {
/**
* 逻辑节点
*/
private String ln;
}
我现在需要对这个实体类的字段进行排序,以升序方式进行排序,请问可以怎么样处理,可以贴上实例代码嘛
List<BtseHistory> locations = influxDbConnection.getInfluxQuery(BtseHistory.class, sql);
ln节点的值是这种类型的
ln=“celGGIO1.AnIn75.mag.f”,需要将75截取出来,然后排序
List<BtseHistory> locations = influxDbConnection.getInfluxQuery(BtseHistory.class, sql);
Map<String, Object> map = new HashMap<>();
Set<BtseHistory> historySet = new HashSet<>(locations);
for (final BtseHistory btseHistory : historySet) {
map.put("key",btseHistory);
}
}
中间有一段是用Set进行去重
请问如何排序呢,如果可以的话,有什么方案可以进行去重排序一起实现
package com.boot.test.demo;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.*;
import java.util.stream.Collectors;
@Data
@AllArgsConstructor
public class BtHistory {
/**
* 逻辑节点
*/
private String ln;
public static void main(String[] args) {
List<BtHistory> list = new ArrayList<>();
list.add(new BtHistory("celGGIO1.AnIn75.mag.f"));
list.add(new BtHistory("celGGIO1.AnIn80.mag.f"));
list.add(new BtHistory("celGGIO1.AnIn60.mag.f"));
Set<BtHistory> set = new HashSet<>(list);
List<BtHistory> collect = set.stream()
.sorted(Comparator.comparingInt(a -> Integer.parseInt(a.getLn().substring(13, 15))))//这里截取方式自己去定义可能有不是两位数的情况
.collect(Collectors.toList());
System.out.println(collect.toString());
}
}
[BtHistory(ln=celGGIO1.AnIn60.mag.f), BtHistory(ln=celGGIO1.AnIn75.mag.f), BtHistory(ln=celGGIO1.AnIn80.mag.f)]