- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 4332 Articles for Java 8
2K+ Views
The toList() method of the Collectors class returns a Collector that accumulates the input elements into a new List.The syntax is as follows −static Collector toList()Here, parameter T is the type of input elements.To work with Collectors class in Java, import the following package −import java.util.stream.Collectors;The following is an example to implement Collectors toList() method in Java −Example Live Demoimport java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class Demo { public static void main(String[] args) { Stream stream = Stream.of("25", "40", "90", "150", "180", "220", "350"); ... Read More
810 Views
The equality of two Instant objects can be determined using the equals() method in the Instant class in Java. This method requires a single parameter i.e. the Instant to be compared. Also it returns true if both the Instant objects are equal and false otherwise.A program that demonstrates this is given as followsExample Live Demoimport java.time.*; import java.time.temporal.ChronoUnit; public class Demo { public static void main(String[] args) { Instant i1 = Instant.parse("2019-01-13T16:10:35.00Z"); Instant i2 = Instant.parse("2019-01-13T16:10:35.00Z"); System.out.println("Instant object i1 is: " + i1); System.out.println("Instant object i2 is: " ... Read More
415 Views
The filter() method of the IntStream class in Java returns a stream consisting of the elements of this stream that match the given predicate.The syntax is as follows −IntStream filter(IntPredicate predicate)Here, the predicate parameter is a stateless predicate to apply to each element to determine if it should be included. The IntPredicate above is the predicate of one int-valued argument.Create an IntStream and add elements −IntStream intStream = IntStream.of(20, 34, 45, 67, 89, 100);Now, set a condition and filter stream elements based on it using the filter() method −intStream.filter(a -> a < 50).The following is an example to implement IntStream ... Read More
82 Views
The empty() method of the LongStream class in Java returns an empty sequential LongStream.The syntax is as follows.static LongStream empty()To use the LongStream class in Java, import the following package.import java.util.stream.LongStream;The following is an example to implement LongStream empty() method in Java.Example Live Demoimport 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()); } }OutputThe count of elements in the stream: 0
150 Views
The add() method of the StringJoiner class is used in Java 8 to add a copy of the given CharSequence value as the next element of the StringJoiner value. If the new element ele is null, then the value null gets added.The syntax is as follows −public StringJoiner add(CharSequence ele)Here, the parameter ele is the element to be added, whereas, CharSequence is a readable sequence of char values.To work with the StringJoiner in Java 8, import the following package −import java.util.StringJoiner;We will first create a StringJoiner and set the delimeter −StringJoiner strJoin = new StringJoiner(", ")Use the add() method to ... Read More
131 Views
The hash code for a particular Instant object can be obtained using the hashCode() method in the Instant class in Java. This method requires no parameters and it returns the hash code for the Instant object.A program that demonstrates this is given as followsExample Live Demoimport java.time.*; import java.time.temporal.ChronoUnit; public class Demo { public static void main(String[] args) { Instant i = Instant.now(); System.out.println("The current instant is: " + i); int hashCode = i.hashCode(); System.out.println("The Hash Code value for the instant is: " + hashCode); } }OutputThe ... Read More
506 Views
The addIfAbsent() method appends the element if it is not in the list. If the element is already in the list, then FALSE is returned.The syntax is as follows.public boolean addIfAbsent(E ele)Here, ele is the element to be added to this list, if it is not already in the list.To work with CopyOnWriteArrayList class, you need to import the following package.import java.util.concurrent.CopyOnWriteArrayList;The following is an example to implement CopyOnWriteArrayList class addIfAbsent() method in Java.Example Live Demoimport java.util.concurrent.CopyOnWriteArrayList; public class Demo { public static void main(String[] args) { CopyOnWriteArrayList arrList = new CopyOnWriteArrayList(); arrList.add(30); arrList.add(40); ... Read More
115 Views
The peek() method of the LongStream class returns a stream with the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.The syntax is as follows −LongStream peek(LongConsumer action)Here, LongConsumer represents an operation that accepts a single long-valued argument and returns no result. The action parameter is a non-interfering action to perform on the elements as they are consumed from the stream.To use the LongStream class in Java, import the following package −import java.util.stream.LongStream;The following is an example to implement LongStream peek() method in JavaExample Live Demoimport java.util.stream.LongStream; public class Demo ... Read More
234 Views
The rangeClosed() method of the LongStream class in Java returns a sequential ordered LongStream from startInclusive to endExclusive by an incremental step of 1. This is inclusive of the initial element and the last element.The syntax is as follows −static LongStream rangeClosed(long startInclusive, long endExclusive)Here, startInclusive is the first value, whereas the endExclusive is the last.To use the LongStream class in Java, import the following package −import java.util.stream.LongStream;The following is an example to implement LongStream rangeClosed() method in Java −Example Live Demoimport java.util.stream.LongStream; public class Demo { public static void main(String[] args) { ... Read More