Found 9326 Articles for Object Oriented Programming

StringJoiner add() method in Java 8

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

148 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

Instant hashCode() method in Java

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

130 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

The addIfAbsent() method of CopyOnWriteArrayList in Java

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

491 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

LongStream peek() method in Java

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

114 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

LongStream rangeClosed() method in Java

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

233 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

Collectors averagingDouble() method in Java 8

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

363 Views

The averagingDouble() method of the Collectors class in Java 8 returns a Collector that is the arithmetic mean of a double-valued function applied to the input elements.The syntax is as follows −public static Collector averagingDouble(ToDoubleFunction

The set() method of CopyOnWriteArrayList in Java

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

75 Views

The set() method of the CopyOnWriteArrayList class is used to replace the element at the specified position in this list with the specified element. It returns the element that gets replaced.The syntax is as followspublic E set(int index, E ele)Here, the parameter index is the index of the element to replace and ele is the element to be stored at the specified position.To work with CopyOnWriteArrayList class, you need to import the following packageimport java.util.concurrent.CopyOnWriteArrayList;The following is an example to implement CopyOnWriteArrayList class set() method in JavaExample Live Demoimport java.util.concurrent.CopyOnWriteArrayList; public class Demo { public static void main(String[] ... Read More

ArrayBlockingQueue clear() Method in Java

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

77 Views

The clear() method of the ArrayBlockingQueue class in Java is used to remove all the elements from this queue.The syntax is as followspublic void clear()To work with ArrayBlockingQueue class, you need to import the following packageimport java.util.concurrent.ArrayBlockingQueue;The following is an example to implement clear() method of Java ArrayBlockingQueue classExample Live Demoimport java.util.ArrayList; import java.util.Iterator; import java.util.concurrent.ArrayBlockingQueue; public class Demo { public static void main(String[] args) throws InterruptedException { ArrayBlockingQueue q = new ArrayBlockingQueue(10); q.add(120); q.add(10); ... Read More

Generate Infinite Stream of Integers in Java using Random.ints()

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

162 Views

To generate Infinite Stream of Integers, you can use the Random class and its ints() methodRandom.ints()Here, we have used the ints() method to get the next integer.The following is an example displaying how to generate Infinite Stream of Integers with Random.ints() in JavaExampleimport java.util.stream.*; import java.util.*; public class Demo { public static void main(String[] args) { Random r = new Random(); r.ints().forEach(System.out::println); } }Output78799000099 8787879898 2872737888 . . .

LongStream max() method in Java

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

200 Views

The max() method of the LongStream class in Java returns an OptionalLong describing the maximum element of this stream, or an empty optional if this stream is empty.The syntax is as followsOptionalLong max()Here, OptionalLong is a container object which may or may not contain a long value.To use the LongStream class in Java, import the following packageimport java.util.stream.LongStream;The following is an example to implement LongStream max() method in Java. The isPresent() method of the OptionalLong class returns true if the value is presentExample Live Demoimport java.util.*; import java.util.stream.LongStream; public class Demo {    public static void main(String[] args) {     ... Read More

Advertisements