Tutorialspoint

Java IntStream average() method

import java.util.*;
import java.util.stream.IntStream;
public class Demo {
   public static void main(String[] args) {
      IntStream intStream = IntStream.of(15, 13, 45, 18, 89, 70, 76, 56);
      OptionalDouble res = intStream.average();
      System.out.println("Average of the elements of the stream...");
      if (res.isPresent()) {
         System.out.println(res.getAsDouble());
      } else {
         System.out.println("Nothing!");
      }
   }
}

Java LongStream.Builder build() method

import java.util.stream.LongStream;
public class Demo {
   public static void main(String[] args) {
      LongStream.Builder builder = LongStream.builder();
      builder.accept(255L);
      builder.accept(570L);
      builder.accept(355L);
      builder.accept(155L);
      builder.accept(955L);
      builder.accept(1800L);
      builder.accept(155L);
      builder.accept(2900L);
      builder.build().forEach(System.out::println);
   }
}

Java IntStream count() method

import java.util.*;
import java.util.stream.IntStream;
public class Demo {
   public static void main(String[] args) {
      IntStream intStream = IntStream.of(30, 13, 67, 56, 89, 99, 76, 56);
      long num = intStream.count();
      System.out.println("Count of elements in the stream: "+num);
   }
}

Java IntStream min() method

import java.util.*;
import java.util.stream.IntStream;
public class Demo {
   public static void main(String[] args) {
      IntStream intStream = IntStream.of(89, 45, 67, 12, 78, 99, 100);
      OptionalInt res = intStream.min();
      System.out.println("The minimum element of this stream:");
      if (res.isPresent()) {
         System.out.println(res.getAsInt());
      } else {
         System.out.println("Nothing!");
      }
   }
}

Java LongStream forEach() method

import java.util.*;
import java.util.stream.LongStream;
public class Demo {
   public static void main(String[] args) {
      LongStream longStream = LongStream.of(1000L, 12000L, 30000L);
      longStream.forEach(System.out::println);
   }
}

Java lastIndexOf() method of AbstractList class

import java.util.ArrayList;
import java.util.AbstractList;
public class Demo {
   public static void main(String[] args) {
      AbstractList<Integer> myList = new ArrayList<Integer>();
      myList.add(75);
      myList.add(100);
      myList.add(150);
      myList.add(200);
      myList.add(250);
      myList.add(150);
      myList.add(150);
      myList.add(400);
      System.out.println("Elements in the AbstractList = " + myList);
      System.out.println("Last index of element 150 = "+ myList.lastIndexOf(150));
   }
}

Java IntStream skip() method

import java.util.*;
import java.util.stream.IntStream;
public class Demo {
   public static void main(String[] args) {
      IntStream intStream = IntStream.range(20, 40);
      intStream.skip(15).forEach(System.out::println);
   }
}

Java IntStream generate() method

import java.util.*;
import java.util.stream.IntStream;
public class Demo {
   public static void main(String[] args) {
      IntStream intStream = IntStream.generate(()
      -> { return (int)(Math.random() * 100); });
      intStream.limit(3).forEach(System.out::println);
   }
}

Java IntStream allMatch()

import java.util.*;
import java.util.stream.IntStream;
public class Demo {
   public static void main(String[] args) {
      IntStream intStream = IntStream.of(15, 20, 25, 40, 50, 80);
      boolean res = intStream.allMatch(a -> a < 30);
      System.out.println("Do all the elements of the stream match the predicate? "+res);
   }
}

Java IntStream allMatch() method

import java.util.*;
import java.util.stream.IntStream;
public class Demo {
   public static void main(String[] args) {
      IntStream intStream = IntStream.of(55, 65, 70, 90, 100);
      boolean res = intStream.allMatch(a -> a > 50);
      System.out.println("Does all the elements of the stream matches the predicate?"+res);
   }
}

Previous 1 ... 3 4 5 6 7 8 9 ... 2134 Next
Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.