Found 34472 Articles for Programming

ArrayBlockingQueue take() method in Java

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

168 Views

The take() method of the ArrayBlockingQueue class fetch and removes the head of this queue, waiting if necessary until an element becomes available.The syntax is as followspublic E take() throws InterruptedExceptionTo work with ArrayBlockingQueue class, you need to import the following packageimport java.util.concurrent.ArrayBlockingQueue;The following is an example to implement take() method of Java ArrayBlockingQueue classExample Live Demoimport java.util.concurrent.ArrayBlockingQueue; public class Demo {    public static void main(String[] args) throws InterruptedException {       ArrayBlockingQueue q = new ArrayBlockingQueue(10);       q.add(200);       q.add(310);       q.add(400);       q.add(450);       q.add(500);     ... Read More

How do I create a random alpha-numeric string using C++?

Nitya Raut
Updated on 30-Jul-2019 22:30:25

725 Views

In this section we will see how to generate a random alphanumeric string using C++. Here we are providing lowercase letters, uppercase letters and numbers (0-9). This program takes the characters randomly, then creates the random string.Input: Here we are giving the string length Output: A random string of that length. Example “XSme6VAsvJ”AlgorithmStep 1:Define array to hold all uppercase, lowercase letters and numbers Step 2: Take length n from user Step 3: Randomly choose characters’ n times and create a string of length n Step 4: EndExample Code Live Demo#include #include #include #include using namespace std; static ... Read More

Convert IntStream to String in Java

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

321 Views

If you have IntStream with ASCII values, then easily convert it to string using the below given example.For IntStream class, import the following packageimport java.util.stream.IntStream;Let’s say the following is our IntStreamIntStream stream = "Example".chars();Now, convert the IntStream to stringString str = stream.collect(StringBuilder::new,    StringBuilder::appendCodePoint,    StringBuilder::append).toString();The following is an example to convert IntStream to String in JavaExampleimport java.util.stream.IntStream; class Demo {    public static void main(String[] args) {       IntStream stream = "Example".chars();       System.out.println("The ASCII values...");       String str = stream.collect(StringBuilder::new,       StringBuilder::appendCodePoint,       StringBuilder::append).toString();       System.out.println("The ... Read More

Extract all integers from string in C++

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

5K+ Views

Here we will see how to extract all integers from strings in C++. We can put a string where numbers and no-numbers are present. We will extract all numeric values from it.To solve this problem, we will use the stringstream class in C++. We will cut the string word by word and then try to convert it into integer type data. if the conversion is done, then it is integer and print the value.Input: A string with some numbers “Hello 112 World 35 75” Output: 112 35 75AlgorithmStep 1:Take a number string Step 2: Divide it into different words Step ... Read More

DoubleStream sorted() method in Java

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

50 Views

The sorted() method of the DoubleStream class returns a stream consisting of the elements of this stream in sorted order.The syntax is as followsDoubleStream sorted()To use the DoubleStream class in Java, import the following packageimport java.util.stream.DoubleStream;Create a DoubleStream and add some elements to the streamDoubleStream doubleStream = DoubleStream.of(78.9, 90.4, 27.9, 20.6, 45.3, 18.5);Now, sort the elements of the streamdoubleStream.sorted().The following is an example to implement DoubleStream sorted() method in JavaExample Live Demoimport java.util.*; import java.util.stream.DoubleStream; public class Demo {    public static void main(String[] args) {       DoubleStream doubleStream = DoubleStream.of(78.9, 90.4, 27.9, 20.6, 45.3, 18.5);       ... Read More

Converting string to number and vice-versa in C++

Jennifer Nicholas
Updated on 30-Jul-2019 22:30:25

612 Views

In this section we will see how to convert string to number and number to string. At first we will see how to convert string to number.String to Number ConversionHere we will see how to convert a number string to integer type data. We can solve this problem by using the atoi() function. This function takes string as input and converts into integer data.The atoi() function is present in the library.Input: A number string “1234” Output: 1234AlgorithmStep 1:Take a number string Step 2: Convert it to integer using atoi() function Step 3: Print the result. Step 4: EndExample Code Live ... Read More

Collectors toCollection() method in Java 8

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

852 Views

The toCollection() method of the Collectors class in Java returns a Collector that accumulates the input elements into a new Collection in encounter order.The syntax is as followsstatic Collector toCollection(Supplier collectionFactory)Here, the parameterT - Type of the input elementsC - Type of the resulting CollectionSupplier: A supplier of resultscollectionFactory - Supplier that returns a new, empty Collection of the appropriate typeTo work with Collectors class in Java, import the following packageimport java.util.stream.Collectors;The following is an example to implement toCollection() method in JavaExample Live Demoimport java.util.Collection; import java.util.TreeSet; import java.util.stream.Collectors; import java.util.stream.Stream; public class Demo {    public static void main(String[] ... Read More

Convert number string to integers in C++

Vrundesha Joshi
Updated on 30-Jul-2019 22:30:25

237 Views

Here we will see how to convert a number string to integer type data. We can solve this problem by using the atoi() function. This function takes string as input and converts into integer data.The atoi() function is present in the library.Input: A number string “1234” Output: 1234AlgorithmStep 1: Take a number string Step 2: Convert it to integer using atoi() function Step 3: Print the result. Step 4: EndExample Code Live Demo#include #include using namespace std; main() {    int n;    char num_string[20] = "1234"; n = atoi(num_string); cout

Convert an integer to a hex string in C++

Nitya Raut
Updated on 30-Jul-2019 22:30:25

16K+ Views

In this program we will see how to convert an integer to hex string. To convert an integer into hexadecimal string we can follow mathematical steps. But in this case we have solved this problem using simple trick.In C / C++ there is a format specifier %X. It prints the value of some variable into hexadecimal form. We have used this format specifier to convert number into a string by using sprintf() function.Input: An integer number 255 Output: FFAlgorithmStep 1:Take a number from the user Step 2: Make a string after converting number using %X format specifier Step 3: Print ... Read More

How to create Ennead Tuple in Java?

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

72 Views

To create Ennead Tuple, you can use the with() method or even constructor. Let us see how to create Ennead Tuple and add elements to it with Constructor.Let us first see what we need to work with JavaTuples. To work with Ennead class in JavaTuples, you need to import the following packageimport org.javatuples.Ennead;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 JavaTuplesSteps ... Read More

Advertisements