对两个元素进行嵌套列表
我有一个嵌套列表,名为outputtostore
其内容以下给出:
[[Final, 331, M, 22/03/2020 00:00:00],
[Initial, 335, M, 22/06/2022 00:00:00],
[Exception, 335, M, 22/05/2022 00:00:00],
[Final, 335, M, 20/06/2022 00:00:00],
[Keep, 335, M, 02/06/2022 11:00:00],
[Final, 335, M, 10/04/2022 02:00:00],
[Deleted, 335, M, 22/06/2022 15:55:10],
[Exception, 335, M, 22/06/2022 15:55:09],
[Final, 335, M, 22/06/2022 15:56:00],
[Initial, 335, M, 11/06/2022 00:00:00]]
我需要基于2条条件进行对此进行排序:第一个是自定义订单:“初始” “,“ final”,“删除”,“ keep”,“异常” ,然后基于 dateTime 。
我能够做到这一点,但不确定这是做到这一点的最佳方法。
我的代码:
List<String> definedOrder = Arrays.asList("Initial","Final","Deleted","Keep","Exception");
Collections.sort(outputToStore, Comparator.comparing(o -> Integer.valueOf(definedOrder.indexOf(o.get(0)))));
Collections.sort(outputToStore,( o1, o2)-> {
// let your comparator look up your car's color in the custom order
try {
if(Integer.valueOf(definedOrder.indexOf(o1.get(0))).compareTo(Integer.valueOf(definedOrder.indexOf(o2.get(0))))==0){
Date date1=simpleDateFormat.parse(o1.get(3));
Date date2=simpleDateFormat.parse(o2.get(3));
return date1.compareTo(date2);
}
} catch (ParseException e) {
e.printStackTrace();
}
return 0;
});
我得到了所需的结果:
[[Initial, 335, M, 11/06/2022 00:00:00],
[Initial, 335, M, 22/06/2022 00:00:00],
[Final, 331, M, 22/03/2020 00:00:00],
[Final, 335, M, 10/04/2022 02:00:00],
[Final, 335, M, 20/06/2022 00:00:00],
[Final, 335, M, 22/06/2022 15:56:00],
[Deleted, 335, M, 22/06/2022 15:55:10],
[Keep, 335, M, 02/06/2022 11:00:00],
[Exception, 335, M, 22/05/2022 00:00:00],
[Exception, 335, M, 22/06/2022 15:55:09]]
但是有什么更好或简洁的方法吗?
I have a nested list, named outputToStore
whose contents are given below:
[[Final, 331, M, 22/03/2020 00:00:00],
[Initial, 335, M, 22/06/2022 00:00:00],
[Exception, 335, M, 22/05/2022 00:00:00],
[Final, 335, M, 20/06/2022 00:00:00],
[Keep, 335, M, 02/06/2022 11:00:00],
[Final, 335, M, 10/04/2022 02:00:00],
[Deleted, 335, M, 22/06/2022 15:55:10],
[Exception, 335, M, 22/06/2022 15:55:09],
[Final, 335, M, 22/06/2022 15:56:00],
[Initial, 335, M, 11/06/2022 00:00:00]]
I need to sort this based on 2 conditions: The first is a custom order: "Initial","Final","Deleted","Keep","Exception" and then based on datetime.
I am able to do it, but not sure that this is the best way to do it.
My code:
List<String> definedOrder = Arrays.asList("Initial","Final","Deleted","Keep","Exception");
Collections.sort(outputToStore, Comparator.comparing(o -> Integer.valueOf(definedOrder.indexOf(o.get(0)))));
Collections.sort(outputToStore,( o1, o2)-> {
// let your comparator look up your car's color in the custom order
try {
if(Integer.valueOf(definedOrder.indexOf(o1.get(0))).compareTo(Integer.valueOf(definedOrder.indexOf(o2.get(0))))==0){
Date date1=simpleDateFormat.parse(o1.get(3));
Date date2=simpleDateFormat.parse(o2.get(3));
return date1.compareTo(date2);
}
} catch (ParseException e) {
e.printStackTrace();
}
return 0;
});
I'm getting the desired result:
[[Initial, 335, M, 11/06/2022 00:00:00],
[Initial, 335, M, 22/06/2022 00:00:00],
[Final, 331, M, 22/03/2020 00:00:00],
[Final, 335, M, 10/04/2022 02:00:00],
[Final, 335, M, 20/06/2022 00:00:00],
[Final, 335, M, 22/06/2022 15:56:00],
[Deleted, 335, M, 22/06/2022 15:55:10],
[Keep, 335, M, 02/06/2022 11:00:00],
[Exception, 335, M, 22/05/2022 00:00:00],
[Exception, 335, M, 22/06/2022 15:55:09]]
But is there any better or concise way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用对象的力量
以您表示数据的方式
,不方便且容易出错。显然,它必须是具有适当类型的属性,而不是字符串列表。 滥用集合的,
它是使用
String
作为数字,日期等的类型 这并不能带来任何优势。除了在控制台上打印外,您无需分析字符串就无能为力。正确的数据类型使您可以访问其字符串
无法为您提供的独特行为。第一个属性,我们称其为
状态
可能是 enum 。 enum - 是一种特殊的类,当您需要代表有限的价值观时,它们非常方便,并且它们具有自然顺序,这与 enum-constants 的顺序(已声明的顺序)。为了表示日期时间信息,我们可以使用
java.time
软件包中的Java 8类之一。在下面的示例中,我将使用localdateTime
(类date
是遗产,避免使用它)。这样的类的外观是:
使用Java 8构建比较器
,以便对
foo
对象的列表进行排序,我们需要定义比较器。为此,我们可以使用
比较器
与Java 8的接口中引入的静态方法。我们可以流利的方式链接这些方法,因为它们都提供了一个比较器。注意:我们需要在A 单个比较器中定义排序逻辑。不要像您在代码中完成的两次数据对数据进行排序,它会导致不必要的性能开销。
要了解如何使用Java-8方法构建比较器,请查看 教程 。
为了排序
foo
对象的列表,我们可以使用方法list.sort()
,该是在Java 9中引入的,作为collections.sort的更流利的替代方案。 ()
:解析数据
如果数据作为字符串的集合来
,则需要对其进行解析。为此,我们需要增强
foo
及其嵌套 enum (下面的链接中提供的完整代码)。注意:
SimpleDateFormat
类是遗产,现代替代方案是dateTimeFormatter
。main()
输出:
a链接在线演示
Use the Power of Objects
The way you're representing your data is inconvenient and error-prone.
It clearly has to be an object having attributes of appropriate types, and not a list of strings. It's misuse of collections
Using
String
as a type for numbers, dates, etc. doesn't give you any advantage. There's nothing you can do with string without parsing apart from printing on the console. Proper data-types give you access to their unique behavior thatString
can't offer you.The first property, let's call it
status
might be an enum. Enum - is a special kind of class, they are very handy when you need to represent a limited set of values, and they have a natural order, that is the same as the order of the enum-constants (the order in which they have been declared).And to represent date-time information we can use one of the Java 8 classes from
java.time
package. In the example below, I'll useLocalDateTime
(classDate
is legacy, avoid using it).That how such a class might look like:
Building Comparators with Java 8
In order to sort a list of
Foo
objects, we need to define a comparator.For that, we can use static methods that were introduced in the
Comparator
interface with Java 8. We can chain these methods in a fluent way because each of them gives a comparator.Note: we need to define the sorting logic in a single comparator. Don't sort the data twice like you've done in your code, it causes unnecessary performance overhead.
To learn how to build comparators using Java-8 methods, have a look at this tutorial.
And to sort a list of
Foo
objects, we can use methodList.sort()
which was introduced in Java 9 as a more fluent alternative toCollections.sort()
:Parsing the data
If data comes to you as a collection of strings, then you need to parse it.
For that we need to enhance
Foo
and its nested enum (the full code provided in the link below).Note:
SimpleDateFormat
class is legacy, the modern alternative isDateTimeFormatter
.main()
Output:
A link to Online Demo
尝试一下。
输出:
Try this.
output:
地图可能更好地用于预定义订单,并使用
localdateTime
按日期进行比较。但否则你走在正确的道路上;您可以将链条链接到比较器:
A map might be better to use for the predefined order and use
LocalDateTime
to compare by date. But else your were on the right track; you could just chainthenComparing
to your Comparator :