Found 4332 Articles for Java 8

IntStream boxed() method in Java

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

2K+ Views

The boxed() method of the IntStream class returns a Stream consisting of the elements of this stream, each boxed to an Integer.The syntax is as follows.Stream boxed()At first, create an IntStreamIntStream intStream = IntStream.range(20, 30);Now, use the boxed() method to return a Stream consisting of the elements of this stream, each boxed to an Integer.Stream s = intStream.boxed();The following is an example to implement IntStream boxed() method in Java.Example Live Demoimport java.util.*; import java.util.stream.Stream; import java.util.stream.IntStream; public class Demo {    public static void main(String[] args) {       IntStream intStream = IntStream.range(20, 30);       Stream s = ... Read More

The indexOf() method of AbstractList class in Java

Chandu yadav
Updated on 30-Jul-2019 22:30:25

94 Views

The indexOf() method is used to return the index of the first occurrence of the specified element in this list. If the list is empty, it returns -1.The syntax is as follows.int indexOf(Object ob)Here, the parameter ob is the element to search for.To work with the AbstractList class, import the following package.import java.util.AbstractList;The following is an example to implement indexOf() method of the AbstractlList class in Java.Example Live Demoimport java.util.ArrayList; import java.util.AbstractList; public class Demo {    public static void main(String[] args) {       AbstractList myList = new ArrayList();       myList.add(5);       myList.add(20);     ... Read More

Collectors.joining() method in Java 8

Anvi Jain
Updated on 30-Jul-2019 22:30:25

2K+ Views

The joining() method of the Collectors class in Java 8 returns a Collector that concatenates the input elements into a String, in encounter order.The syntax is as follows −public static Collector joining()Here, CharSequence is a readable sequence of char values, whereas String class represents character strings.To work with Collectors class in Java, import the following package −import java.util.stream.Collectors;The following is an example to implement Collectors.joining() method in Java 8 −Example Live Demoimport 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 list ... Read More

The subList() method of AbstractList class in Java

George John
Updated on 30-Jul-2019 22:30:25

83 Views

The subList() method returns a part of this list between the specified fromIndex, inclusive, and toIndex, exclusive. Get a sublist using the method by setting the range as the two parameters.The syntax is as follows.public List subList(int fromIndex, int toIndex)Here, the parameter fromIndex is the low endpoint (inclusive) of the subList and toIndex is the high endpoint (exclusive) of the subList.To work with the AbstractList class, import the following package.import java.util.AbstractList;The following is an example to implement subList() method of the AbstractlList class in Java.Exampleimport java.util.ArrayList; import java.util.AbstractList; public class Demo {    public static void main(String[] args) {   ... Read More

IntStream toArray() method in Java

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

211 Views

The toArray() method returns an array containing the elements of this stream.Set the elements in the stream with the IntStream class of() method.IntStream stream = IntStream.of(20, 40, 60, 70, 100, 120, 140);Now, display an array with the elements of this stream using the toArray() method.int[] myArr = stream.toArray();The following is the syntax.int[] toArray()The following is an example to implement IntStream toArray() method in Java.Example Live Demoimport 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));    } }Output[20, 40, 60, 70, 100, 120, 140]

Collectors averagingInt() method in Java 8

Smita Kapse
Updated on 30-Jul-2019 22:30:25

772 Views

The averagingInt() method of the Collectors class returns a Collector that produces the arithmetic mean of a int-valued function applied to the input elements.The syntax is as follows −static Collector averagingInt(ToIntFunction

Period isNegative() method in Java

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

182 Views

It can be checked if the days, months and years in the Period are negative or not using the isNegative() method in the Period class in Java. This method requires no parameters. Also, it returns true if any of the days, months and years in the Period are negative and false if all of the days, months and years in the Period are positive.A program that demonstrates this is given as followsExample Live Demoimport java.time.Period; public class Demo {    public static void main(String[] args) {       String period = "P5Y9M4D";       Period p = Period.parse(period);   ... Read More

IntStream mapToDouble() method in Java

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

700 Views

The mapToDouble() method returns a DoubleStream consisting of the results of applying the given function to the elements of this stream.The syntax is as follows.DoubleStream mapToDouble(IntToDoubleFunction mapper)Here, the parameter mapper is the stateless function applied to each element.Create an IntStream with some elements.IntStream intStream = IntStream.of(5, 20, 25, 45, 60, 75, 85, 90);Now, use the mapToDouble() method to return a DoubleStream.DoubleStream doubleStream = intStream.mapToDouble(val -> (double) val);The following is an example to implement IntStream mapToDouble() method in Java.Example Live Demoimport java.util.*; import java.util.stream.IntStream; import java.util.stream.DoubleStream; public class Demo {    public static void main(String[] args) {       IntStream intStream ... Read More

Period equals() method in Java

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

99 Views

The equality of two Periods can be determined using the equals() method in the Period class in Java. This method requires a single parameter i.e. the Period object to be compared. Also it returns true if both the Period objects are equal and false otherwise.A program that demonstrates this is given as followsExample Live Demoimport java.time.Period; public class Demo {    public static void main(String[] args) {       String period1 = "P5Y7M15D";       Period p1 = Period.parse(period1);       String period2 = "P5Y7M15D";       Period p2 = Period.parse(period2);       System.out.println("The Period p1 ... Read More

Collectors counting() method in Java 8

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

1K+ Views

The counting() method of the Java 8 Collectors class returns a Collector accepting elements of type T that counts the number of input elements.The syntax is as follows −static Collector counting()Here, the parameter −T − type of input elementsLong − This class value of the primitive type long in an objectTo work with Collectors class in Java, import the following package −import java.util.stream.Collectors;The following is an example to implement counting() method in Java 8 −Example Live Demoimport java.util.stream.Collectors; import java.util.stream.Stream; public class Demo {    public static void main(String[] args) {       Stream stream = Stream.of("25", "50", ... Read More

Advertisements