使用Java8排序并检查数字是否按连续顺序排列
我正在尝试对一个多头列表进行排序,并验证这些数字是否从1开始按连续顺序排列。我想用java8/streams做所有的事情使用Java8排序并检查数字是否按连续顺序排列,java,java-8,Java,Java 8,我正在尝试对一个多头列表进行排序,并验证这些数字是否从1开始按连续顺序排列。我想用java8/streams做所有的事情 //sorting first List<Long> sortedNums = request.stream().map(Request::getSequence) .collect(Collectors.toList()).stream().sorted().collect(Collectors.toList()); //ge
//sorting first
List<Long> sortedNums = request.stream().map(Request::getSequence)
.collect(Collectors.toList()).stream().sorted().collect(Collectors.toList());
//getting first num to see if the seq starting from 1
Optional<Long> first = sortedNums.stream().findFirst();
if (first.isPresent()) {
if (first.get() != 1) {
throw new BadRequestException("Sequence should start from 1");
}
}
//checking if the list is in consecutive order
if(!isConsecutive(sortedNums)) {
throw new BadRequestException("Sequences should be in consecutive order");
}
private boolean isConsecutive(List<Long> list) {
for (int i = 1; i < list.size(); i++) {
if (list.get(i - 1) + 1 != list.get(i)) {
return false;
}
}
return true;
}
//先排序
List sortedNums=request.stream().map(request::getSequence)
.collect(Collectors.toList()).stream().sorted().collect(Collectors.toList());
//获取第一个num以查看seq是否从1开始
可选的first=sortedNums.stream().findFirst();
if(first.isPresent()){
if(first.get()!=1){
抛出新的BadRequestException(“序列应从1开始”);
}
}
//检查列表是否按连续顺序排列
如果(!isConsecutive(sortedNums)){
抛出新的BadRequestException(“序列应为连续顺序”);
}
私有布尔值是连续的(列表){
对于(int i=1;i
我试图通过流将所有语句组合成一个或两个语句,看看是否有更好的方法来实现这一点 我会用不同的小方法来做这件事,而不是什么都做:
public static boolean startsWith1(List<Long> list){
//return list.stream().sorted().findFirst().get() == 1; //see @Holger's comment
return Collections.min(list) == 1;
}
// The sum of all numbers from 1 to n = n * (n+1) /2;
// you can use that to check if your list contains all numbers from 1 to n
public static boolean isConsecutive(List<Long> list){
long n = list.size();
return list.stream().mapToLong(Long::longValue).distinct().sum() == n * (n+1) /2;
}
public static void doSomething(List<Long> list) throws BadRequestException{
if(!startsWith1(list)){
throw new BadRequestException("Sequence should start from 1 ");
}
if(!isConsecutive(list)) {
throw new BadRequestException("Sequences should be in consecutive order");
}
else{
// do whatever with your list
}
}
publicstaticbooleanststartswith1(列表){
//return list.stream().sorted().findFirst().get()==1;//参见@Holger的注释
返回集合。最小值(列表)=1;
}
//从1到n的所有数字之和=n*(n+1)/2;
//您可以使用它来检查列表是否包含从1到n的所有数字
公共静态布尔值是连续的(列表){
long n=list.size();
return list.stream().mapToLong(Long::longValue).distinct().sum()==n*(n+1)/2;
}
公共静态void doSomething(列表)引发BadRequestException{
如果(!startsWith1(list)){
抛出新的BadRequestException(“序列应从1开始”);
}
如果(!isConsecutive(列表)){
抛出新的BadRequestException(“序列应为连续顺序”);
}
否则{
//用你的清单做任何事
}
}
排序后检查顺序有什么意义?.collect(collector.toList()).stream()
毫无意义。@shmosel我认为OP检查的是数字是否连续,而不仅仅是顺序。如果第一个元素不是一,那么你已经要抛出了,不必费心处理可选项,只需执行:first=sortedNums.stream().findFirst().orElse(-1)代码>啊,你的意思是如果它们是顺序的。.sorted().findFirst()
是获取.min(Comparator.naturalOrder())
的低效方法。在这里,你可以做得更简单:返回集合.min(list)=1代码>@Holger非常感谢你的提示。我只是从OP的代码中采纳了它,并没有考虑太多。的确,Collections.min(list)
比我上面提到的更优雅。更新。