Seiten

Montag, 10. Februar 2014

JDK8/Streams are cool? Here is the answer. ;-)

Today I was preparing a few slides for my next JUG talk. For this if started with a normal piece of code like the following.
  
    public List<List<Integer>> generateDemoValueMatrix() {
        final List<List<Integer>> resultMatrix = new ArrayList<>();
        final Random random = new Random();
        for(int anzahlKurven = 0; anzahlKurven <ANZAHL_KURVEN; anzahlKurven++){
            final List<Integer> result = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                final int nextInt = random.nextInt(100);
                result.add(nextInt);
            }
            resultMatrix.add(result);
        }
        return resultMatrix;
    }

Next step would be the creation of two methods...
  
    public List<List<Integer>> generateDemoValueMatrix() {
        final List<List<Integer>> result = new ArrayList<>();
        for(int anzahlKurven = 0; anzahlKurven <ANZAHL_KURVEN; anzahlKurven++){
            final List<Integer> demoValuesForY = generateDemoValuesForY();
            result.add(demoValuesForY);
        }
        return result;
    }
    public List<Integer> generateDemoValuesForY() {
        final Random random = new Random();
        final List<Integer> result = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            final int nextInt = random.nextInt(100);
            result.add(nextInt);
        }
        return result;
    }
Ok, look´s like always... booooring.... So I started with Streams..
    public List<List<Integer>> generateDemoValueMatrix() {
        return Stream
                .generate(this::generateDemoValuesForY)
                .limit(ANZAHL_KURVEN)
                .collect(Collectors.toList());
    }
    public List<Integer> generateDemoValuesForY(){
        final Random random = new Random();
        return Stream
                .generate(() -> {
                    return random.nextInt(100);
                })
                .limit(10)
                .collect(Collectors.toList());
    }
Not really better.. only new syntax.. now reducing the syntax..
    public List<List<Integer>> generateDemoValueMatrix() {
        return Stream
                .generate(this::generateDemoValuesForY)
                .limit(ANZAHL_KURVEN)
                .collect(Collectors.toList());
    }
    public List<Integer> generateDemoValuesForY(){
        final Random random = new Random();
        return Stream
                .generate(() -> random.nextInt(100))
                .limit(10)
                .collect(Collectors.toList());
    }

But Random offer something new.
    public List<List<Integer>> generateDemoValueMatrix() {
        return Stream
                .generate(this::generateDemoValuesForY)
                .limit(ANZAHL_KURVEN)
                .collect(Collectors.toList());
    }
    public List<Integer> generateDemoValuesForY(){
        return new Random()
                .ints(0, 100)
                .limit(10)
                .boxed()
                .collect(Collectors.toList());
    }
Combining both methods..
    public List<List<Integer>> generateDemoValueMatrix() {
        final Random random = new Random();
        return Stream.generate(
                    () -> Stream.generate(
                            () -> random.nextInt(100))
                            .limit(10)
                            .collect(Collectors.toList())
                )
                .limit(ANZAHL_KURVEN)
                .collect(Collectors.toList());
    }
OK, not short enough.. we could it better..
    public List<List<Integer>> generateDemoValueMatrix() {
        return Stream.generate(
                () -> new Random()
                        .ints(0, 100)
                        .limit(10)
                        .boxed()
                        .collect(Collectors.toList())
                ).limit(ANZAHL_KURVEN)
                .collect(Collectors.toList());
    }
Uuuppsss.. static imports possible.. ;-)
    public List<List<Integer>> generateDemoValueMatrix() {
        return generate(() -> new Random()
                .ints(0, 100)
                .limit(10).boxed().collect(toList())
        ).limit(ANZAHL_KURVEN).collect(toList());
    }
Now compare with the first and decide ;-) Streams are cool ? I think so!!