Programming Articles - Page 2175 of 3366

How to use IntSupplier in lambda expression in Java?

raja
Updated on 13-Jul-2020 08:41:18

989 Views

An IntSupplier is a functional interface defined in "java.util.function" package. This interface represents an operation that takes without arguments and returns the result of int type. IntSupplier interface has only one method, getAsInt() and returns a result. This functional interface can be used as an assignment target for lambda expressions or method references.Syntax@FunctionalInterface public interface IntSupplier {    int getAsInt(); }Exampleimport java.util.function.IntSupplier; public class IntSupplierTest {    public static void main(String[] args) {       IntSupplier intSupplier1 = () -> Integer.MAX_VALUE;  // lamba expression       System.out.println("The maximum value of an Integer is: " + intSupplier1.getAsInt());       IntSupplier intSupplier2 = () -> ... Read More

How to use IntStream in lambdas and method references in Java?

raja
Updated on 13-Jul-2020 08:42:07

6K+ Views

An IntStream interface extends the BaseStream interface in Java 8. It is a sequence of primitive int-value elements and a specialized stream for manipulating int values. We can also use the IntStream interface to iterate the elements of a collection in lambda expressions and method references.Syntaxpublic interface IntStream extends BaseStreamExampleimport java.util.stream.IntStream; public class StringToIntegerStreamTest {    public static void main(String[] args) {       String str = "Tutorials Point";       IntStream stream = str.chars();       stream.forEach(element -> System.out.println(((char)element))); // using lambda expression    } }OutputT u t o r i a l s P o i n tExampleimport java.util.*; ... Read More

Construct BST from its given level order traversal in C++

Arnab Chakraborty
Updated on 03-Jan-2020 11:42:38

407 Views

Suppose we have one level order traversal. From this traversal. we have to generate the tree So if the traversal is like [7, 4, 12, 3, 6, 8, 1, 5, 10], then the tree will be like −To solve this, we will use recursive approach. The first element will be the root. The second element will be the left child, and third element will be right child (If the condition of BST satisfies), this property will be satisfied for all elements. So we will follow these steps −At first we have to take the first element of the array, and ... Read More

Construct BST from given preorder traversal - Set 2 in C++

Arnab Chakraborty
Updated on 03-Jan-2020 11:39:48

224 Views

Suppose we have one pre order traversal. From this traversal. we have to generate the tree So if the traversal is like [10, 5, 1, 7, 40, 50], then the tree will be like −To solve this, we will follow these steps −Create empty stackMake the first value as root, and push it into stack.Now keep pooping while the stack is not empty and the next value is greater than stack top element, make this as right child of the last popped node. Now push the new node to the stack.When the next value is less than the top element, ... Read More

Construct BST from given preorder traversal - Set 1 in C++

Arnab Chakraborty
Updated on 03-Jan-2020 11:36:03

195 Views

Suppose we have one pre order traversal. From this traversal. we have to generate the tree So if the traversal is like [10, 5, 1, 7, 40, 50], then the tree will be like −To solve this, we will use this trick. The trick is to set a range {min… max} for each node. At first we will initialize the range as {INT_MIN… INT_MAX}. The first node will definitely be in range, so after that we will create root node. To construct the left subtree, set the range as {INT_MIN… root->data}. If a values is in the range {INT_MIN… root->data}, ... Read More

Construct a Turing Machine for language L = {wwr | w ∈ {0, 1}}

Arnab Chakraborty
Updated on 03-Jan-2020 11:31:12

6K+ Views

Here we will see how to make a Turing machine for language L = {WWr |W belongs to {0, 1}}. So this represents a kind of language where we will use only two characters 0s and 1s. The w is a string and wr is reverse of it. So if w = 10110, then wr will be 01101. So the Turing machine will accept the string z = 1011001101.To solve this, we will use this approach. First check the first symbol, if it’s 0 then replace it using y and if that is 1, then replace using x. Then go ... Read More

Construct a Turing Machine for language L = {ww | w ∈ {0,1}}

Arnab Chakraborty
Updated on 03-Jan-2020 11:29:23

7K+ Views

Here we will see how to make a Turing machine for language L = {WW |W belongs to {0, 1}}. So this represents a kind of language where we will use only two characters 0s and 1s. The w is a string. So if w = 10110, so the Turing machine will accept the string z = 1011010110.To solve this, we will use this approach. The first thing is to find the midpoint of the string, we will convert a 0 to x and 1 to y. After continuously doing it a point is reached when all 0’s and 1’s ... Read More

Construct a Turing Machine for language L = {0n1n2n | n≥1}

Arnab Chakraborty
Updated on 03-Jan-2020 11:28:08

5K+ Views

Here we will see how to make a Turing machine for language L = {0n1n2n | n ≥ n}. So this represents a kind of language where we will use only three characters 0s, 1s and 2s. The w is a string. So if w = 000111222, The Turing machine will accept it.To solve this, we will use this approach. First replace one 0 from front by x, then keep moving right till we get one 1 and replace this 1 by y. Again, keep moving right till we get one 2, replace it by z and move left. Now ... Read More

Construct a Turing machine for L = {aibjck | i>j>k; k ≥ 1}

Arnab Chakraborty
Updated on 03-Jan-2020 11:26:31

845 Views

Here we will see how to make a Turing machine for language L = {AiBjCk | i > j > k; k ≥ 1}. So this represents a kind of language where we will use only three characters a, b and c. The w is a string. So if w = aaaaaabbbbccc, The Turing machine will accept it.To solve this, we will use this approach. Firstly compare two elements by making A and D as a single element, after that comparing A and D if count of C is greater than |(A, D)|, then the string will not be accepted, ... Read More

Construct a Turing machine for L = {aibjck | i< j< k; i ≥ 1}

Arnab Chakraborty
Updated on 03-Jan-2020 11:24:50

2K+ Views

Here we will see how to make a Turing machine for language L = {AiBjCk | i < j < k; i ≥ 1}. So this represents a kind of language where we will use only three characters A, B and C. The w is a string. So if w = AABBBBCCCCC, The Turing machine will accept it.To solve this, we will use this approach. Firstly compare two elements as a single element, after that comparing the single element if |first| > |(Second, Third)|, and |Second| > |Third|, then it will be accepted. Now if |Third| > |(First, Second)| and ... Read More

Advertisements