Example usage for java.util.stream IntStream range

List of usage examples for java.util.stream IntStream range

Introduction

In this page you can find the example usage for java.util.stream IntStream range.

Prototype

public static IntStream range(int startInclusive, int endExclusive) 

Source Link

Document

Returns a sequential ordered IntStream from startInclusive (inclusive) to endExclusive (exclusive) by an incremental step of 1 .

Usage

From source file:Main.java

public static void main(String[] args) {
    IntStream i = IntStream.range(1, 7);
    i.forEach(System.out::println);

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    IntStream.range(1, 4).mapToObj(i -> "a" + i).forEach(System.out::println);
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    IntStream.range(0, 10).average().ifPresent(System.out::println);
}

From source file:Main.java

public static void main(String[] args) {
    Stream.of("XML", "Java", "CSS").flatMap(name -> IntStream.range(0, name.length()).mapToObj(name::charAt))
            .forEach(System.out::println);
}

From source file:Main.java

public static void main(String[] args) {
    long first = System.currentTimeMillis();
    IntStream.range(0, 4).map(a -> {
        sleep1sec();//from  ww w .j  a v a2s.c om
        return a;
    }).reduce((a, b) -> a * b).ifPresent(System.out::println);

    long second = System.currentTimeMillis();
    System.out.println("time 1:" + (second - first));

    IntStream.range(0, 4).parallel().map(a -> {
        sleep1sec();
        return a;
    }).reduce((a, b) -> a * b).ifPresent(System.out::println);

    long last = System.currentTimeMillis();
    System.out.println("time 2:" + (last - second));

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    IntStream.range(1, 4).mapToObj(num -> new Foo("Foo" + num))
            .peek(f -> IntStream.range(1, 4).mapToObj(num -> new Bar("Bar" + num + " <- " + f.name))
                    .forEach(f.bars::add))
            .flatMap(f -> f.bars.stream()).forEach(b -> System.out.println(b.name));
}

From source file:Main.java

public static void main(String[] argv) {
    System.out.println("Benchmarking...");
    long t0 = System.nanoTime();

    int a[] = IntStream.range(0, 1_000_000).filter(p -> p % 2 == 0).toArray();
    long t1 = System.nanoTime();

    int b[] = IntStream.range(0, 1_000_000).parallel().filter(p -> p % 2 == 0).toArray();
    long t2 = System.nanoTime();

    System.out.printf("serial: %.2fs, parallel %.2fs%n", (t1 - t0) * 1e-9, (t2 - t1) * 1e-9);
}

From source file:Main.java

public static void main(String[] argv) {
    IntStream.rangeClosed(1, 10).forEach(num -> System.out.println(num));

    System.out.println("");

    IntStream.range(1, 10).forEach(num -> System.out.println(num));
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    List<Foo> foos = new ArrayList<>();

    IntStream.range(1, 4).forEach(num -> foos.add(new Foo("Foo" + num)));

    foos.forEach(f -> IntStream.range(1, 4).forEach(num -> f.bars.add(new Bar("Bar" + num + " <- " + f.name))));

    foos.stream().flatMap(f -> f.bars.stream()).forEach(b -> System.out.println(b.name));
}

From source file:Main.java

public static void main(String[] arg) throws Exception {
    Runnable parallelCode = () -> {
        HashSet<String> allThreads = new HashSet<>();
        IntStream.range(0, 1_000_000).parallel().filter(i -> {
            allThreads.add(Thread.currentThread().getName());
            return false;
        }).min();/* www. j a v  a  2  s . com*/
        System.out.println("executed by " + allThreads);
    };
    System.out.println("default behavior: ");
    parallelCode.run();
    System.out.println("specialized pool:");
    ForkJoinPool pool = new ForkJoinPool(2);
    pool.submit(parallelCode).get();
}