Samual Sam

Samual Sam

1,507 Articles Published

Articles by Samual Sam

Page 12 of 151

Remove a value from Java LinkedHashMap

Samual Sam
Samual Sam
Updated on 11-Mar-2026 2K+ Views

Use the remove() method to remove a single value from LinkedHashMap −At first, create a LinkedHashMap and add some elements −LinkedHashMap l = new LinkedHashMap(); l.put("1", "Jack"); l.put("2", "Tom"); l.put("3", "Jimmy"); l.put("4", "Morgan"); l.put("5", "Tim"); l.put("6", "Brad");Now, let’s say you need to remove the element 2 from the LinkedHashMap. For that, use the remove() method −Object ob = l.remove("2");The following is an example to remove a value from LinkedHashMap in Java −Exampleimport java.util.*; public class Demo { public static void main(String[] args) { LinkedHashMap l = new LinkedHashMap(); ...

Read More

How to convert Integer array list to float array in Java?

Samual Sam
Samual Sam
Updated on 11-Mar-2026 1K+ Views

To convert integer array list to float array, let us first create an integer array list −ArrayList < Integer > arrList = new ArrayList < Integer > (); arrList.add(25); arrList.add(50); arrList.add(100); arrList.add(200); arrList.add(300); arrList.add(400); arrList.add(500);Now, convert integer array list to float array. We have first set the size to the float array. With that, each and every value of the integer array is assigned to the float array −final float[] arr = new float[arrList.size()]; int index = 0; for (final Integer value: arrList) {    arr[index++] = value; }Exampleimport java.util.ArrayList; public class Demo {    public static void main(String[] args) ...

Read More

C++ Program to Find Deepest Left Leaf in a Binary Tree

Samual Sam
Samual Sam
Updated on 11-Mar-2026 249 Views

A binary tree which has at most two children, specified as left child and right child. This is a C++ Program to Find Deepest Left Leaf in a Binary TreeAlgorithmBegin.    function deepestLLeafutil() find the deepest left leaf in a given    binary tree:         lvel is level of current node.       maxlvel is pointer to the deepest left leaf node found so far         isLeft Indicates that this node is left child of its parent         resPtr is Pointer to the result         If root is equal to Null ...

Read More

How to generate random values that won&rsquo;t repeat in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 496 Views

To generate random values that won’t repeat, use HashSet collection. Firstly, create a random object and HashSet −Random randNum = new Random(); Sets = new HashSet();Now, add random integers −while (s.size() < 10) {    s.add(randNum.nextInt()); }Now, display the random numbers that are unique −Listlist = new ArrayList(s); System.out.println(list);Exampleimport java.util.ArrayList; import java.util.HashSet; import java.util.Set; import java.util.Random; import java.util.List; public class Demo {    public static void main(String[] args) {       Random randNum = new Random();       Sets = new HashSet();       while (s.size() < 10) {          s.add(randNum.nextInt());       ...

Read More

How can I generate two separate outputs using Random in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 259 Views

To generate two separate outputs, at first create a new Random object −private static final Random r = new Random();Now, let us declare a value −int val = 5;Loop from the value till 100 and generate random numbers between 1 to 100 −while (val

Read More

How to convert Wrapper value array list into primitive array in Java?

Samual Sam
Samual Sam
Updated on 11-Mar-2026 758 Views

Here, to convert Wrapper value array list into primitive array, we are considering Integer as Wrapper, whereas double as primitive.At first, declare an Integer array list and add elements to it −ArrayList < Integer > arrList = new ArrayList < Integer > (); arrList.add(5); arrList.add(10); arrList.add(15); arrList.add(20); arrList.add(25); arrList.add(30); arrList.add(45); arrList.add(50);Now, convert the above Integer array list to primitive array. Firstly, we set the same size for the double array and then assigned each valuefinal double[] arr = new double[arrList.size()]; int index = 0; for (final Integer value: arrList) {    arr[index++] = value; }The following is an example to ...

Read More

Java Program to output fixed number of array elements in a line

Samual Sam
Samual Sam
Updated on 11-Mar-2026 233 Views

To output fixed number of array elements in a line, we should check for a condition and if that is true, we should place System.out.println(); properly to get a line and the process goes on.Here, we will display 5 elements in a line. At first, create a new integer array and add some elements −int[] list = new int[50]; for (int i = 0; i < list.length; i++) {    list[i] = (int)(i + 20); }Now, declare a new variable and initialize it to 0. This variable is checked in a for loop that loops until the length of the ...

Read More

C++ Program to Find Size of the Largest Independent Set(LIS) in a Given a Binary Tree

Samual Sam
Samual Sam
Updated on 11-Mar-2026 210 Views

This is a C++ Program to Find Size of the Largest Independent Set (LIS) in a Given a Binary Tree.AlgorithmBegin.    Create a structure n to declare data d, a left child pointer l and a right child pointer r.    Call a function max() to return maximum between two integers. Create a function LIS() to return the    size of the largest independent set in a given binary tree.    Calculate size excluding the current node    int size_excl = LIS(root->l) + LIS(root->r)    Calculate size including the current node    int size_incl = 1;    if (root->l)   ...

Read More

Create a TreeSet in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 226 Views

Create a TreeSet and add elements −TreeSet tSet = new TreeSet(); tSet.add("TV"); tSet.add("Radio"); tSet.add("Internet");Iterate through the elements −Iterator i = tSet.iterator(); while(i.hasNext()){ System.out.println(i.next()); }The following is an example to create a TreeSet −Exampleimport java.util.*; public class Demo { public static void main(String args[]){ TreeSet tSet = new TreeSet(); tSet.add("TV"); tSet.add("Radio"); tSet.add("Internet"); Iterator i = tSet.iterator(); while(i.hasNext()){ System.out.println(i.next()); } } }OutputInternet Radio TV

Read More

How to randomize and shuffle array of numbers in Java?

Samual Sam
Samual Sam
Updated on 11-Mar-2026 785 Views

At first, create an integer array −int[] arr = { 20, 40, 60, 80, 100, 120, 140, 160, 180, 200};Now, create a Random class object −Random rand = new Random();Loop until the length of the array and shuffle the elements −for (int i = 0; i < arr.length; ++i) {    int index = rand.nextInt(arr.length - i);    int tmp = arr[arr.length - 1 - i];    arr[arr.length - 1 - i] = arr[index];    arr[index] = tmp; }Exampleimport java.util.Arrays; import java.util.Random; public class Demo {    public static void main(String[] args) {       int[] arr = { ...

Read More
Showing 111–120 of 1,507 articles
« Prev 1 10 11 12 13 14 151 Next »
Advertisements