Google Prettify

2019-05-13

FixedOrderComparator簡介

Apache commons collections 是一套專門針對Collection進行處理的一系列工具。其中提供了一些Comparactor,可以針對Collection來進行處理。這些Comparactor,大都放在org.apache.commons.collections.comparators package中。本篇文章主要針對其中的FixedOrderComparator進行demo:

以下是範例:

String[] data = new String[] { "GUEST", "NORMAL", "NORMAL", "PAID", "VIP", "VIP", "GUEST", "PAID" };
System.out.println(Arrays.toString(data));
Comparator<String> comparator = new FixedOrderComparator(new String[] { "PAID", "VIP", "NORMAL", "GUEST" });
Arrays.sort(data, comparator);
System.out.println(Arrays.toString(data));

[GUEST, NORMAL, NORMAL, PAID, VIP, VIP, GUEST, PAID]
[PAID, PAID, VIP, VIP, NORMAL, NORMAL, GUEST, GUEST]

沒有留言 :

您或許對這些有興趣

Related Posts with Thumbnails

最後

謝謝您的閱讀,希望您可以有豐富的收獲。