Found 7442 Articles for Java

Collectors collectingAndThen() method in Java 8

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

1K+ Views

The collectingAndThen() method in Java Collectors class acclimates a Collector to perform an additional finishing transformation. It returns collector which performs the action of the downstream collector, followed by an additional ending step.The syntax is as follows.static Collector collectingAndThen(Collector downstream, Function finisher)Here, the parameter, T − Type of the input elementsA − Intermediate accumulation type of the downstream collectorR − The result type of the downstream collectorRR − The result type of the resulting collectordownstream − Collectorfinisher − A function to be applied to the final result of the downstream collectorTo work with Collectors class in Java, import the ... Read More

IntStream mapToLong() method in Java

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

725 Views

The mapToLong() function in IntStream class returns a LongStream consisting of the results of applying the given function to the elements of this stream.The syntax is as follows.LongStream mapToLong(IntToLongFunction mapper)Here, the parameter mapper is a stateless function to apply to each element.Create an IntStream with some elements in the Stream.IntStream intStream = IntStream.of(50, 100, 150, 200);Now create a LongStream and use the mapToLong() with a condition.LongStream longStream = intStream.mapToLong(num → (long)num);The following is an example to implement IntStream mapToLong() method in Java.Exampleimport java.util.*; import java.util.stream.IntStream; import java.util.stream.LongStream; public class Demo {    public static void main(String[] args) {     ... Read More

IntStream mapToObj() method in Java

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

3K+ Views

The mapToObj() method in the IntStream class returns an object-valued Stream consisting of the results of applying the given function to the elements of this stream.The syntax is as follows. StreammapToObj(IntFunction

LongStream iterator() method in Java

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

161 Views

The iterator() method of the LongStream class in Java returns an iterator for the elements of this stream.The syntax is as follows.PrimitiveIterator.OfLong iterator()Here, PrimitiveIterator is an Iterator specialized for long values.To use the LongStream class in Java, import the following package.import java.util.stream.LongStream;The following is an example to implement LongStream iterator() method in Java.Example Live Demoimport java.util.*; import java.util.stream.LongStream; public class Demo {    public static void main(String[] args) {       LongStream longStream = LongStream.of(15000L, 17000L, 25000L);       PrimitiveIterator.OfLong i = longStream.iterator();       while (i.hasNext()) {          System.out.println(i.nextLong());       }   ... Read More

Create Octet Tuple using with() method in Java

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

114 Views

You can easily create Octet Tuple in Java using with() method. Let us first see what we need to work with JavaTuples. To work with Octet class in JavaTuples, you need to import the following package.import org.javatuples.Octet;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 an example to create Octet Tuple using with() ... Read More

What is AbstractCollection class in Java?

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

514 Views

The AbstractCollection class provides an implementation of the Collection interface. This is done to minimize the effort in the implementation of this interface.For an unmodifiable collectionExtend this class and provide implementations for the iterator and size methods.For modifiable collectionAdditionally override the add() method of the class. The iterator method returns the iterator and it must implement the remove() method.The syntax is as follows.public abstract class AbstractCollection extends Object implements CollectionHere, Object is the root of the class hierarchy and Collection is a group of objects.To work with AbstractCollection class in Java, import the following package.import java.util.AbstractCollection;Let us now see an ... Read More

StringJoiner setEmptyValue() method in Java 8

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

366 Views

The setEmptyValue() method of the StringJoiner class in Java 8 sets the sequence of characters. These characters are to be used when determining the string representation of this StringJoiner and when it is empty. That would be none of the elements have been added.The syntax is as followspublic StringJoiner setEmptyValue(CharSequence emptyVal)Here, emptyVal are the characters to return as the value of an empty StringJoinerTo work with the StringJoiner in Java 8, import the following package.import java.util.StringJoiner;The following is an example to implement StringJoiner setEmptyValue() method in Java:Example Live Demoimport java.util.StringJoiner; public class Demo {    public static void main(String[] args) { ... Read More

LocalDateTime getDayOfYear() method in Java

Samual Sam
Updated on 30-Jul-2019 22:30:25

109 Views

The day of the year for a particular LocalDateTime can be obtained using the getDayOfYear() method in the LocalDateTime class in Java. This method requires no parameters and it returns the day of the year which can be in the range of 1 to 365 and also 366 for leap years.A program that demonstrates this is given as follows −Example Live Demoimport java.time.*; public class Demo {    public static void main(String[] args) {       LocalDateTime ldt = LocalDateTime.parse("2019-02-18T11:30:15");       System.out.println("The LocalDateTime is: " + ldt);       System.out.println("The day of the year is: " + ... Read More

LocalDateTime getHour() method in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

153 Views

The hour of the day for a particular LocalDateTime can be obtained using the getHour() method in the LocalDateTime class in Java. This method requires no parameters and it returns the hour of the day which can range from 0 to 23.A program that demonstrates this is given as follows −Example Live Demoimport java.time.*; public class Demo {    public static void main(String[] args) {       LocalDateTime ldt = LocalDateTime.parse("2019-02-18T15:28:35");       System.out.println("The LocalDateTime is: " + ldt);       System.out.println("The hour is: " + ldt.getHour());    } }OutputThe LocalDateTime is: 2019-02-18T15:28:35 The hour is: 15Now let ... Read More

LocalDateTime getYear() method in Java

Samual Sam
Updated on 30-Jul-2019 22:30:25

643 Views

The year for a particular LocalDateTime can be obtained using the getYear() method in the LocalDateTime class in Java. This method requires no parameters and it returns the year which can range from MIN_YEAR to MAX_YEAR.A program that demonstrates this is given as follows −Example Live Demoimport java.time.*; public class Demo {    public static void main(String[] args) {       LocalDateTime ldt = LocalDateTime.parse("2019-02-18T15:28:35");       System.out.println("The LocalDateTime is: " + ldt);       System.out.println("The year is: " + ldt.getYear());    } }OutputThe LocalDateTime is: 2019-02-18T15:28:35 The year is: 2019Now let us understand the above program.First the ... Read More

Advertisements