Found 4335 Articles for Java 8

Program to convert Primitive Array to Stream in Java

Nancy Den
Updated on 30-Jul-2019 22:30:25

179 Views

To convert Primitive Array to Stream, you need to use the of() method.Let’s say the following is our Primitive Array:int[] myArr = new int[] { 20, 50, 70, 90, 100, 120, 150 };Now, use the of() method to convert the primitive array to stream:IntStream stream = IntStream.of(myArr);The following is an example to convert primitive array to stream in Java:Example Live Demoimport java.util.stream.IntStream; import java.util.*; public class Main {    public static void main(String[] args) {       int[] myArr = new int[] { 20, 50, 70, 90, 100, 120, 150 };       System.out.println("The Primitive Array = "+Arrays.toString(myArr));   ... Read More

Convert String to IntStream in Java

Nancy Den
Updated on 30-Jul-2019 22:30:25

213 Views

If you have a string and you want to convert it into IntStream with ASCII values, then it can be easily achieved using the below code.To work with IntStream class, you need to import the following package:import java.util.stream.IntStream;Let’s say the following is our string:String str = "Benz";Convert the above string to IntStream:IntStream stream = str.chars();The following is an example to convert String to IntStream in Java:Example Live Demoimport java.util.stream.IntStream; public class Demo {    public static void main(String[] args) {       String str = "Benz";       System.out.println("String to be converted = " + str);       ... Read More

Convert Character Array to IntStream in Java

Nancy Den
Updated on 30-Jul-2019 22:30:25

380 Views

Let’s say the following is our character array:Character arr[] = { 'V', 'e', 'h', 'i', 'c', 'l' , 'e' };To convert the above character array to IntStreamIntStream stream = Stream.of(arr).flatMapToInt(IntStream::of);We have used the flatMapToInt() method for this.The following is an example to convert character array to IntStream in Java:Example Live Demoimport java.util.stream.*; public class Main {    public static void main(String[] args) {       Character arr[] = { 'V', 'e', 'h', 'i', 'c', 'l' , 'e' };       System.out.println("The character array = ");       for (char value : arr) {          System.out.println("Value ... Read More

The build() method in Java LongStream.Builder

Nancy Den
Updated on 30-Jul-2019 22:30:25

123 Views

The build() method of the LongStream.Builder class builds the stream and returns the built stream. The following is the syntax:LongStream build()Import the following package for the LongStream.Builder class in Java:import java.util.stream.LongStream;Declare a LongStream.Builder and add elements:LongStream.Builder builder = LongStream.builder(); builder.add(24000L); builder.add(47470L); builder.add(12999L);Now, use the build() method to build the stream:builder.build()The following is an example displaying how to implement build() method of LongStream.Builder in Java:Example Live Demoimport java.util.stream.LongStream; public class Demo {    public static void main(String[] args) {       LongStream.Builder builder = LongStream.builder();       builder.add(24000L);       builder.add(47470L);       builder.add(12999L);       ... Read More

The accept() method in Java Stream.Builder

Nancy Den
Updated on 30-Jul-2019 22:30:25

1K+ Views

Add an element to the stream using the accept() method of Java Stream.Builder.The following is the syntax:void accept(T t)Here, t is the argument to be inserted.Import the following package for the Stream.Builder class in Java:import java.util.stream.Stream;First, declare a Stream.Builder:Stream.Builder builder = Stream.builder();Now, use the accept() method:builder.accept("Demo"); builder.accept("Text");The following is an example displaying how to implement accept() method of Stream.Builder in Java:Example Live Demoimport java.util.stream.Stream; public class Demo {    public static void main(String[] args){       Stream.Builder builder = Stream.builder();       builder.accept("Demo");       builder.accept("Text");       Stream str = builder.build();       str.forEach(System.out::println);   ... Read More

The add() method in Java Stream.Builder

Nancy Den
Updated on 30-Jul-2019 22:30:25

381 Views

Use the add() method to insert elements in the Stream.Builder. The element to be added is a parameter for the add().The following is the syntax:default Stream.Builder add(T t)Here, t is the element to be added.Import the following package for the Stream.Builder class in Java:import java.util.stream.Stream;At first, create Stream.Builder:Stream.Builder builder = Stream.builder();Now, add elements using add() method:builder.add("This"); builder.add("is"); builder.add("it!");Here is an example displaying how to implement add() method of Stream.Builder in Java:Example Live Demoimport java.util.stream.Stream; public class Demo {    public static void main(String[] args) {       Stream.Builder builder = Stream.builder();       builder.add("This");       builder.add("is");   ... Read More

StringJoiner toString() method in Java 8

Nancy Den
Updated on 30-Jul-2019 22:30:25

199 Views

The toString() method of the StringJoiner class in Java8 is used to return the string representation of the StringJoiner.The syntax of the toString() method is as follows:String toString()To work with the StringJoiner in Java 8, import the following package:import java.util.StringJoiner;The following is an example to implement StringJoiner toString() method in Java:Example Live Demoimport java.util.StringJoiner; public class Demo {    public static void main(String[] args) {       StringJoiner strJoin = new StringJoiner(" ");       strJoin.add("One");       strJoin.add("Two");       strJoin.add("Three");       strJoin.add("Four");       strJoin.add("Five");       System.out.println(strJoin.toString());    } }outputOne ... Read More

What is StringJoiner class in Java 8?

Nancy Den
Updated on 30-Jul-2019 22:30:25

189 Views

The StringJoiner class in Java 8 constructs a sequence of characters. This sequence is separated by a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix.The following are the constructors of the StringJoiner class:StringJoiner(CharSequence delimiter): This constructor constructs a StringJoiner with no characters in it and with no prefix or suffix. It used the copy of the supplied delimiter.StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix) This constructor constructs a StringJoiner with no characters in it. It uses the copies of the supplied prefix, delimiter and suffix.The syntax is as follows:public final class StringJoiner extends ObjectHere, class ... Read More

Iterate through KeyValue Tuple in Java

Nancy Den
Updated on 30-Jul-2019 22:30:25

339 Views

To iterate through the KeyValue tuple in Java, use the Iterable and loop it through using the for loop. Let us first see what we need to work with JavaTuples. To work with KeyValue class in JavaTuples, you need to import the following package:import org.javatuples.KeyValue;Note: Download JavaTuples Jar library to run JavaTuples program. If you are using Eclipse IDE, then Right Click Project → Properties → Java Build Path → Add External Jars and upload the downloaded JavaTuples jar file. Refer the below guide for all the steps to run JavaTuples:Steps: How to run JavaTuples program in EclipseThe following is ... Read More

Create KeyValue Tuple from another collection in Java

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

53 Views

To create KeyValue tuple from another collection, use the fromCollection() method. We will be creating the tuple from List collection. Let us first see what we need to work with JavaTuples. To work with KeyValue class in JavaTuples, you need to import the following package.import org.javatuples.KeyValue;Note: Download JavaTuples Jar library to run JavaTuples program. If you are using Eclipse IDE, then Right Click Project → Properties → Java Build Path → Add External Jars and upload the downloaded JavaTuples jar file. Refer the below guide for all the steps to run JavaTuples.Steps: How to run JavaTuples program in EclipseThe following ... Read More

Advertisements