2

How do i sort this using Lambda expression? I am suppose to sort the first 7 numbers and exclude the last number. I saw that IntStream.concat can be used but i need to use Lambda expression to sort.

     Random random = new Random();
        List <Integer> lucky = random.ints (1, 64)
                                     .distinct()
                                     .limit(8)
                                     .boxed()
                                     .sorted()
                                     .collect(Collectors.toList());
6
  • Sort based on what? Commented Nov 7, 2018 at 18:22
  • Why insisting on using a lambda? Commented Nov 7, 2018 at 18:22
  • Do you need to generate random ints and sort them in one expression or can it be done in 2 expressions? Commented Nov 7, 2018 at 18:27
  • @NicholasK sort based on ascending order. Commented Nov 7, 2018 at 18:52
  • @Sweeper Preferably lambda as we are required to implement lambda expression Commented Nov 7, 2018 at 18:52

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Surprisingly, I don't think this implementation of Comparator violates the general contract! I so thought it was going to violate the contract.
I was planning on using Collections.sort(lucky.sublist(0,6)) as i don't see the point in using Lambda just to sort unless i misunderstood what my prof meant when he said "I do no expect you to implement some sorting methods to sort them. You are working on functional programming; Lambda can help you to achieve this target. "
@KevinChee well, you aren’t really writing a sorting method. The lambda only compares two integers.
2

You can use lambda as a second parameter to Collections.sort(), providing sublist as the first parameter:

Collections.sort(lucky.subList(0, lucky.size()-1), 
                 (i1, i2) -> i1.compareTo(i2));

This will be equivalent to Collections.sort(lucky.subList(0, lucky.size()-1)), so you don't really need this lambda expression here.

Another way to do it (not an efficient one) would be using Stream.concat():

List<Integer> sorted = Stream.concat(
        lucky.stream()
             .filter(elem -> !elem.equals(lucky.get(lucky.size() - 1))).sorted(),
        Stream.of(lucky.get(lucky.size() - 1)))
                       .collect(Collectors.toList());

Note that I am filtering items in the first stream based on value, not the index, as items in lucky list are distinct. This could be done based on the index, though that would make performance of that sorting even worse.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.