[英]Java Stream Sort - Comparator with 2 or more Criteria for Objects
Issue: I am trying to check if I can sort the list (ArrayList of data type Object), using 2 or more criteria's. 问题:我试图检查是否可以使用2个或更多条件对列表(数据类型为Object的ArrayList)进行排序。 Please note I am aware of using Comparator with thenComparing feature. 请注意,我知道将Comparator与thenComparing功能一起使用。 I wish to check if there is a way to sort with 2 or more criteria w/o having to use a custom data type where I can easily use Comparator feature. 我希望检查是否有一种方法可以对2个或更多条件进行排序,而不必使用自定义数据类型,从而可以轻松使用比较器功能。 For 1 criteria sorting, the below code works. 对于1个条件排序,以下代码适用。
In this case if I do the below, the IntelliJ IDE immediately gives a error saying - 'Cannot resolve method 'get(int)' for o.get(3) 在这种情况下,如果我执行以下操作,则IntelliJ IDE会立即显示错误消息-'无法解析o.get(3)的方法'get(int)'
.sorted(Comparator.comparing(o -> o.get(3).toString()).thenComparing(...)
I have also referred to many threads in this forum sample - Link1 Link2 我在此论坛示例中也提到了许多主题-Link1 Link2
Code (This works well for single criteria) 代码 (这对于单个条件非常有效)
List<List<Object>> listOutput = new ArrayList<>();
.........
.........
listOutput = listOutput
.stream()
.sorted(Comparator.comparing(o -> o.get(3).toString()))
.collect(Collectors.toList());
Added (Details of Object) 已添加 (对象详细信息)
(Note) (注意)
String dataType - exchange, broker,segment, coCode 字符串数据类型-交易所,经纪人,细分,共同代码
LocalDate dataType - tradeDate LocalDate数据类型-tradeDate
LocalTime dataType - tradeTime LocalTime数据类型-tradeTime
double dataType - sellPrice, buyPrice double dataType-卖方价格,买方价格
List<Object> lineOutput = new ArrayList<>();
lineOutput.add(exchange);
lineOutput.add(broker);
lineOutput.add(segment);
lineOutput.add(tradeDate); // Entry Date
lineOutput.add(tradeTime); // Entry Time
lineOutput.add(coCode);
lineOutput.add(sellPrice - buyPrice); // Profit / Loss
listOutput.add(lineOutput); // Add line to Output
It seems that the compiler can not infer the correct types (I've tried javac 8 and 9 with the same effect). 看来编译器无法推断出正确的类型(我试过javac 8和9具有相同的效果)。 The only way I could make it work is specifying the types directly via casting: 我可以使之起作用的唯一方法是直接通过强制转换指定类型:
list.stream()
.sorted(
Comparator.comparing((List<Object> o) -> o.get(3).toString())
.thenComparing((List<Object> x) -> x.get(3).toString()));
I have also had these problems a couple of times, especially with Comparator. 我也遇到过几次这些问题,尤其是在使用Comparator时。
Eclipse and/or the java compiler have trouble infering the types correctly
.
Eclipse和/或Java编译器在正确推断类型时遇到麻烦
。 As Holger pointed out, this is not a bug but working as specified: The types in the Comparator cannot be inferred solely by the expected type of sorted
's parameter. 正如Holger指出的那样,这不是错误,而是按指定的方式工作:比较器中的类型不能仅通过sorted
参数的预期类型来推断。 You have to type manually/explicitly to give it enough info to compile: 您必须手动/明确输入以提供足够的信息来进行编译:
List<List<Object>> listOutput = new ArrayList<>();
listOutput = listOutput.stream()
.sorted(Comparator.comparing((List<Object> o) -> o.get(3).toString())
.thenComparing(o -> o.get(2).toString()))
.collect(Collectors.toList());
In the subsequent thenComparing
s the type is then correctly recognized. 然后在随后的thenComparing
正确识别类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.