几乎没有什么要解决的,您需要确保字符串有两种不同的分类,一种具有大写字母,而另一种则没有.为此,您可以将列表划分为:
Map> partitionedValues = phones.stream()
.collect(Collectors.partitioningBy(a -> containsUpperCase(a)));
在哪里containsUpperCase实现将看起来像:
boolean containsUpperCase(String value) {
for (char ch : value.toCharArray()) {
if (Character.isUpperCase(ch)) {
return true;
}
}
return false;
}
对数据进行这样的分区后,您需要将这些数据汇总到一个最终列表中,如下所示:
List finalOutput = partitionedValues.get(Boolean.TRUE) // with upper case
.stream()
.sorted(Comparator.comparing(String::length)) // sorted by length
.collect(Collectors.toList());
finalOutput.addAll(partitionedValues.get(Boolean.FALSE) // without uppercase
.stream()
.sorted(Comparator.naturalOrder()) // sorted naturally
.collect(Collectors.toList()));
您的最终输出将显示为:
finalOutput.forEach(System.out::println);