1

I want to optimize following code:

List<Integer> numbers = Arrays.asList(5, 3, 2, 8);
// sorted Array of all numbers>2
        Arrays.asList(sortWithCondition(numbers, i -> i > 2)).forEach(
                data -> System.out.println("MM=" + data));



public static Integer[] sortWithCondition(List<Integer> numbers,
Predicate<Integer> predicate) {
    IntStream intStream = numbers.parallelStream().filter(predicate)
                    .mapToInt(i -> i).sorted();

            Object[] objArray = intStream.boxed().collect(Collectors.toList())
                    .toArray();

            Integer[] intArray = new Integer[objArray.length];
            for (int j = 0; j < intArray.length; j++) {
                intArray[j] = (Integer) objArray[j];
            }
            return intArray;
        }
}

Output: MM=3 MM=5 MM=8

I want to sort number if particular condition satisfied.

How to convert Stream<Integer> to Integer[] directly? and

How to convert IntStream to Integer[] directly?

7
  • Possible duplicate of How to Convert a Java 8 Stream to an Array? Commented Mar 9, 2018 at 8:02
  • 1
    Even if you have to copy an array to get a different type, you can simply use, e.g. Arrays.copyOf(objArray, objArray.length, Integer[].class) to get Integer[]. But your entire code bears several unnecessary steps. You can replace the entire method with return .toArray(Integer[]::new);; forget about parallel stream, your list is way too small to get an advantage from parallel processing. Or replace the entire code with numbers.stream().filter(predicate).sorted().forEachOrdered(data -> System.out.println("MM=" + data)); Commented Mar 9, 2018 at 8:21
  • can you post code for this algorithm assuming list is large Commented Mar 9, 2018 at 8:25
  • You can just replace stream() with parallelStream(). You may try both and measure to find out whether there is a benefit. Commented Mar 9, 2018 at 8:26
  • @Seelenvirtuose I'm looking for minimal code which can avoid these conversions. Commented Mar 9, 2018 at 8:32

2 Answers 2

3

Note the when you have to copy an array to get a different type, you can simply use, e.g. Arrays.copyOf(objArray, objArray.length, Integer[].class) to get Integer[]. That’s just for the case you will need it in the future, as here, you don’t need it as you can simply call .toArray(Integer[]::new) on the stream to get the right array in the first place.

But since your subsequent processing step is to do Arrays.asList(…).forEach(data -> System.out.println("MM=" + data)); on the result, there is no need to collect the data at all. Apply the action to the stream elements in the first place:

numbers.stream()
       .filter(i -> i > 2)
       .sorted()
       .forEachOrdered(data -> System.out.println("MM=" + data));

This also omits the unboxing and re-boxing steps.

This doesn’t use a parallel stream, your list is way too small to get an advantage from parallel processing and for such simple tasks you usually need a really large list to get a benefit. But you may replace stream() with parallelStream() at any time and try and measure to find out whether there is a benefit.

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

2 Comments

As it related to optimisation. what's the problem in using parallelStream here?
@AdeshKumar parallel processing always incurs an overhead for splitting the work, sending tasks to other threads (which might even have to be started first) and to join and assemble the result. You need a sufficiently large workload truly running in parallel, to get a benefit that compensates (in the best case: overcompensates) the overhead. For small workloads, the disadvantages will dominate.
2

How to convert Stream to Integer[] directly?

stream.toArray(Integer[]::new);

How to convert IntStream to Integer[] directly?

intstream.boxed().toArray(Integer[]::new);

However, rethink your use of Integer[]. If you are dealing with boxed type, might as well put them in a List<Integer> and if you going with arrays, it is better to use a primitive array int[]

1 Comment

…and if the OP is only going to chain .forEach(data -> System.out.println("MM=" + data)) to it, neither, List nor array, are really necessary, just chaining .forEachOrdered(data -> System.out.println("MM=" + data)) to the stream would be enough.

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.