【发布时间】:2019-04-21 07:38:44
【问题描述】:
public static void main(String[] args) throws IOException
{
HashSet set = new HashSet<String>();
set.add("{}");
set.add("{a}");
set.add("{b}");
set.add("{a, b}");
set.add("{a, c}");
sortedSet(set);
}
public static void sortedSet(HashSet set)
{
List<String> setList = new ArrayList<String>(set);
List<String> orderedByAlpha = new ArrayList<String>(set);
//sort by alphabetical order
orderedByAlpha = (List<String>) setList.stream()
.sorted((s1, s2) -> s1.compareToIgnoreCase(s2))
.collect(Collectors.toList());
System.out.println(orderedByAlpha);
}
我正在尝试按字母顺序排序,但我得到的输出是这样的:
[{a, b}, {a, c}, {a}, {b}, {}]
但应该是:
[{a}, {a, b}, {a, c}, {b}, {}]
【问题讨论】:
-
按字母顺序,
,(0x2C) 在}(0x7D) 之前。如果您希望得到不同的结果,则需要编写自己的自定义比较器。
标签: java sorting lambda java-8 comparator