The requirement of "use a lambda expression" is quite weird. I can fulfil this requirement simply by replace the .limit call with
.limit(((IntSupplier)() -> 8).getAsInt())
Look! I've used a lambda there! () -> 8. And then you can move on to sort the problem with concat as you said.
Obviously, this is not what you meant.
If you want to put a lambda into the sort method to sort the first 7 integers and then always leave the 8th at the end, you could do something like this:
Random random = new Random();
List<Integer> unsorted = random.ints(1, 64)
.distinct()
.limit(8)
.boxed()
.collect(Collectors.toList());
// here you need to get the last element that you don't want to sort
int last = unsorted.get(unsorted.size() - 1);
// here is the lambda
List<Integer> sorted = unsorted.stream().sorted((x, y) -> {
if (Integer.compare(x, y) == 0) {
return 0;
}
// if any one of the arguments is the last one...
if (last == x) {
return 1;
}
if (last == y) {
return -1;
}
return Integer.compare(x, y);
}).collect(Collectors.toList());
// you can also use "List.sort" with the same lambda
Note that I personally find this sorted method call very unreadable. I can't see at first glance that you are trying to sort everything but the last. In terms of readability, using concat would be better.