Tutorialspoint

Compile and Execute Java8 Online

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(5);
      myList.add(20);
      myList.add(35);
      myList.add(47);
      myList.add(55);
      myList.add(70);
      myList.add(100);
      myList.add(140);
      myList.add(180);
      myList.add(250);
      System.out.println("Elements in AbstractList = " + myList);
      List<Integer> list = myList.subList(5, 7);
      System.out.println("Sublist of AbstractList: "+ list);
   }
}

Collectors.joining() method in Java 8

import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.Arrays;
import java.util.List;

public class Demo {
   public static void main(String[] args) {
      List<Character> list = Arrays.asList('D', 'e', 'm', 'o');
      String str = list.stream().map(String::valueOf).collect(Collectors.joining());
      System.out.println("Concatenated = "+str);
   }
}

Java IntStream toArray() method

import java.util.*;
import java.util.stream.IntStream;
public class Demo {
   public static void main(String[] args) {
      IntStream stream = IntStream.of(20, 40, 60, 70, 100, 120, 140);
      int[] myArr = stream.toArray();
      System.out.println(Arrays.toString(myArr));
   }
}

Java IntStream mapToDouble() method

import java.util.*;
import java.util.stream.IntStream;
import java.util.stream.DoubleStream;
public class Demo {
   public static void main(String[] args) {
      IntStream intStream = IntStream.of(5, 20, 25, 45, 60, 75, 85, 90);
      DoubleStream doubleStream = intStream.mapToDouble(val -> (double) val);
      doubleStream.forEach(System.out::println);
   }
}

Java ArrayBlockingQueue peek() Method

import java.util.concurrent.ArrayBlockingQueue;
public class Demo {
   public static void main(String[] args) throws InterruptedException {
      ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(10);
      q.add(200);
      q.add(310);
      q.add(400);
      q.add(450);
      q.add(500);
      q.add(550);
      q.add(700);
      System.out.println("ArrayBlockingQueue = " + q);
      System.out.println("Head of the queue = "+q.peek());
   }
}

Java LongStream allMatch()

import java.util.stream.LongStream;
public class Demo {
   public static void main(String[] args) {
      LongStream longStream = LongStream.of(100L, 150L, 200L, 300L, 400L, 500L);
      boolean res = longStream.allMatch(a -> a > 50L);
      System.out.println("Do all the elements match the predicate? "+res);
   }
}

Java LongStream anyMatch() method

import java.util.stream.LongStream;
public class Demo {
   public static void main(String[] args) {
      LongStream longStream = LongStream.of(100L, 150L, 200L, 300L, 400L, 500L);
      boolean res = longStream.anyMatch(a -> a > 350L);
      System.out.println("Do any of the element match the predicate? "+res);
   }
}

Java LongStream of() method

import java.util.stream.LongStream;
public class Demo {
   public static void main(String[] args) {
      LongStream longStream = LongStream.of(20000L, 35000L, 40000L, 55000L);
      longStream.forEach(System.out::println);
   }
}

Java LongStream findFirst() method

import java.util.OptionalLong;
import java.util.stream.LongStream;
public class Demo {
   public static void main(String[] args) {
      LongStream longStream = LongStream.of(25000L, 35000L, 40000L, 50000L, 60000L);
      OptionalLong res = longStream.findFirst();
      System.out.println("The first element of the stream: ");
      if (res.isPresent())
         System.out.println(res.getAsLong());
      else
         System.out.println("Nothing!");
   }
}

Java LongStream empty() method

import java.util.stream.LongStream;
public class GFG {
   public static void main(String[] args) {
      LongStream longStream = LongStream.empty();
      System.out.println("The count of elements in the stream: "+longStream.count());
   }
}

Advertisements
Loading...

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